22
33import com .google .gson .*;
44import io .github .kiber2009 .plugin .contentapi .ContentApiPlugin ;
5- import io .github .kiber2009 .plugin .contentapi .utils .HttpUtils ;
5+ import org .apache .http .client .methods .CloseableHttpResponse ;
6+ import org .apache .http .client .methods .HttpPost ;
7+ import org .apache .http .entity .ContentType ;
8+ import org .apache .http .entity .mime .MultipartEntityBuilder ;
9+ import org .apache .http .impl .client .CloseableHttpClient ;
10+ import org .apache .http .impl .client .HttpClients ;
11+ import org .apache .http .util .EntityUtils ;
612import org .jspecify .annotations .NonNull ;
713
814import java .io .IOException ;
915import java .lang .reflect .Type ;
1016import java .net .URI ;
11- import java .net .http .HttpClient ;
12- import java .net .http .HttpRequest ;
13- import java .net .http .HttpResponse ;
14- import java .util .UUID ;
1517
1618public class LobfileUploadProvider implements UploadProvider {
1719 private static final URI UPLOAD_URI = URI .create ("https://lobfile.com/api/v3/upload" );
18- private static final HttpClient HTTP_CLIENT = HttpClient .newHttpClient ();
1920
2021 private final String apiKey ;
2122
@@ -24,23 +25,23 @@ public LobfileUploadProvider(final String apiKey) {
2425 }
2526
2627 @ Override
27- public @ NonNull String uploadBytes (final byte @ NonNull [] bytes ) {
28- final String boundary = UUID . randomUUID (). toString ();
29- try {
30- final HttpRequest request = HttpRequest . newBuilder ()
31- . uri ( UPLOAD_URI )
32- .header ( "X-API-Key " , apiKey )
33- .header ( "Content-Type " , "multipart/form-data; boundary=" + boundary )
34- .POST ( HttpRequest . BodyPublishers . ofByteArray ( HttpUtils . createMultipartBody ( boundary ,
35- new HttpUtils . MultipartEntry ( "file" , bytes ))))
36- . build ( );
37- final HttpResponse < String > httpResponse = HTTP_CLIENT . send ( request , HttpResponse . BodyHandlers . ofString () );
38- final Response response = ContentApiPlugin . GSON . fromJson ( httpResponse . body (), Response . class );
39- if ( response .success )
40- return response . url ;
41- else
42- throw new RuntimeException ( "Error: " + response . error );
43- } catch (final IOException | InterruptedException e ) {
28+ public @ NonNull String uploadBytes (final @ NonNull String fileName , final byte @ NonNull [] bytes ) {
29+ try ( final CloseableHttpClient client = HttpClients . createDefault ()) {
30+ final HttpPost request = new HttpPost ( UPLOAD_URI );
31+ request . setHeader ( "X-API-Key" , apiKey );
32+ request . setEntity ( MultipartEntityBuilder . create ( )
33+ .addBinaryBody ( "file " , bytes , ContentType . APPLICATION_OCTET_STREAM , fileName )
34+ .addTextBody ( "sha256 " , "john_doe" )
35+ .build ());
36+ try ( final CloseableHttpResponse httpResponse = client . execute ( request )) {
37+ final String responseString = EntityUtils . toString ( httpResponse . getEntity () );
38+ final Response response = ContentApiPlugin . GSON . fromJson ( responseString , Response . class );
39+ if ( response . success )
40+ return response .url ;
41+ else
42+ throw new RuntimeException ( "Error: " + response . error );
43+ }
44+ } catch (final IOException e ) {
4445 throw new RuntimeException (e );
4546 }
4647 }
0 commit comments