pkgdownload.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. unit pkgDownload;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, pkghandler;
  6. Type
  7. { TBaseDownloader }
  8. TBaseDownloader = Class(TComponent)
  9. Private
  10. FBackupFile : Boolean;
  11. Protected
  12. // Needs overriding.
  13. Procedure FTPDownload(Const URL : String; Dest : TStream); Virtual;
  14. Procedure HTTPDownload(Const URL : String; Dest : TStream); Virtual;
  15. Procedure FileDownload(Const URL : String; Dest : TStream); Virtual;
  16. Public
  17. Procedure Download(Const URL,DestFileName : String);
  18. Procedure Download(Const URL : String; Dest : TStream);
  19. Property BackupFiles : Boolean Read FBackupFile Write FBackupFile;
  20. end;
  21. TBaseDownloaderClass = Class of TBaseDownloader;
  22. { TDownloadPackage }
  23. TDownloadPackage = Class(TPackagehandler)
  24. Public
  25. Function Execute(const Args:TActionArgs):boolean;override;
  26. end;
  27. procedure RegisterDownloader(const AName:string;Downloaderclass:TBaseDownloaderClass);
  28. function GetDownloader(const AName:string):TBaseDownloaderClass;
  29. procedure DownloadFile(const RemoteFile,LocalFile:String);
  30. implementation
  31. uses
  32. contnrs,
  33. uriparser,
  34. pkgglobals,
  35. pkgoptions,
  36. pkgmessages;
  37. var
  38. DownloaderList : TFPHashList;
  39. procedure RegisterDownloader(const AName:string;Downloaderclass:TBaseDownloaderClass);
  40. begin
  41. if DownloaderList.Find(AName)<>nil then
  42. begin
  43. Error('Downloader already registered');
  44. exit;
  45. end;
  46. DownloaderList.Add(AName,Downloaderclass);
  47. end;
  48. function GetDownloader(const AName:string):TBaseDownloaderClass;
  49. begin
  50. result:=TBaseDownloaderClass(DownloaderList.Find(AName));
  51. if result=nil then
  52. Error('Downloader %s not supported',[AName]);
  53. end;
  54. procedure DownloadFile(const RemoteFile,LocalFile:String);
  55. var
  56. DownloaderClass : TBaseDownloaderClass;
  57. begin
  58. DownloaderClass:=GetDownloader(GlobalOptions.Downloader);
  59. with DownloaderClass.Create(nil) do
  60. try
  61. Download(RemoteFile,LocalFile);
  62. finally
  63. Free;
  64. end;
  65. end;
  66. { TBaseDownloader }
  67. procedure TBaseDownloader.FTPDownload(const URL: String; Dest: TStream);
  68. begin
  69. Error(SErrNoFTPDownload);
  70. end;
  71. procedure TBaseDownloader.HTTPDownload(const URL: String; Dest: TStream);
  72. begin
  73. Error(SErrNoHTTPDownload);
  74. end;
  75. procedure TBaseDownloader.FileDownload(const URL: String; Dest: TStream);
  76. Var
  77. FN : String;
  78. F : TFileStream;
  79. begin
  80. URIToFilename(URL,FN);
  81. If Not FileExists(FN) then
  82. Error(SErrNoSuchFile,[FN]);
  83. F:=TFileStream.Create(FN,fmOpenRead);
  84. Try
  85. Dest.CopyFrom(F,0);
  86. Finally
  87. F.Free;
  88. end;
  89. end;
  90. procedure TBaseDownloader.Download(const URL, DestFileName: String);
  91. Var
  92. F : TFileStream;
  93. begin
  94. If FileExists(DestFileName) and BackupFiles then
  95. BackupFile(DestFileName);
  96. try
  97. F:=TFileStream.Create(DestFileName,fmCreate);
  98. try
  99. Download(URL,F);
  100. finally
  101. F.Free;
  102. end;
  103. except
  104. DeleteFile(DestFileName);
  105. raise;
  106. end;
  107. end;
  108. procedure TBaseDownloader.Download(const URL: String; Dest: TStream);
  109. Var
  110. URI : TURI;
  111. P : String;
  112. begin
  113. URI:=ParseURI(URL);
  114. P:=URI.Protocol;
  115. If CompareText(P,'ftp')=0 then
  116. FTPDownload(URL,Dest)
  117. else if CompareText(P,'http')=0 then
  118. HTTPDownload(URL,Dest)
  119. else if CompareText(P,'file')=0 then
  120. FileDownload(URL,Dest)
  121. else
  122. Error(SErrUnknownProtocol,[P]);
  123. end;
  124. { TDownloadPackage }
  125. function TDownloadPackage.Execute(const Args:TActionArgs):boolean;
  126. var
  127. DownloaderClass : TBaseDownloaderClass;
  128. begin
  129. DownloaderClass:=GetDownloader(GlobalOptions.Downloader);
  130. with DownloaderClass.Create(nil) do
  131. try
  132. Log(vCommands,SLogDownloading,[PackageRemoteArchive,PackageLocalArchive]);
  133. Download(PackageRemoteArchive,PackageLocalArchive);
  134. finally
  135. Free;
  136. end;
  137. end;
  138. initialization
  139. DownloaderList:=TFPHashList.Create;
  140. RegisterDownloader('base',TBaseDownloader);
  141. RegisterPkgHandler('downloadpackage',TDownloadPackage);
  142. finalization
  143. FreeAndNil(DownloaderList);
  144. end.