|
| 1 | +package org.magic.services.tools; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.Date; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.apache.http.entity.mime.MultipartEntityBuilder; |
| 9 | +import org.apache.http.entity.mime.content.FileBody; |
| 10 | +import org.apache.logging.log4j.Logger; |
| 11 | +import org.magic.services.logging.MTGLogger; |
| 12 | +import org.magic.services.network.MTGHttpClient; |
| 13 | +import org.magic.services.network.RequestBuilder; |
| 14 | +import org.magic.services.network.URLTools; |
| 15 | + |
| 16 | +import com.google.gson.JsonObject; |
| 17 | + |
| 18 | +public class PostImage { |
| 19 | + |
| 20 | + private Logger logger = MTGLogger.getLogger(PostImage.class); |
| 21 | + private int expirationDay = 0; |
| 22 | + private static final String BASE_URL = "https://postimages.org/"; |
| 23 | + MTGHttpClient client = URLTools.newClient(); |
| 24 | + |
| 25 | + |
| 26 | + public void setExpirationDay(int expirationDay) { |
| 27 | + this.expirationDay = expirationDay; |
| 28 | + } |
| 29 | + |
| 30 | + public boolean isProxified(String url) |
| 31 | + { |
| 32 | + return url.contains("postimg"); |
| 33 | + } |
| 34 | + |
| 35 | + public String upload(String path) throws IOException |
| 36 | + { |
| 37 | + if(path.startsWith("http")) |
| 38 | + return uploadUrl(path); |
| 39 | + else |
| 40 | + return uploadFile(new File(path)); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + public String uploadFile(File f) throws IOException { |
| 46 | + |
| 47 | + var ret = client.doPost(BASE_URL + "/json", MultipartEntityBuilder.create().addPart("file", new FileBody(f)) |
| 48 | + .addTextBody("gallery","") |
| 49 | + .addTextBody("expire",String.valueOf(expirationDay)) |
| 50 | + .addTextBody("numfiles","1") |
| 51 | + .addTextBody("upload_session",generateSessionId()) |
| 52 | + .build(), generateHeaders()); |
| 53 | + |
| 54 | + return directLink(URLTools.toJson(ret.getEntity().getContent()).getAsJsonObject()); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + public String uploadUrl(String url) throws IOException { |
| 60 | + |
| 61 | + var jsonRet = RequestBuilder.build().setClient(client).post().url(BASE_URL + "/json") |
| 62 | + .addHeaders(generateHeaders()) |
| 63 | + .addContent("gallery", "") |
| 64 | + .addContent("optsize", "0") |
| 65 | + .addContent("expire", String.valueOf(expirationDay)) |
| 66 | + .addContent("url", url) |
| 67 | + .addContent("numfiles", "1") |
| 68 | + .addContent("upload_session",generateSessionId()) |
| 69 | + .toJson() |
| 70 | + .getAsJsonObject(); |
| 71 | + |
| 72 | + logger.debug("upload result : {}", jsonRet); |
| 73 | + |
| 74 | + return directLink(jsonRet); |
| 75 | + } |
| 76 | + |
| 77 | + private Map<String,String> generateHeaders() |
| 78 | + { |
| 79 | + return Map.of(URLTools.ORIGIN, BASE_URL, URLTools.REFERER, BASE_URL + "/web", |
| 80 | + URLTools.ACCEPT, URLTools.HEADER_JSON, |
| 81 | + URLTools.ACCEPT_ENCODING, "gzip, deflate, br, zstd", |
| 82 | + URLTools.ACCEPT_LANGUAGE,"fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7\r\n", |
| 83 | + "priority", "u=1, i", |
| 84 | + "Cache-Control", "no-cache", |
| 85 | + "x-requested-with", "XMLHttpRequest"); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + private String generateSessionId() { |
| 90 | + return new Date().getTime() + Double.toString(CryptoUtils.randomDouble(Double.MAX_VALUE)).substring(1); |
| 91 | + } |
| 92 | + |
| 93 | + private String directLink(JsonObject jsonRet) throws IOException { |
| 94 | + |
| 95 | + var url = jsonRet.get("url").getAsString(); |
| 96 | + try { |
| 97 | + return RequestBuilder.build().setClient(client).get().url(url).toHtml().getElementById("direct").attr("value"); |
| 98 | + } catch (Exception _) { |
| 99 | + logger.error("error to upload {} : {}", url, jsonRet); |
| 100 | + throw new IOException("Error at upload"); |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments