-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudownload.pas
262 lines (234 loc) · 6.61 KB
/
udownload.pas
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{ Source: https://forum.lazarus.freepascal.org/index.php/topic,52773.msg389901.html#msg389901 }
unit uDownload;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, fphttpclient, opensslsockets;
type
{ TDownloadStream }
TOnWriteStream = procedure(Sender: TObject; APos: Int64) of object;
TDownloadStream = class(TStream)
private
FOnWriteStream: TOnWriteStream;
FStream: TStream;
public
constructor Create(AStream: TStream);
destructor Destroy; override;
function Read(var Buffer; Count: LongInt): LongInt; override;
function Write(const Buffer; Count: LongInt): LongInt; override;
function Seek(Offset: LongInt; Origin: Word): LongInt; override;
procedure DoProgress;
published
property OnWriteStream: TOnWriteStream read FOnWriteStream write FOnWriteStream;
end;
{TDownload}
TOnDownloadProgress = procedure(Sender: TObject; AFrom, ATo: String; APos, ASize, AElapsed, ARemaining, ASpeed: LongInt) of object;
TOnDownloadError = procedure(Sender: TObject; const AErrMsg: String = '') of object;
TOnDownloadCompleted = TNotifyEvent;
TDownload = class(TThread)
private
FMS: TMemoryStream;
FFPHTTPClient: TFPHTTPClient;
FURL: String;
FLocalFile: String;
FRemaining: Integer;
FSpeed: Integer;
FStartTime: QWord;
FElapsed: QWord;
FTick: Qword;
FPos: Int64;
FSize: Int64;
FErrMsg: String;
FOnDownloadProgress: TOnDownloadProgress;
FOnDownloadError: TOnDownloadError;
FOnDownloadCompleted: TOnDownloadCompleted;
procedure GetContentLength;
function FixProtocol(const AURL: String): String;
procedure DoOnDataReceived(Sender: TObject; const ContentLength, {%H-}CurrentPos: int64);
procedure DoOnWriteStream(Sender: TObject; APos: Int64);
procedure DoOnDownloadProgress;
procedure DoOnDownloadError;
procedure DoOnDownloadCompleted;
protected
procedure Execute; override;
public
constructor Create;
destructor Destroy; override;
procedure DownloadFile(const AURL, ALocalFile: String);
procedure CancelDownoad;
public
property OnDownloadProgress: TOnDownloadProgress read FOnDownloadProgress write FOnDownloadProgress;
property OnDownloadError: TOnDownloadError read FOnDownloadError write FOnDownloadError;
property OnDownloadCompleted: TOnDownloadCompleted read FOnDownloadCompleted write FOnDownloadCompleted;
end;
implementation
{ TDownloadStream }
constructor TDownloadStream.Create(AStream: TStream);
begin
inherited Create;
FStream := AStream;
FStream.Position := 0;
end;
destructor TDownloadStream.Destroy;
begin
FStream.Free;
inherited Destroy;
end;
function TDownloadStream.Read(var Buffer; Count: LongInt): LongInt;
begin
Result := FStream.Read(Buffer, Count);
end;
function TDownloadStream.Write(const Buffer; Count: LongInt): LongInt;
begin
Result := FStream.Write(Buffer, Count);
DoProgress;
end;
function TDownloadStream.Seek(Offset: LongInt; Origin: Word): LongInt;
begin
Result := FStream.Seek(Offset, Origin);
end;
procedure TDownloadStream.DoProgress;
begin
if Assigned(FOnWriteStream) then
FOnWriteStream(Self, Self.Position);
end;
{TDownload}
constructor TDownload.Create;
begin
inherited Create(True);
FreeOnTerminate := True;
FMS := TMemoryStream.Create;
FFPHTTPClient := TFPHTTPClient.Create(nil);
end;
destructor TDownload.Destroy;
begin
FFPHTTPClient.Free;
FMS.Free;
inherited Destroy;
end;
procedure TDownload.DownloadFile(const AURL, ALocalFile: String);
begin
FURL := FixProtocol(AURL);
FLocalFile := ALocalFile;
Self.Start;
end;
procedure TDownload.CancelDownoad;
begin
if Assigned(FFPHTTPClient) then
FFPHTTPClient.Terminate;
end;
procedure TDownload.DoOnDataReceived(Sender: TObject; const ContentLength,
CurrentPos: int64);
begin
if ContentLength > 0 then
Abort;
end;
procedure TDownload.DoOnWriteStream(Sender: TObject; APos: Int64);
begin
FElapsed := GetTickCount64 - FStartTime;
if FElapsed < 1000 then
Exit;
FElapsed := FElapsed div 1000;
FPos := APos;
FSpeed := Round(FPos/FElapsed);
if FSpeed > 0 then
FRemaining := Round((FSize - FPos)/FSpeed);
if FElapsed >= FTick + 1 then
begin
FTick := FElapsed;
Synchronize(@DoOnDownloadProgress);
end;
end;
procedure TDownload.DoOnDownloadProgress;
begin
if Assigned(FOnDownloadProgress) then
FOnDownloadProgress(Self, FURL, FLocalFile, FPos, FSize, FElapsed, FRemaining, FSpeed);
end;
procedure TDownload.DoOnDownloadError;
begin
if Assigned(FOnDownloadError) then
FOnDownloadError(Self, FErrMsg);
end;
procedure TDownload.DoOnDownloadCompleted;
begin
if Assigned(FOnDownloadCompleted) then
FOnDownloadCompleted(Self);
end;
procedure TDownload.GetContentLength;
var
SS: TStringStream;
HttpClient: TFPHTTPClient;
URL: String;
begin
FSize := 0;
SS := TStringStream.Create('');
try
URL := FixProtocol(FURL);
HttpClient := TFPHTTPClient.Create(nil);
try
HttpClient.OnDataReceived := @DoOnDataReceived;
HttpClient.AllowRedirect := True;
HttpClient.ResponseHeaders.NameValueSeparator := ':';
try
HttpClient.HTTPMethod('GET', URL, SS, []);
except
end;
if HttpClient.ResponseStatusCode = 200 then
FSize := StrToIntDef(HttpClient.ResponseHeaders.Values['CONTENT-LENGTH'], 0)
finally
HttpClient.Free;
end;
finally
SS.Free
end;
end;
function TDownload.FixProtocol(const AURL: String): String;
begin
Result := AURL;
if (Pos('http://', Result) = 0) and (Pos('https://', Result) = 0) then
Result := 'https://' + Result;
end;
procedure TDownload.Execute;
var
DS: TDownloadStream;
Flags: Word;
begin
FStartTime := GetTickCount64;
GetContentLength;
Flags := fmOpenWrite;
if not FileExists(FLocalFile) then
begin
FPos := 0;
Flags := Flags or fmCreate;
end
else
FPos := FileUtil.FileSize(FLocalFile);
DS := TDownloadStream.Create(TFileStream.Create(FLocalFile, Flags));
try
DS.FOnWriteStream := @DoOnWriteStream;
try
if (FPos > 0) and (FPos < FSize) then
begin
DS.Position := FPos;
FFPHTTPClient.AddHeader('Range', 'bytes=' + IntToStr(FPos) + '-' + IntToStr(FSize));
end;
FFPHTTPClient.AddHeader('User-Agent','Mozilla/5.0 (compatible; fpweb)');
FFPHTTPClient.AllowRedirect := True;
FFPHTTPClient.HTTPMethod('GET', FURL, DS, [200, 206]);
if not FFPHTTPClient.Terminated then
begin
Synchronize(@DoOnDownloadProgress);
Synchronize(@DoOnDownloadCompleted);
end;
except
on E: Exception do
begin
FErrMsg := E.Message;
Synchronize(@DoOnDownloadError);
end;
end;
finally
DS.Free
end;
end;
end.