11package org .magic .services .tools ;
22
3+ import java .io .File ;
34import java .io .IOException ;
45import java .util .Date ;
6+ import java .util .Map ;
57
8+ import org .apache .http .entity .mime .MultipartEntityBuilder ;
9+ import org .apache .http .entity .mime .content .FileBody ;
610import org .apache .logging .log4j .Logger ;
711import org .magic .services .logging .MTGLogger ;
12+ import org .magic .services .network .MTGHttpClient ;
813import org .magic .services .network .RequestBuilder ;
914import org .magic .services .network .URLTools ;
1015
16+ import com .google .gson .JsonObject ;
17+
1118public class ImagePoster {
1219
1320 private Logger logger = MTGLogger .getLogger (ImagePoster .class );
1421 private int expirationDay = 0 ;
15-
22+ private static final String BASE_URL = "https://postimages.org/" ;
23+ MTGHttpClient client = URLTools .newClient ();
24+
25+
1626 public void setExpirationDay (int expirationDay ) {
1727 this .expirationDay = expirationDay ;
1828 }
@@ -22,33 +32,65 @@ public boolean isProxified(String url)
2232 return url .contains ("postimg" );
2333 }
2434
35+ public String upload (File f ) throws IOException {
36+
37+ var header = Map .of (URLTools .ORIGIN , BASE_URL ,
38+ URLTools .REFERER , BASE_URL + "/web" ,
39+ URLTools .ACCEPT , URLTools .HEADER_JSON ,
40+ URLTools .ACCEPT_ENCODING , "gzip, deflate, br, zstd" ,
41+ URLTools .ACCEPT_LANGUAGE ,"fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7\r \n " ,
42+ "priority" , "u=1, i" ,
43+ "Cache-Control" , "no-cache" ,
44+ "x-requested-with" , "XMLHttpRequest" );
45+
46+ var ret = client .doPost (BASE_URL + "/json" , MultipartEntityBuilder .create ().addPart ("file" , new FileBody (f ))
47+ .addTextBody ("gallery" ,"" )
48+ .addTextBody ("expire" ,String .valueOf (expirationDay ))
49+ .addTextBody ("numfiles" ,"1" )
50+ .addTextBody ("upload_session" ,generateSessionId ())
51+ .build (), header );
52+
53+
54+ return directLink (URLTools .toJson (ret .getEntity ().getContent ()).getAsJsonObject ());
55+ }
56+
2557
58+ private String generateSessionId () {
59+ return new Date ().getTime () + Double .toString (CryptoUtils .randomDouble (Double .MAX_VALUE )).substring (1 );
60+ }
61+
2662 public String upload (String url ) throws IOException {
27- var baseUrl = "https://postimages.org/" ;
28- var client = URLTools . newClient ();
63+
64+
2965
30- var jsonRet = RequestBuilder .build ().setClient (client ).post ().url (baseUrl + "/json" )
66+ var jsonRet = RequestBuilder .build ().setClient (client ).post ().url (BASE_URL + "/json" )
3167
32- .addHeader (URLTools .ORIGIN , baseUrl ).addHeader (URLTools .REFERER , baseUrl + "/web" )
68+ .addHeader (URLTools .ORIGIN , BASE_URL ).addHeader (URLTools .REFERER , BASE_URL + "/web" )
3369 .addHeader (URLTools .ACCEPT , URLTools .HEADER_JSON )
3470 .addHeader (URLTools .ACCEPT_ENCODING , "gzip, deflate, br, zstd" ).addHeader ("priority" , "u=1, i" )
3571 .addHeader ("Cache-Control" , "no-cache" ).addHeaders (URLTools .createSecHeaders ())
3672 .addHeader ("x-requested-with" , "XMLHttpRequest" )
3773 .addContent ("gallery" , "" ).addContent ("optsize" , "0" )
38- .addContent ("expire" , String .valueOf (expirationDay )).addContent ("url" , url ).addContent ("numfiles" , "1" )
39- .addContent ("upload_session" ,
40- new Date ().getTime () + Double .toString (CryptoUtils .randomDouble (Double .MAX_VALUE )).substring (1 ))
74+ .addContent ("expire" , String .valueOf (expirationDay ))
75+ .addContent ("url" , url )
76+ .addContent ("numfiles" , "1" )
77+ .addContent ("upload_session" ,generateSessionId ())
4178 .toJson ().getAsJsonObject ();
4279
4380 logger .debug ("upload result : {}" , jsonRet );
4481
45- try {
46- return RequestBuilder .build ().setClient (client ).post ().url (jsonRet .get ("url" ).getAsString ()).toHtml ()
47- .getElementById ("direct" ).attr ("value" );
48- } catch (Exception _) {
49- logger .error ("error to upload {} : {}" , url , jsonRet );
50- throw new IOException ("Error at upload" );
51- }
82+ return directLink (jsonRet );
83+ }
84+
85+ private String directLink (JsonObject jsonRet ) throws IOException {
86+
87+ var url = jsonRet .get ("url" ).getAsString ();
88+ try {
89+ return RequestBuilder .build ().setClient (client ).get ().url (url ).toHtml ().getElementById ("direct" ).attr ("value" );
90+ } catch (Exception _) {
91+ logger .error ("error to upload {} : {}" , url , jsonRet );
92+ throw new IOException ("Error at upload" );
93+ }
5294
5395 }
5496
0 commit comments