Quick.CloudStorage.pas 7.2 KB

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