123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- unit pkgdownload;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, pkghandler;
-
- Type
- { TBasePackageDownloader }
- TBasePackageDownloader = Class(TPackageHandler)
- Protected
- // Needs overriding.
- Procedure FTPDownload(Const URL : String; Dest : TStream); Virtual;
- Procedure HTTPDownload(Const URL : String; Dest : TStream); Virtual;
- Procedure FileDownload(Const URL : String; Dest : TStream); Virtual;
- Public
- Procedure Download(Const URL,DestFileName : String);
- Procedure Download(Const URL : String; Dest : TStream);
- end;
- TBasePackageDownloaderClass = Class of TBasePackageDownloader;
- Var
- DownloaderClass : TBasePackageDownloaderClass;
- implementation
- uses pkgmessages,uriparser;
- { TBasePackageDownloader }
- procedure TBasePackageDownloader.FTPDownload(const URL: String; Dest: TStream);
- begin
- Error(SErrNoFTPDownload);
- end;
- procedure TBasePackageDownloader.HTTPDownload(const URL: String; Dest: TStream);
- begin
- Error(SErrNoHTTPDownload);
- end;
- procedure TBasePackageDownloader.FileDownload(const URL: String; Dest: TStream);
- Var
- URI : TURI;
- FN : String;
- F : TFileStream;
-
- begin
- URI:=ParseURI(URL);
- FN:=URI.Path+'/'+URI.Document;
- If Not FileExists(FN) then
- Error(SErrNoSuchFile,[FN]);
- F:=TFileStream.Create(FN,fmOpenRead);
- Try
- Dest.CopyFrom(F,0);
- Finally
- F.Free;
- end;
- end;
- procedure TBasePackageDownloader.Download(const URL, DestFileName: String);
- Var
- F : TFileStream;
- begin
- If FileExists(DestFileName) and BackupFiles then
- BackupFile(DestFileName);
- F:=TFileStream.Create(DestFileName,fmCreate);
- Try
- Download(URL,F);
- Finally
- F.Free;
- end;
- end;
- procedure TBasePackageDownloader.Download(const URL: String; Dest: TStream);
- Var
- URI : TURI;
- P : String;
-
- begin
- URI:=ParseURI(URL);
- P:=URI.Protocol;
- If CompareText(P,'ftp')=0 then
- FTPDownload(URL,Dest)
- else if CompareText(P,'http')=0 then
- HTTPDownload(URL,Dest)
- else if CompareText(P,'file')=0 then
- FileDownload(URL,Dest)
- else
- Error(SErrUnknownProtocol,[P]);
- end;
- initialization
- // Default value.
- DownloaderClass := TBasePackageDownloader;
- end.
|