21
21
22
22
import com .google .gson .Gson ;
23
23
import com .google .gson .reflect .TypeToken ;
24
- import com .sk89q .worldedit .util .net .HttpRequest ;
25
24
26
25
import java .io .IOException ;
27
26
import java .net .URI ;
28
27
import java .net .URL ;
28
+ import java .net .http .HttpClient ;
29
+ import java .net .http .HttpRequest ;
30
+ import java .net .http .HttpResponse ;
29
31
import java .util .Map ;
30
32
import java .util .concurrent .Callable ;
31
33
@@ -49,47 +51,49 @@ private PasteTask(String content, PasteMetadata metadata) {
49
51
50
52
@ Override
51
53
public URL call () throws IOException , InterruptedException {
52
- URL initialUrl = HttpRequest .url ("https://paste.enginehub.org/signed_paste" );
53
-
54
- HttpRequest requestBuilder = HttpRequest .get (initialUrl );
55
-
56
- requestBuilder .header ("x-paste-meta-from" , "EngineHub" );
57
- if (metadata .name != null ) {
58
- requestBuilder .header ("x-paste-meta-name" , metadata .name );
59
- }
60
- if (metadata .author != null ) {
61
- requestBuilder .header ("x-paste-meta-author" , metadata .author );
54
+ try (HttpClient client = HttpClient .newHttpClient ()) {
55
+ HttpRequest .Builder signRequestBuilder = HttpRequest .newBuilder ()
56
+ .uri (URI .create ("https://paste.enginehub.org/signed_paste_v2" ))
57
+ .header ("x-paste-meta-from" , "EngineHub" );
58
+
59
+ if (metadata .name != null ) {
60
+ signRequestBuilder = signRequestBuilder .header ("x-paste-meta-name" , metadata .name );
61
+ }
62
+ if (metadata .author != null ) {
63
+ signRequestBuilder = signRequestBuilder .header ("x-paste-meta-author" , metadata .author );
64
+ }
65
+ if (metadata .extension != null ) {
66
+ signRequestBuilder = signRequestBuilder .header ("x-paste-meta-extension" , metadata .extension );
67
+ }
68
+
69
+ SignedPasteResponse response = GSON .fromJson (
70
+ client .send (signRequestBuilder .build (), HttpResponse .BodyHandlers .ofString ()).body (),
71
+ TypeToken .get (SignedPasteResponse .class ).getType ()
72
+ );
73
+
74
+ HttpRequest .Builder uploadRequestBuilder = HttpRequest .newBuilder ()
75
+ .uri (URI .create (response .uploadUrl ))
76
+ .method ("PUT" , HttpRequest .BodyPublishers .ofString (content ));
77
+
78
+ for (Map .Entry <String , String > entry : response .headers .entrySet ()) {
79
+ uploadRequestBuilder = uploadRequestBuilder .header (entry .getKey (), entry .getValue ());
80
+ }
81
+
82
+ // If this succeeds, it will not return any data aside from a 204 status.
83
+ HttpResponse <String > uploadResponse = client .send (uploadRequestBuilder .build (), HttpResponse .BodyHandlers .ofString ());
84
+
85
+ if (uploadResponse .statusCode () != 200 && uploadResponse .statusCode () != 204 ) {
86
+ throw new IOException ("Failed to upload paste: " + uploadResponse .statusCode ());
87
+ }
88
+
89
+ return URI .create (response .viewUrl ).toURL ();
62
90
}
63
- if (metadata .extension != null ) {
64
- requestBuilder .header ("x-paste-meta-extension" , metadata .extension );
65
- }
66
-
67
- SignedPasteResponse response = GSON .fromJson (requestBuilder
68
- .execute ()
69
- .expectResponseCode (200 )
70
- .returnContent ()
71
- .asString ("UTF-8" ), TypeToken .get (SignedPasteResponse .class ).getType ());
72
-
73
- HttpRequest .Form form = HttpRequest .Form .create ();
74
- for (Map .Entry <String , String > entry : response .uploadFields .entrySet ()) {
75
- form .add (entry .getKey (), entry .getValue ());
76
- }
77
- form .add ("file" , content );
78
-
79
- URL url = HttpRequest .url (response .uploadUrl );
80
- // If this succeeds, it will not return any data aside from a 204 status.
81
- HttpRequest .post (url )
82
- .bodyMultipartForm (form )
83
- .execute ()
84
- .expectResponseCode (200 , 204 );
85
-
86
- return URI .create (response .viewUrl ).toURL ();
87
91
}
88
92
}
89
93
90
94
private static final class SignedPasteResponse {
91
95
String viewUrl ;
92
96
String uploadUrl ;
93
- Map <String , String > uploadFields ;
97
+ Map <String , String > headers ;
94
98
}
95
99
}
0 commit comments