diff --git a/common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageUpload.java b/common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageUpload.java index d35b513..76b514a 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageUpload.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageUpload.java @@ -1,6 +1,7 @@ package cn.zbx1425.worldcomment.data.network; import cn.zbx1425.worldcomment.BuildConfig; +import cn.zbx1425.worldcomment.data.CommentEntry; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.apache.commons.io.FilenameUtils; @@ -22,54 +23,34 @@ public class ImageUpload { private static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); - private static String apiUrl = "https://smms.app/api/v2/upload"; - private static String apiToken = "ZpWPoCoqZ8B5TR0mmIF7k4XFxcoaLdom"; + private static String apiUrl = "https://imgloc.com/api/1/upload"; + private static String apiToken = "chv_WdW_afa0889c65f7afcdbd13fc2528d9b9793d193a9a8e16c78f964be1e731cd10e9884d806a1bf767d45cafc5952216827d65541a31174b5acc45f988e6a2210885"; - public static ThumbImage uploadImage(Path imagePath, int thumbWidth) throws IOException, InterruptedException { - BufferedImage fullSizeImage = ImageIO.read(imagePath.toFile()); - String fullSizeUrl = uploadImage(Files.readAllBytes(imagePath), imagePath.getFileName().toString()); - String thumbUrl; - if (fullSizeImage.getWidth() < thumbWidth) { - thumbUrl = fullSizeUrl; - } else { - BufferedImage thumbImage = new BufferedImage(thumbWidth, - (int)((float)fullSizeImage.getHeight() / fullSizeImage.getWidth() * thumbWidth), - fullSizeImage.getType()); - thumbImage.getGraphics().drawImage( - fullSizeImage.getScaledInstance(thumbWidth, thumbImage.getHeight(), Image.SCALE_DEFAULT), - 0, 0, null); - ByteArrayOutputStream oStream = new ByteArrayOutputStream(64 * 1024); - ImageIO.write(thumbImage, "png", oStream); - thumbUrl = uploadImage(oStream.toByteArray(), - FilenameUtils.removeExtension(imagePath.getFileName().toString()) + ".thumb.png"); - } - return new ThumbImage(fullSizeUrl, thumbUrl); - } - - public static String uploadImage(byte[] data, String fileName) throws IOException, InterruptedException { + public static ThumbImage uploadImage(Path imagePath, CommentEntry comment) throws IOException, InterruptedException { MimeMultipartData body = MimeMultipartData.newBuilder() .withCharset(StandardCharsets.UTF_8) - .addFile("smfile", fileName, data, "image/png") + .addFile("source", imagePath.getFileName().toString(), Files.readAllBytes(imagePath), "image/png") + .addText("title", "WorldComment from " + comment.initiatorName) + .addText("description", comment.message) .build(); HttpRequest reqUpload = HttpRequest.newBuilder(URI.create(apiUrl)) .header("Content-Type", body.getContentType()) .header("User-Agent", "Mozilla/5.0 WorldComment/" + BuildConfig.MOD_VERSION + " +https://www.zbx1425.cn") - .header("Authorization", "Basic " + apiToken) + .header("X-API-Key", apiToken) .POST(body.getBodyPublisher()) .build(); HttpResponse respUpload = HTTP_CLIENT.send(reqUpload, HttpResponse.BodyHandlers.ofString()); if (respUpload.statusCode() != 200) throw new IOException("Upload HTTP " + respUpload.statusCode() + "\n" + respUpload.body()); JsonObject respObj = JsonParser.parseString(respUpload.body()).getAsJsonObject(); - if (!respObj.get("success").getAsBoolean()) { - if (respObj.get("code").getAsString().equals("image_repeated")) { - return respObj.get("images").getAsString(); - } else { - throw new IOException("Upload Fail " + respUpload.body()); - } + if (!respObj.has("success")) { + throw new IOException("Upload Fail " + respUpload.body()); } else { - return respObj.get("data").getAsJsonObject().get("url").getAsString(); + return new ThumbImage( + respObj.get("image").getAsJsonObject().get("url").getAsString(), + respObj.get("image").getAsJsonObject().get("medium").getAsJsonObject().get("url").getAsString() + ); } } } diff --git a/common/src/main/java/cn/zbx1425/worldcomment/data/network/SubmitDispatcher.java b/common/src/main/java/cn/zbx1425/worldcomment/data/network/SubmitDispatcher.java index af8f116..a80cf6e 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/data/network/SubmitDispatcher.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/data/network/SubmitDispatcher.java @@ -28,7 +28,7 @@ public static long addJob(CommentEntry comment, Path imagePath, Consumer { try { - job.setImage(ImageUpload.uploadImage(imagePath, 256)); + job.setImage(ImageUpload.uploadImage(imagePath, comment)); trySendPackage(jobId); } catch (Exception ex) { job.exception = ex;