-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImgBB.API.pas
390 lines (356 loc) · 12.6 KB
/
ImgBB.API.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
unit ImgBB.API;
interface
uses
System.Classes, System.SysUtils, System.Net.HttpClient, System.Net.URLClient,
System.Net.Mime;
type
EImgBBWrapper = class(Exception);
{$IF CompilerVersion < 34.0}
TSendDataCallback = reference to procedure(const Sender: TObject; AContentLength: Int64; AWriteCount: Int64; var AAbort: Boolean);
{$ENDIF}
TImgBBUploadResponse = class
private
type
TImgBBError = class
private
FCode: Integer;
FMessage: string;
public
property Code: Integer read FCode write FCode;
property Message: string read FMessage write FMessage;
end;
TImgBBLink = class
private
FExtension: string;
FFilename: string;
FMime: string;
FName: string;
FUrl: string;
public
property Extension: string read FExtension write FExtension;
property Filename: string read FFilename write FFilename;
property Mime: string read FMime write FMime;
property Name: string read FName write FName;
property Url: string read FUrl write FUrl;
end;
TImgBBData = class
private
FDelete_url: string;
FDisplay_url: string;
FExpiration: Int64;
FId: string;
FImage: TImgBBLink;
FMedium: TImgBBLink;
FSize: Int64;
FThumb: TImgBBLink;
FTime: Int64;
FTitle: string;
FUrl: string;
FUrl_viewer: string;
public
property DeleteUrl: string read FDelete_url write FDelete_url;
property DisplayUrl: string read FDisplay_url write FDisplay_url;
property Expiration: Int64 read FExpiration write FExpiration;
property Id: string read FId write FId;
property Image: TImgBBLink read FImage;
property Medium: TImgBBLink read FMedium;
property Size: Int64 read FSize write FSize;
property Thumb: TImgBBLink read FThumb;
property Time: Int64 read FTime write FTime;
property Title: string read FTitle write FTitle;
property Url: string read FUrl write FUrl;
property UrlViewer: string read FUrl_viewer write FUrl_viewer;
destructor Destroy; override;
end;
private
FData: TImgBBData;
FStatus: Integer;
FSuccess: Boolean;
FError: TImgBBError;
FStatusCode: Integer;
FStatusTxt: string;
public
property Data: TImgBBData read FData;
property Status: Integer read FStatus write FStatus;
property Success: Boolean read FSuccess write FSuccess;
//
property Error: TImgBBError read FError;
property StatusCode: Integer read FStatusCode write FStatusCode;
property StatusTxt: string read FStatusTxt write FStatusTxt;
destructor Destroy; override;
end;
IImgBB = interface
['{1C9FA631-5F45-4853-88CA-47525DB9E57C}']
/// <summary>
/// Image Upload from file
/// </summary>
function Upload(const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): TImgBBUploadResponse; overload;
/// <summary>
/// Image Upload from file
/// </summary>
function UploadGetUrl(const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): string; overload;
/// <summary>
/// Image Upload from stream
/// </summary>
function Upload(const Stream: TStream; const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): TImgBBUploadResponse; overload;
/// <summary>
/// Image Upload from stream
/// </summary>
function UploadGetUrl(const Stream: TStream; const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): string; overload;
/// <summary>
/// Image Upload from url
/// </summary>
function Upload(const URI: TURI; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): TImgBBUploadResponse; overload;
/// <summary>
/// Image Upload from url
/// </summary>
function UploadGetUrl(const URI: TURI; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): string; overload;
/// <summary>
/// Image Upload from base64
/// </summary>
function Upload(const Base64: TStringList; const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): TImgBBUploadResponse; overload;
/// <summary>
/// Image Upload from base64
/// </summary>
function UploadGetUrl(const Base64: TStringList; const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): string; overload;
end;
TImgBB = class(TInterfacedObject, IImgBB)
const
UrlUpload = 'https://api.imgbb.com/1/upload';
private
FKey: string;
FAPIUrl: string;
function Upload(Body: TMultipartFormData; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): TImgBBUploadResponse; overload;
public
/// <summary>
/// Image Upload from file
/// </summary>
function Upload(const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): TImgBBUploadResponse; overload;
/// <summary>
/// Image Upload from file
/// </summary>
function UploadGetUrl(const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): string; overload;
/// <summary>
/// Image Upload from stream
/// </summary>
function Upload(const Stream: TStream; const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): TImgBBUploadResponse; overload;
/// <summary>
/// Image Upload from stream
/// </summary>
function UploadGetUrl(const Stream: TStream; const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): string; overload;
/// <summary>
/// Image Upload from url
/// </summary>
function Upload(const URI: TURI; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): TImgBBUploadResponse; overload;
/// <summary>
/// Image Upload from url
/// </summary>
function UploadGetUrl(const URI: TURI; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): string; overload;
/// <summary>
/// Image Upload from base64
/// </summary>
function Upload(const Base64: TStringList; const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): TImgBBUploadResponse; overload;
/// <summary>
/// Image Upload from base64
/// </summary>
function UploadGetUrl(const Base64: TStringList; const FileName: string; CallBack: TSendDataCallback = nil; const Name: string = ''; Expiration: Integer = 60): string; overload;
/// <summary>
/// The API key
/// </summary>
property Key: string read FKey write FKey;
/// <summary>
/// Create API
/// </summary>
/// <param name="AKey: string">The API key. Get key api from api.imgbb.com</param>
/// <param name="AAPIUrl: string">API url - default UrlUpload const</param>
constructor Create(const AKey: string; const AAPIUrl: string = UrlUpload);
class function Instance(const AKey: string; const AAPIUrl: string = UrlUpload): IImgBB;
end;
/// <summary>
/// Create IImgBB directly
/// </summary>
function ImgBBAPI(const AKey: string; const AAPIUrl: string = TImgBB.UrlUpload): IImgBB;
implementation
uses
REST.Json;
function ImgBBAPI(const AKey: string; const AAPIUrl: string): IImgBB;
begin
Result := TImgBB.Instance(AKey, AAPIUrl);
end;
{ TImgBB }
constructor TImgBB.Create(const AKey: string; const AAPIUrl: string);
begin
inherited Create;
FKey := AKey;
FAPIUrl := AAPIUrl;
end;
function TImgBB.Upload(Body: TMultipartFormData; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): TImgBBUploadResponse;
var
URI: TURI;
Client: THTTPClient;
ResponseStream: TStringStream;
Success: Boolean;
begin
Client := THTTPClient.Create;
ResponseStream := TStringStream.Create;
try
{$IF CompilerVersion >= 34.0}
Client.SendDataCallback := CallBack;
{$ENDIF}
// Uri construct
URI := TURI.Create(UrlUpload);
// Params
URI.AddParameter('key', FKey);
if not Name.IsEmpty then
URI.AddParameter('name', Name);
if Expiration > 60 then
URI.AddParameter('expiration', Expiration.ToString);
// Execute
Success := Client.Post(URI.ToString, Body, ResponseStream).StatusCode = 200;
// Parse
if ResponseStream.Size > 0 then
begin
ResponseStream.Position := 0;
Result := TJson.JsonToObject<TImgBBUploadResponse>(ResponseStream.DataString);
if not Assigned(Result) then
raise EImgBBWrapper.Create('Json is empty');
if not Success then
try
if Assigned(Result.Error) then
raise EImgBBWrapper.Create('ImgBB error: ' + Result.Error.Message + ' (' + Result.Error.Code.ToString + ')')
else
raise EImgBBWrapper.Create('Unknown error');
finally
Result.Free;
end;
end
else
raise EImgBBWrapper.Create('Response is empty');
finally
ResponseStream.Free;
Client.Free;
end;
end;
function TImgBB.Upload(const Stream: TStream; const FileName: string; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): TImgBBUploadResponse;
var
Body: TMultipartFormData;
begin
Body := TMultipartFormData.Create;
try
// Body - file
Body.AddStream('image', Stream, FileName);
Result := Upload(Body, CallBack, Name, Expiration);
finally
Body.Free;
end;
end;
function TImgBB.UploadGetUrl(const URI: TURI; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): string;
var
Response: TImgBBUploadResponse;
begin
Response := Upload(URI, CallBack, Name, Expiration);
try
if Assigned(Response.Data) then
Result := Response.Data.Url;
finally
Response.Free;
end;
end;
function TImgBB.Upload(const URI: TURI; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): TImgBBUploadResponse;
var
Body: TMultipartFormData;
begin
Body := TMultipartFormData.Create;
try
// Body - uri
Body.AddField('image', URI.ToString);
Result := Upload(Body, CallBack, Name, Expiration);
finally
Body.Free;
end;
end;
function TImgBB.UploadGetUrl(const Base64: TStringList; const FileName: string; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): string;
var
Response: TImgBBUploadResponse;
begin
Response := Upload(Base64, FileName, CallBack, Name, Expiration);
try
if Assigned(Response.Data) then
Result := Response.Data.Url;
finally
Response.Free;
end;
end;
class function TImgBB.Instance(const AKey: string; const AAPIUrl: string = UrlUpload): IImgBB;
begin
Result := TImgBB.Create(AKey, AAPIUrl);
end;
function TImgBB.Upload(const Base64: TStringList; const FileName: string; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): TImgBBUploadResponse;
var
Body: TMultipartFormData;
begin
Body := TMultipartFormData.Create;
try
// Body - base64
Body.AddField('image', Base64.Text);
Result := Upload(Body, CallBack, Name, Expiration);
finally
Body.Free;
end;
end;
function TImgBB.Upload(const FileName: string; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): TImgBBUploadResponse;
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create(FileName, fmShareDenyWrite);
try
Result := Upload(FileStream, FileName, CallBack, Name, Expiration);
finally
FileStream.Free;
end;
end;
function TImgBB.UploadGetUrl(const Stream: TStream; const FileName: string; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): string;
var
Response: TImgBBUploadResponse;
begin
Response := Upload(Stream, FileName, CallBack, Name, Expiration);
try
if Assigned(Response.Data) then
Result := Response.Data.Url;
finally
Response.Free;
end;
end;
function TImgBB.UploadGetUrl(const FileName: string; CallBack: TSendDataCallback; const Name: string; Expiration: Integer): string;
var
Response: TImgBBUploadResponse;
begin
Response := Upload(FileName, CallBack, Name, Expiration);
try
if Assigned(Response.Data) then
Result := Response.Data.Url;
finally
Response.Free;
end;
end;
{ TImgBBData }
destructor TImgBBUploadResponse.TImgBBData.Destroy;
begin
if Assigned(FImage) then
FImage.Free;
if Assigned(FMedium) then
FMedium.Free;
if Assigned(FThumb) then
FThumb.Free;
inherited;
end;
{ TImgBBUploadResponse }
destructor TImgBBUploadResponse.Destroy;
begin
if Assigned(FData) then
FData.Free;
if Assigned(FError) then
FError.Free;
inherited;
end;
end.