2
0

Quick.CloudStorage.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.CloudStorage
  4. Description : CloudStorage
  5. Author : Kike Pérez
  6. Version : 1.8
  7. Created : 14/10/2018
  8. Modified : 07/10/2019
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.CloudStorage;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. System.SysUtils,
  27. System.Generics.Collections,
  28. Data.Cloud.CloudAPI;
  29. type
  30. TCloudActionStatus = (stNone, stSearching, stRetrieving, stDone, stFailed);
  31. TCloudProtocol = (cpHTTP,cpHTTPS);
  32. TResponseInfo = record
  33. StatusCode : Integer;
  34. StatusMsg : string;
  35. procedure Get(aStatusCode : Integer; const aStatusMsg : string); overload;
  36. procedure Get(aCloudResponseInfo : TCloudResponseInfo); overload;
  37. end;
  38. TCloudItem = class
  39. private
  40. fName : string;
  41. fIsDir : Boolean;
  42. fSize : Int64;
  43. fDate : TDateTime;
  44. public
  45. property Name : string read fName write fName;
  46. property IsDir : Boolean read fIsDir write fIsDir;
  47. property Size : Int64 read fSize write fSize;
  48. property Date : TDateTime read fDate write fDate;
  49. end;
  50. TCloudItemList = TObjectList<TCloudItem>;
  51. TReadDirEvent = procedure(const aDir : string) of object;
  52. TGetListItemEvent = procedure(aItem : TCloudItem) of object;
  53. TChangeStatusEvent = procedure(aStatus : TCloudActionStatus) of object;
  54. ICloudStorage = interface
  55. ['{5F36CD88-405F-45C1-89E0-9114146CA8D9}']
  56. function GetName : string;
  57. function GetRootFolders : TStrings;
  58. procedure OpenDir(const aPath : string);
  59. function GetFile(const aSourcePath: string; out stream : TStream) : Boolean; overload;
  60. function GetFile(const aSourcePath, aTargetLocalFile : string) : Boolean; overload;
  61. function GetURL(const aPath : string) : string;
  62. end;
  63. TCloudPermissions = class
  64. private
  65. fCanList : Boolean;
  66. fCanRead : Boolean;
  67. fCanWrite : Boolean;
  68. fCanDelete : Boolean;
  69. public
  70. property CanList : Boolean read fCanList write fCanList;
  71. property CanRead : Boolean read fCanRead write fCanRead;
  72. property CanWrite : Boolean read fCanWrite write fCanWrite;
  73. property CanDelete : Boolean read fCanDelete write fCanDelete;
  74. end;
  75. TCloudStorageProvider = class(TInterfacedObject,ICloudStorage)
  76. private
  77. fName : string;
  78. fResponseInfo : TResponseInfo;
  79. fCurrentPath : string;
  80. fOnGetListItem : TGetListItemEvent;
  81. fOnBeginReadDir : TReadDirEvent;
  82. fOnRefresReadDir : TReadDirEvent;
  83. fOnEndReadDir : TReadDirEvent;
  84. fOnChangeStatus : TChangeStatusEvent;
  85. fStatus: TCloudActionStatus;
  86. fRootFolder : string;
  87. fTimeout : Integer;
  88. fSecure : Boolean;
  89. fPermissions : TCloudPermissions;
  90. procedure SetStatus(aStatus : TCloudActionStatus);
  91. protected
  92. fCancelOperation : Boolean;
  93. procedure SetSecure(aValue : Boolean); virtual;
  94. function GMT2DateTime(const gmtdate : string):TDateTime;
  95. public
  96. constructor Create; virtual;
  97. destructor Destroy; override;
  98. property Name : string read fName write fName;
  99. property ResponseInfo : TResponseInfo read fResponseInfo write fResponseInfo;
  100. property Timeout : Integer read fTimeout write fTimeout;
  101. property CurrentPath : string read fCurrentPath write fCurrentPath;
  102. property RootFolder : string read fRootFolder write fRootFolder;
  103. property OnBeginReadDir : TReadDirEvent read fOnBeginReadDir write fOnBeginReadDir;
  104. property OnRefreshReadDir : TReadDirEvent read fOnRefresReadDir write fOnRefresReadDir;
  105. property OnEndReadDir : TReadDirEvent read fOnEndReadDir write fOnEndReadDir;
  106. property OnGetListItem : TGetListItemEvent read fOnGetListItem write fOnGetListItem;
  107. property Status : TCloudActionStatus read fStatus write SetStatus;
  108. property Secure : Boolean read fSecure write SetSecure;
  109. property OnChangeStatus : TChangeStatusEvent read fOnChangeStatus write fOnChangeStatus;
  110. property Permissions : TCloudPermissions read fPermissions write fPermissions;
  111. class function GetStatusStr(aStatus : TCloudActionStatus) : string;
  112. function GetName : string;
  113. function GetRootFolders : TStrings; virtual; abstract;
  114. procedure OpenDir(const aPath : string); virtual; abstract;
  115. function GetFile(const aPath: string; out stream : TStream) : Boolean; overload; virtual; abstract;
  116. function GetFile(const aSourcePath, aTargetLocalFile : string) : Boolean; overload; virtual;
  117. function GetURL(const aPath : string) : string; virtual; abstract;
  118. end;
  119. implementation
  120. const
  121. CloudActionStatusStr : array of string = ['','Searching...','Retrieving...','Done','Failed'];
  122. constructor TCloudStorageProvider.Create;
  123. begin
  124. fCancelOperation := False;
  125. fPermissions := TCloudPermissions.Create;
  126. fTimeout := 30;
  127. fSecure := True;
  128. fPermissions.CanList := True;
  129. fPermissions.CanRead := True;
  130. fPermissions.CanWrite := True;
  131. fPermissions.CanDelete := True;
  132. end;
  133. destructor TCloudStorageProvider.Destroy;
  134. begin
  135. if Assigned(fPermissions) then fPermissions.Free;
  136. inherited;
  137. end;
  138. function TCloudStorageProvider.GetFile(const aSourcePath, aTargetLocalFile: string): Boolean;
  139. var
  140. stream : TStream;
  141. begin
  142. stream := TFileStream.Create(aTargetLocalFile,fmCreate);
  143. try
  144. Result := GetFile(aSourcePath,stream);
  145. finally
  146. stream.Free;
  147. end;
  148. end;
  149. function TCloudStorageProvider.GetName: string;
  150. begin
  151. Result := fName;
  152. end;
  153. class function TCloudStorageProvider.GetStatusStr(aStatus: TCloudActionStatus): string;
  154. begin
  155. Result := CloudActionStatusStr[Integer(aStatus)];
  156. end;
  157. function TCloudStorageProvider.GMT2DateTime(const gmtdate: string): TDateTime;
  158. function GetMonthDig(Value : string):Integer;
  159. const
  160. aMonth : array[1..12] of string = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  161. var
  162. idx : Integer;
  163. begin
  164. Result := 0;
  165. for idx := 1 to 12 do
  166. begin
  167. if CompareText(Value,aMonth[idx]) = 0 then
  168. begin
  169. Result := idx;
  170. Break;
  171. end;
  172. end;
  173. end;
  174. var
  175. i : Integer;
  176. Len : Integer;
  177. wDay, wMonth, wYear,
  178. wHour, wMinute, wSec : Word;
  179. begin
  180. //GMT Format: 'Mon, 12 Jan 2014 16:20:35 GMT'
  181. Result := 0;
  182. Len := 0;
  183. if gmtdate = '' then Exit;
  184. try
  185. for i := 0 to Length(gmtdate) do
  186. begin
  187. if gmtdate[i] in ['0'..'9'] then
  188. begin
  189. Len := i;
  190. Break;
  191. end;
  192. end;
  193. //Day
  194. wDay := StrToIntDef(Copy(gmtdate,Len,2),0);
  195. if wDay = 0 then Exit;
  196. Inc(Len,3);
  197. //Month
  198. wMonth := GetMonthDig(Copy(gmtdate,Len,3));
  199. if wMonth = 0 then Exit;
  200. Inc(Len,4);
  201. //Year
  202. wYear := StrToIntDef(Copy(gmtdate,Len,4),0);
  203. if wYear = 0 then Exit;
  204. Inc(Len,5);
  205. //Hour
  206. wHour := StrToIntDef(Copy(gmtdate,Len,2),99);
  207. if wHour = 99 then Exit;
  208. Inc(Len,3);
  209. //Min
  210. wMinute := StrToIntDef(Copy(gmtdate,Len,2),99);
  211. if wMinute = 99 then Exit;
  212. Inc(Len,3);
  213. //Sec
  214. wSec := StrToIntDef(Copy(gmtdate,Len,2),99);
  215. if wSec = 99 then Exit;
  216. Result := EncodeDate(wYear,wMonth,wDay) + EncodeTime(wHour,wMinute,wSec,0);
  217. except
  218. Result := 0;
  219. end;
  220. end;
  221. procedure TCloudStorageProvider.SetSecure(aValue: Boolean);
  222. begin
  223. fSecure := aValue;
  224. end;
  225. procedure TCloudStorageProvider.SetStatus(aStatus: TCloudActionStatus);
  226. begin
  227. fStatus := aStatus;
  228. if Assigned(fOnChangeStatus) then fOnChangeStatus(aStatus);
  229. end;
  230. { TResponseInfo }
  231. procedure TResponseInfo.Get(aStatusCode: Integer; const aStatusMsg: string);
  232. begin
  233. Self.StatusCode := aStatusCode;
  234. Self.StatusMsg := aStatusMsg;
  235. end;
  236. procedure TResponseInfo.Get(aCloudResponseInfo : TCloudResponseInfo);
  237. begin
  238. Self.StatusCode := aCloudResponseInfo.StatusCode;
  239. Self.StatusMsg := aCloudResponseInfo.StatusMessage;
  240. end;
  241. end.