2929import java .util .Base64 ;
3030import java .util .Optional ;
3131import java .util .concurrent .CompletableFuture ;
32+ import java .util .regex .Matcher ;
33+ import java .util .regex .Pattern ;
3234
3335import io .github .axolotlclient .api .API ;
3436import io .github .axolotlclient .api .Constants ;
3537import io .github .axolotlclient .api .Request ;
3638
3739public abstract class ImageNetworking {
3840
41+ private static final Pattern URL_PATTERN = Pattern .compile ("(?:.+/)?(\\ d+)(?:/?.+)?" );
42+
3943 public abstract void uploadImage (Path file );
4044
4145 protected CompletableFuture <String > upload (Path file ) {
@@ -52,17 +56,18 @@ protected CompletableFuture<String> upload(String name, byte[] data) {
5256 }
5357
5458 protected static String idToUrl (String id ) {
55- return Request .Route .IMAGE .builder ().path (id ).path ("raw " ).build ().resolve ().toString ();
59+ return Request .Route .IMAGE .builder ().path (id ).path ("view " ).build ().resolve ().toString ();
5660 }
5761
5862 protected static Optional <String > urlToId (String url ) {
5963 if (url .contains ("/" ) && !url .startsWith (Constants .API_URL )) {
6064 return Optional .empty ();
6165 }
62- if (url .endsWith ("/raw" )) {
63- url = url .substring (0 , url .length () - 4 );
66+ Matcher matcher = URL_PATTERN .matcher (url );
67+ if (!matcher .matches ()) {
68+ return Optional .empty ();
6469 }
65- return Optional .of (url . substring ( url . lastIndexOf ( "/" ) + 1 ));
70+ return Optional .of (matcher . group ( 1 ));
6671 }
6772
6873 protected static Optional <String > ensureUrl (String urlOrId ) {
0 commit comments