pkgdownload.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. Var
  28. DownloaderClass : TBaseDownloaderClass;
  29. procedure DownloadFile(const RemoteFile,LocalFile:String);
  30. implementation
  31. uses
  32. uriparser,
  33. pkgglobals,
  34. pkgmessages;
  35. procedure DownloadFile(const RemoteFile,LocalFile:String);
  36. begin
  37. with DownloaderClass.Create(nil) do
  38. try
  39. Download(RemoteFile,LocalFile);
  40. finally
  41. Free;
  42. end;
  43. end;
  44. { TBaseDownloader }
  45. procedure TBaseDownloader.FTPDownload(const URL: String; Dest: TStream);
  46. begin
  47. Error(SErrNoFTPDownload);
  48. end;
  49. procedure TBaseDownloader.HTTPDownload(const URL: String; Dest: TStream);
  50. begin
  51. Error(SErrNoHTTPDownload);
  52. end;
  53. procedure TBaseDownloader.FileDownload(const URL: String; Dest: TStream);
  54. Var
  55. FN : String;
  56. F : TFileStream;
  57. begin
  58. URIToFilename(URL,FN);
  59. If Not FileExists(FN) then
  60. Error(SErrNoSuchFile,[FN]);
  61. F:=TFileStream.Create(FN,fmOpenRead);
  62. Try
  63. Dest.CopyFrom(F,0);
  64. Finally
  65. F.Free;
  66. end;
  67. end;
  68. procedure TBaseDownloader.Download(const URL, DestFileName: String);
  69. Var
  70. F : TFileStream;
  71. begin
  72. If FileExists(DestFileName) and BackupFiles then
  73. BackupFile(DestFileName);
  74. try
  75. F:=TFileStream.Create(DestFileName,fmCreate);
  76. try
  77. Download(URL,F);
  78. finally
  79. F.Free;
  80. end;
  81. except
  82. DeleteFile(DestFileName);
  83. raise;
  84. end;
  85. end;
  86. procedure TBaseDownloader.Download(const URL: String; Dest: TStream);
  87. Var
  88. URI : TURI;
  89. P : String;
  90. begin
  91. URI:=ParseURI(URL);
  92. P:=URI.Protocol;
  93. If CompareText(P,'ftp')=0 then
  94. FTPDownload(URL,Dest)
  95. else if CompareText(P,'http')=0 then
  96. HTTPDownload(URL,Dest)
  97. else if CompareText(P,'file')=0 then
  98. FileDownload(URL,Dest)
  99. else
  100. Error(SErrUnknownProtocol,[P]);
  101. end;
  102. { TDownloadPackage }
  103. function TDownloadPackage.Execute(const Args:TActionArgs):boolean;
  104. begin
  105. with DownloaderClass.Create(nil) do
  106. try
  107. Log(vCommands,SLogDownloading,[PackageRemoteArchive,PackageLocalArchive]);
  108. Download(PackageRemoteArchive,PackageLocalArchive);
  109. finally
  110. Free;
  111. end;
  112. end;
  113. initialization
  114. // Default value.
  115. DownloaderClass := TBaseDownloader;
  116. RegisterPkgHandler('downloadpackage',TDownloadPackage);
  117. end.