pkgdownload.pp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. unit pkgdownload;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, pkghandler;
  6. Type
  7. { TBasePackageDownloader }
  8. TBasePackageDownloader = Class(TPackageHandler)
  9. Protected
  10. // Needs overriding.
  11. Procedure FTPDownload(Const URL : String; Dest : TStream); Virtual;
  12. Procedure HTTPDownload(Const URL : String; Dest : TStream); Virtual;
  13. Procedure FileDownload(Const URL : String; Dest : TStream); Virtual;
  14. Public
  15. Procedure Download(Const URL,DestFileName : String);
  16. Procedure Download(Const URL : String; Dest : TStream);
  17. end;
  18. TBasePackageDownloaderClass = Class of TBasePackageDownloader;
  19. Var
  20. DownloaderClass : TBasePackageDownloaderClass;
  21. implementation
  22. uses pkgmessages,uriparser;
  23. { TBasePackageDownloader }
  24. procedure TBasePackageDownloader.FTPDownload(const URL: String; Dest: TStream);
  25. begin
  26. Error(SErrNoFTPDownload);
  27. end;
  28. procedure TBasePackageDownloader.HTTPDownload(const URL: String; Dest: TStream);
  29. begin
  30. Error(SErrNoHTTPDownload);
  31. end;
  32. procedure TBasePackageDownloader.FileDownload(const URL: String; Dest: TStream);
  33. Var
  34. URI : TURI;
  35. FN : String;
  36. F : TFileStream;
  37. begin
  38. URI:=ParseURI(URL);
  39. FN:=URI.Path+'/'+URI.Document;
  40. If Not FileExists(FN) then
  41. Error(SErrNoSuchFile,[FN]);
  42. F:=TFileStream.Create(FN,fmOpenRead);
  43. Try
  44. Dest.CopyFrom(F,0);
  45. Finally
  46. F.Free;
  47. end;
  48. end;
  49. procedure TBasePackageDownloader.Download(const URL, DestFileName: String);
  50. Var
  51. F : TFileStream;
  52. begin
  53. If FileExists(DestFileName) and BackupFiles then
  54. BackupFile(DestFileName);
  55. F:=TFileStream.Create(DestFileName,fmCreate);
  56. Try
  57. Download(URL,F);
  58. Finally
  59. F.Free;
  60. end;
  61. end;
  62. procedure TBasePackageDownloader.Download(const URL: String; Dest: TStream);
  63. Var
  64. URI : TURI;
  65. P : String;
  66. begin
  67. URI:=ParseURI(URL);
  68. P:=URI.Protocol;
  69. If CompareText(P,'ftp')=0 then
  70. FTPDownload(URL,Dest)
  71. else if CompareText(P,'http')=0 then
  72. HTTPDownload(URL,Dest)
  73. else if CompareText(P,'file')=0 then
  74. FileDownload(URL,Dest)
  75. else
  76. Error(SErrUnknownProtocol,[P]);
  77. end;
  78. initialization
  79. // Default value.
  80. DownloaderClass := TBasePackageDownloader;
  81. end.