-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVCL.TMS.myCloudDataRestClient.pas
More file actions
200 lines (184 loc) · 5.72 KB
/
VCL.TMS.myCloudDataRestClient.pas
File metadata and controls
200 lines (184 loc) · 5.72 KB
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
{ *************************************************************************** }
{ TMS VCL myCloudData RESTClient }
{ for Delphi & C++Builder }
{ }
{ written by TMS Software }
{ copyright © 2016 }
{ Email : info@tmssoftware.com }
{ Web : http://www.tmssoftware.com }
{ }
{ The source code is given as is. The author is not responsible }
{ for any possible damage done due to the use of this code. }
{ The component can be freely used in any application. The complete }
{ source code remains property of the author and may not be distributed, }
{ published, given or sold in any form as such. No parts of the source }
{ code can be included in any other component or application without }
{ written authorization of the author. }
{ *************************************************************************** }
unit VCL.TMS.myCloudDataRestClient;
interface
uses
System.Classes, SysUtils, AnsiStrings,
GIFImg, PNGImage, JPEG,
REST.TMS.myCloudDataRestClient,
REST.TMS.myCloudDataRestClient.Data,
REST.Authenticator.OAuth.WebForm.Win,
VCL.Controls, VCL.Forms, VCL.Dialogs, VCL.StdCtrls, VCL.ExtCtrls, VCL.Graphics;
type
TVCLmyCloudDataRESTClient = class(TmyCloudDataRESTClient)
public
procedure Authenticate; override;
end;
TmyCloudDataBlobClassHelper = class helper for TmyCloudDataBlob
private
function FindGraphicClass(const Buffer; const BufferSize: Int64; out GraphicClass: TGraphicClass): Boolean;
public
function TryGetAsGraphic(out AGraphic: TGraphic): Boolean;
end;
implementation
uses
REST.Utils;
{ VCLTMSMyCloudDataClient }
procedure TVCLmyCloudDataRESTClient.Authenticate;
var
LWebForm: Tfrm_OAuthWebForm;
begin
if (ClientID.Trim = '') OR (ClientSecret.Trim = '') then
begin
raise Exception.Create('Please make sure to set your application KEY and SECRET that you can obtain by registering at http://myclouddata.net');
end;
LWebForm := Tfrm_OAuthWebForm.Create(Owner);
try
LWebForm.OnAfterRedirect := OAuth2_AuthTokenRedirect;
LWebForm.ShowModalWithURL(GetLoginUrl);
finally
LWebForm.Release;
end;
end;
{ TmyCloudDataBlobClassHelper }
const
MinGraphicSize = 40;
function TmyCloudDataBlobClassHelper.FindGraphicClass(const Buffer; const BufferSize: Int64; out GraphicClass: TGraphicClass): Boolean;
var
LongWords: array [Byte] of LongWord absolute Buffer;
Words: array [Byte] of Word absolute Buffer;
begin
GraphicClass := nil;
Result := False;
if BufferSize < MinGraphicSize then
Exit;
case Words[0] of
$4D42:
GraphicClass := TBitmap;
$D8FF:
GraphicClass := TJPEGImage;
$4949:
if Words[1] = $002A then
GraphicClass := TWicImage; // i.e., TIFF
$4D4D:
if Words[1] = $2A00 then
GraphicClass := TWicImage; // i.e., TIFF
else
if Int64(Buffer) = $A1A0A0D474E5089 then
GraphicClass := TPNGImage
else if LongWords[0] = $9AC6CDD7 then
GraphicClass := TMetafile
else if (LongWords[0] = 1) and (LongWords[10] = $464D4520) then
GraphicClass := TMetafile
else if AnsiStrings.StrLComp(PAnsiChar(@Buffer), 'GIF', 3) = 0 then
GraphicClass := TGIFImage
else if Words[1] = 1 then
GraphicClass := TIcon;
end;
Result := (GraphicClass <> nil);
end;
function TmyCloudDataBlobClassHelper.TryGetAsGraphic(out AGraphic: TGraphic): Boolean;
var
Buffer: PByte;
LStream: TStream;
LGraphicClass: TGraphicClass;
LJPEGImage: TJPEGImage;
LBitmap: TBitmap;
LPNGImage: TPNGImage;
LGIFImage: TGIFImage;
begin
LStream := GetStream;
try
Buffer := TCustomMemoryStream(LStream).Memory;
Result := FindGraphicClass(Buffer^, LStream.Size, LGraphicClass);
if (Result) then
begin
if LGraphicClass = TJPEGImage then
begin
LJPEGImage := TJPEGImage.Create;
try
LJPEGImage.LoadFromStream(LStream);
AGraphic := LJPEGImage;
Exit;
except
on E: Exception do
begin
LJPEGImage.Free;
Result := False;
Exit;
end;
end;
end;
// TBitmap:
if LGraphicClass = TBitmap then
begin
LBitmap := TBitmap.Create;
try
LBitmap.LoadFromStream(LStream);
AGraphic := LBitmap;
Exit;
except
on E: Exception do
begin
LBitmap.Free;
Result := False;
Exit;
end;
end;
end;
// TPNGImage:
if LGraphicClass = TPNGImage then
begin
LPNGImage := TPNGImage.Create;
try
LPNGImage.LoadFromStream(LStream);
AGraphic := LPNGImage;
Exit;
except
on E: Exception do
begin
LPNGImage.Free;
Result := False;
Exit;
end;
end;
end;
// TGIFImage:
if LGraphicClass = TGIFImage then
begin
LGIFImage := TGIFImage.Create;
try
LGIFImage.LoadFromStream(LStream);
AGraphic := LGIFImage;
Exit;
except
on E: Exception do
begin
LGIFImage.Free;
Result := False;
Exit;
end;
end;
end;
end;
Result := False;
finally
LStream.Free;
end;
end;
end.