1
- using Newtonsoft . Json ;
2
- using Newtonsoft . Json . Linq ;
3
1
using OpenDirectoryDownloader . Models ;
2
+ using System . Net . Http . Headers ;
3
+ using System . Net . Http . Json ;
4
+ using System . Text ;
5
+ using System . Text . Json ;
6
+ using System . Text . Json . Serialization ;
4
7
5
8
namespace OpenDirectoryDownloader . FileUpload ;
6
9
@@ -17,35 +20,70 @@ public async Task<IFileUploadSiteFile> UploadFile(HttpClient httpClient, string
17
20
{
18
21
try
19
22
{
20
- string jsonServer = await httpClient . GetStringAsync ( "https://api.gofile.io/servers" ) ;
23
+ GoFileIoServersResponse serversResponse = await httpClient . GetFromJsonAsync < GoFileIoServersResponse > ( "https://api.gofile.io/servers" ) ;
21
24
22
- JObject result = JObject . Parse ( jsonServer ) ;
25
+ if ( serversResponse . Status != "ok" )
26
+ {
27
+ throw new Exception ( "GoFile.io servers error, in maintenance?" ) ;
28
+ }
29
+
30
+ string server = serversResponse . Data . Servers . First ( ) . Name ;
31
+
32
+ using HttpResponseMessage httpResponseMessageAccount = await httpClient . PostAsync ( "https://api.gofile.io/accounts" , new StringContent ( "{}" , Encoding . UTF8 , "text/plain" ) ) ;
33
+
34
+ GoFileIoAccountsResponse accountsResponse = await httpResponseMessageAccount . Content . ReadFromJsonAsync < GoFileIoAccountsResponse > ( ) ;
35
+
36
+ if ( accountsResponse . Status != "ok" )
37
+ {
38
+ throw new Exception ( "GoFile.io accounts error, in maintenance?" ) ;
39
+ }
40
+
41
+ httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accountsResponse . Data . Token ) ;
42
+
43
+ GoFileIoAccountResponse accountResponse = await httpClient . GetFromJsonAsync < GoFileIoAccountResponse > ( $ "https://api.gofile.io/accounts/{ accountsResponse . Data . Id } ") ;
44
+
45
+ if ( accountResponse . Status != "ok" )
46
+ {
47
+ throw new Exception ( "GoFile.io account error, in maintenance?" ) ;
48
+ }
49
+
50
+ using HttpResponseMessage httpResponseMessageCreateFolder = await httpClient . PostAsync ( "https://api.gofile.io/contents/createfolder" , new StringContent ( $ "{{\" parentFolderId\" : \" { accountsResponse . Data . RootFolder } \" }}", Encoding . UTF8 , "application/json" ) ) ;
23
51
24
- if ( result [ "status" ] . Value < string > ( ) == "error" )
52
+ GoFileIoFolderResponse createFolderResponse = await httpResponseMessageCreateFolder . Content . ReadFromJsonAsync < GoFileIoFolderResponse > ( ) ;
53
+
54
+ if ( createFolderResponse . Status != "ok" )
25
55
{
26
- throw new Exception ( "GoFile.io error, probably in maintenance" ) ;
56
+ throw new Exception ( "GoFile.io create folder error, in maintenance? " ) ;
27
57
}
28
58
29
- string server = result . SelectToken ( "data.servers" ) . First ( ) [ "name" ] . Value < string > ( ) ;
59
+ using HttpResponseMessage httpResponseMessageUpdateFolder = await httpClient . PutAsync ( $ "https://api.gofile.io/contents/{ createFolderResponse . Data . Id } /update", new StringContent ( "{\" attribute\" : \" public\" , \" attributeValue\" : \" true\" }" , Encoding . UTF8 , "application/json" ) ) ;
60
+
61
+ GoFileIoFolderResponse updateFolderResponse = await httpResponseMessageUpdateFolder . Content . ReadFromJsonAsync < GoFileIoFolderResponse > ( ) ;
62
+
63
+ if ( updateFolderResponse . Status != "ok" )
64
+ {
65
+ throw new Exception ( "GoFile.io update folder error, in maintenance?" ) ;
66
+ }
30
67
31
68
using FileStream fileStream = new ( path , FileMode . Open ) ;
32
69
using StreamContent streamContent = new ( fileStream ) ;
33
70
34
71
using MultipartFormDataContent multipartFormDataContent = new ( $ "Upload----{ Guid . NewGuid ( ) } ")
35
72
{
73
+ { new StringContent ( createFolderResponse . Data . Id ) , "folderId" } ,
36
74
{ streamContent , "file" , Path . GetFileName ( path ) }
37
75
} ;
38
76
39
- using HttpResponseMessage httpResponseMessage = await httpClient . PostAsync ( $ "https://{ server } .gofile.io/uploadFile", multipartFormDataContent ) ;
77
+ using HttpResponseMessage httpResponseMessageUploadFile = await httpClient . PostAsync ( $ "https://{ server } .gofile.io/contents /uploadFile", multipartFormDataContent ) ;
40
78
41
- if ( httpResponseMessage . IsSuccessStatusCode )
79
+ if ( httpResponseMessageUploadFile . IsSuccessStatusCode )
42
80
{
43
- string response = await httpResponseMessage . Content . ReadAsStringAsync ( ) ;
44
- OpenDirectoryIndexer . Session . UploadedUrlsResponse = response ;
81
+ GoFileIoUploadFileResponse uploadFileResponse = await httpResponseMessageUploadFile . Content . ReadFromJsonAsync < GoFileIoUploadFileResponse > ( ) ;
82
+ OpenDirectoryIndexer . Session . UploadedUrlsResponse = JsonSerializer . Serialize ( uploadFileResponse ) ;
45
83
46
- Program . Logger . Debug ( "Response from {siteName}: {response}" , Name , response ) ;
84
+ Program . Logger . Debug ( "Response from {siteName}: {response}" , Name , uploadFileResponse ) ;
47
85
48
- return JsonConvert . DeserializeObject < GoFileIoFile > ( response ) ;
86
+ return JsonSerializer . Deserialize < GoFileIoFile > ( JsonSerializer . Serialize ( uploadFileResponse ) ) ;
49
87
}
50
88
else
51
89
{
@@ -69,20 +107,175 @@ public async Task<IFileUploadSiteFile> UploadFile(HttpClient httpClient, string
69
107
70
108
public class GoFileIoFile : IFileUploadSiteFile
71
109
{
72
- public string Url => $ "https://gofile.io/?c= { Data . Code } " ;
110
+ public string Url => Data . DownloadPage ;
73
111
74
- [ JsonProperty ( "status" ) ]
112
+ [ JsonPropertyName ( "status" ) ]
75
113
public string Status { get ; set ; }
76
114
77
- [ JsonProperty ( "data" ) ]
78
- public GoFileIoFileData Data { get ; set ; }
115
+ [ JsonPropertyName ( "data" ) ]
116
+ public GoFileIoUploadedFile Data { get ; set ; }
79
117
}
80
118
81
119
public class GoFileIoFileData
82
120
{
83
- [ JsonProperty ( "code" ) ]
121
+ [ JsonPropertyName ( "code" ) ]
84
122
public string Code { get ; set ; }
85
123
86
- [ JsonProperty ( "removalCode" ) ]
124
+ [ JsonPropertyName ( "removalCode" ) ]
87
125
public string RemovalCode { get ; set ; }
88
126
}
127
+
128
+ public class GoFileIoResponse
129
+ {
130
+ [ JsonPropertyName ( "status" ) ]
131
+ public string Status { get ; set ; }
132
+ }
133
+
134
+ public class GoFileIoServersResponse : GoFileIoResponse
135
+ {
136
+ [ JsonPropertyName ( "data" ) ]
137
+ public GoFileIoServersData Data { get ; set ; }
138
+ }
139
+
140
+ public class GoFileIoServersData
141
+ {
142
+ [ JsonPropertyName ( "servers" ) ]
143
+ public List < GoFileIoServer > Servers { get ; set ; }
144
+ }
145
+
146
+ public class GoFileIoServer
147
+ {
148
+ [ JsonPropertyName ( "name" ) ]
149
+ public string Name { get ; set ; }
150
+
151
+ [ JsonPropertyName ( "zone" ) ]
152
+ public string Zone { get ; set ; }
153
+ }
154
+
155
+ public class GoFileIoFolderResponse : GoFileIoResponse
156
+ {
157
+ [ JsonPropertyName ( "data" ) ]
158
+ public GoFileIoFolder Data { get ; set ; }
159
+ }
160
+
161
+ public class GoFileIoFolder
162
+ {
163
+ [ JsonPropertyName ( "code" ) ]
164
+ public string Code { get ; set ; }
165
+
166
+ [ JsonPropertyName ( "createTime" ) ]
167
+ public int CreateTime { get ; set ; }
168
+
169
+ [ JsonPropertyName ( "id" ) ]
170
+ public string Id { get ; set ; }
171
+
172
+ [ JsonPropertyName ( "modTime" ) ]
173
+ public int ModTime { get ; set ; }
174
+
175
+ [ JsonPropertyName ( "name" ) ]
176
+ public string Name { get ; set ; }
177
+
178
+ [ JsonPropertyName ( "owner" ) ]
179
+ public string Owner { get ; set ; }
180
+
181
+ [ JsonPropertyName ( "parentFolder" ) ]
182
+ public string ParentFolder { get ; set ; }
183
+
184
+ [ JsonPropertyName ( "type" ) ]
185
+ public string Type { get ; set ; }
186
+ }
187
+
188
+ public class GoFileIoAccountsResponse : GoFileIoResponse
189
+ {
190
+ [ JsonPropertyName ( "data" ) ]
191
+ public GoFileIoAccount Data { get ; set ; }
192
+ }
193
+
194
+ public class GoFileIoAccount
195
+ {
196
+ [ JsonPropertyName ( "id" ) ]
197
+ public string Id { get ; set ; }
198
+
199
+ [ JsonPropertyName ( "email" ) ]
200
+ public string Email { get ; set ; }
201
+
202
+ [ JsonPropertyName ( "rootFolder" ) ]
203
+ public string RootFolder { get ; set ; }
204
+
205
+ [ JsonPropertyName ( "tier" ) ]
206
+ public string Tier { get ; set ; }
207
+
208
+ [ JsonPropertyName ( "token" ) ]
209
+ public string Token { get ; set ; }
210
+
211
+ [ JsonPropertyName ( "statsCurrent" ) ]
212
+ public GoFileIoAccountStats StatsCurrent { get ; set ; }
213
+ }
214
+
215
+ public class GoFileIoAccountResponse : GoFileIoResponse
216
+ {
217
+ [ JsonPropertyName ( "data" ) ]
218
+ public GoFileIoAccount Data { get ; set ; }
219
+ }
220
+
221
+ public class GoFileIoAccountStats
222
+ {
223
+ [ JsonPropertyName ( "folderCount" ) ]
224
+ public int FolderCount { get ; set ; }
225
+
226
+ [ JsonPropertyName ( "fileCount" ) ]
227
+ public int FileCount { get ; set ; }
228
+
229
+ [ JsonPropertyName ( "storage" ) ]
230
+ public int Storage { get ; set ; }
231
+ }
232
+
233
+ public class GoFileIoUploadFileResponse : IFileUploadSiteFile
234
+ {
235
+ [ JsonPropertyName ( "data" ) ]
236
+ public GoFileIoUploadedFile Data { get ; set ; }
237
+
238
+ public string Url => Data . DownloadPage ;
239
+ }
240
+
241
+ public class GoFileIoUploadedFile
242
+ {
243
+ [ JsonPropertyName ( "createTime" ) ]
244
+ public int CreateTime { get ; set ; }
245
+
246
+ [ JsonPropertyName ( "downloadPage" ) ]
247
+ public string DownloadPage { get ; set ; }
248
+
249
+ [ JsonPropertyName ( "guestToken" ) ]
250
+ public string GuestToken { get ; set ; }
251
+
252
+ [ JsonPropertyName ( "id" ) ]
253
+ public string Id { get ; set ; }
254
+
255
+ [ JsonPropertyName ( "md5" ) ]
256
+ public string Md5 { get ; set ; }
257
+
258
+ [ JsonPropertyName ( "mimetype" ) ]
259
+ public string MimeType { get ; set ; }
260
+
261
+ [ JsonPropertyName ( "modTime" ) ]
262
+ public int ModTime { get ; set ; }
263
+
264
+ [ JsonPropertyName ( "name" ) ]
265
+ public string Name { get ; set ; }
266
+
267
+ [ JsonPropertyName ( "parentFolder" ) ]
268
+ public string ParentFolder { get ; set ; }
269
+
270
+ [ JsonPropertyName ( "parentFolderCode" ) ]
271
+ public string ParentFolderCode { get ; set ; }
272
+
273
+ [ JsonPropertyName ( "servers" ) ]
274
+ public List < string > Servers { get ; set ; }
275
+
276
+ [ JsonPropertyName ( "size" ) ]
277
+ public int Size { get ; set ; }
278
+
279
+ [ JsonPropertyName ( "type" ) ]
280
+ public string Type { get ; set ; }
281
+ }
0 commit comments