Skip to content

Commit

Permalink
Change use ImgLoc for uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Sep 18, 2023
1 parent 3d6248e commit 36b5678
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String> 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()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static long addJob(CommentEntry comment, Path imagePath, Consumer<SubmitJ
if (imagePath != null) {
NETWORK_EXECUTOR.execute(() -> {
try {
job.setImage(ImageUpload.uploadImage(imagePath, 256));
job.setImage(ImageUpload.uploadImage(imagePath, comment));
trySendPackage(jobId);
} catch (Exception ex) {
job.exception = ex;
Expand Down

0 comments on commit 36b5678

Please sign in to comment.