pkgdownload.pp 3.1 KB

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