1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
unit pkgDownload;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, pkghandler;
Type
{ TBaseDownloader }
TBaseDownloader = Class(TComponent)
Private
FBackupFile : Boolean;
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);
Property BackupFiles : Boolean Read FBackupFile Write FBackupFile;
end;
TBaseDownloaderClass = Class of TBaseDownloader;
{ TDownloadPackage }
TDownloadPackage = Class(TPackagehandler)
Public
Procedure Execute;override;
end;
procedure RegisterDownloader(const AName:string;Downloaderclass:TBaseDownloaderClass);
function GetDownloader(const AName:string):TBaseDownloaderClass;
procedure DownloadFile(const RemoteFile,LocalFile:String);
implementation
uses
contnrs,
uriparser,
fprepos,
pkgglobals,
pkgoptions,
pkgmessages,
pkgrepos;
var
DownloaderList : TFPHashList;
procedure RegisterDownloader(const AName:string;Downloaderclass:TBaseDownloaderClass);
begin
if DownloaderList.Find(AName)<>nil then
begin
Error('Downloader already registered');
exit;
end;
DownloaderList.Add(AName,Downloaderclass);
end;
function GetDownloader(const AName:string):TBaseDownloaderClass;
begin
result:=TBaseDownloaderClass(DownloaderList.Find(AName));
if result=nil then
Error('Downloader %s not supported',[AName]);
end;
procedure DownloadFile(const RemoteFile,LocalFile:String);
var
DownloaderClass : TBaseDownloaderClass;
begin
DownloaderClass:=GetDownloader(GlobalOptions.Downloader);
with DownloaderClass.Create(nil) do
try
Download(RemoteFile,LocalFile);
finally
Free;
end;
end;
{ TBaseDownloader }
procedure TBaseDownloader.FTPDownload(const URL: String; Dest: TStream);
begin
Error(SErrNoFTPDownload);
end;
procedure TBaseDownloader.HTTPDownload(const URL: String; Dest: TStream);
begin
Error(SErrNoHTTPDownload);
end;
procedure TBaseDownloader.FileDownload(const URL: String; Dest: TStream);
Var
FN : String;
F : TFileStream;
begin
URIToFilename(URL,FN);
If Not FileExists(FN) then
Error(SErrNoSuchFile,[FN]);
F:=TFileStream.Create(FN,fmOpenRead);
Try
Dest.CopyFrom(F,0);
Finally
F.Free;
end;
end;
procedure TBaseDownloader.Download(const URL, DestFileName: String);
Var
F : TFileStream;
begin
If FileExists(DestFileName) and BackupFiles then
BackupFile(DestFileName);
try
F:=TFileStream.Create(DestFileName,fmCreate);
try
Download(URL,F);
finally
F.Free;
end;
except
DeleteFile(DestFileName);
raise;
end;
end;
procedure TBaseDownloader.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;
{ TDownloadPackage }
procedure TDownloadPackage.Execute;
var
DownloaderClass : TBaseDownloaderClass;
P : TFPPackage;
begin
P:=AvailableRepository.PackageByName(PackageName);
DownloaderClass:=GetDownloader(GlobalOptions.Downloader);
with DownloaderClass.Create(nil) do
try
Log(vlCommands,SLogDownloading,[PackageRemoteArchive(P),PackageLocalArchive(P)]);
pkgglobals.log(vlProgres,SProgrDownloadPackage,[P.Name, P.Version.AsString]);
Download(PackageRemoteArchive(P),PackageLocalArchive(P));
finally
Free;
end;
end;
initialization
DownloaderList:=TFPHashList.Create;
RegisterDownloader('base',TBaseDownloader);
RegisterPkgHandler('downloadpackage',TDownloadPackage);
finalization
FreeAndNil(DownloaderList);
end.
|