Repository navigation
Expand file tree
/
Copy pathLightCore.Download.Thread.pas
More file actions
136 lines (104 loc) · 4.92 KB
/
Copy pathLightCore.Download.Thread.pas
File metadata and controls
136 lines (104 loc) · 4.92 KB
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
UNIT LightCore.Download.Thread;
{-------------------------------------------------------------------------------------------------------------
Gabriel Moraru
2026.09.10
www.GabrielMoraru.com
Github.com/GabrielOnDelphi/Delphi-LightSaber/blob/main/System/Copyright.txt
--------------------------------------------------------------------------------------------------------------
DOWNLOADS A FILE FROM THE INTERNET
Uses WinInet via LightCore.Download.
Features:
- Asynchronous download in a background thread
- Fires OnDownloadDone event when complete (thread-safe via Synchronize)
- Supports HTTP and HTTPS URLs
Usage:
var Downloader: TWinInetObj;
Downloader:= TWinInetObj.Create;
Downloader.OnDownloadDone:= MyDownloadDoneHandler;
Downloader.URL:= 'https://example.com/file.zip';
Downloader.Start;
// When done, Data property contains the downloaded content
// Remember to free the Downloader when finished. Do NOT free Data - it is owned and freed by the Downloader!
--------------------------------------------------------------------------------------------------------------
Also see:
c:\Users\Public\Documents\Embarcadero\Studio\37.0\Samples\Object Pascal\RTL\HttpAsyncDownload\HttpAsyncDownloadDemo.dpr
Tester:
c:\Projects\LightSaber\Demo\Core\Demo Internet\
c:\Projects\Testers\Internet download tester images\
-------------------------------------------------------------------------------------------------------------}
{$WARN GARBAGE OFF} {Silence the: 'W1011 Text after final END' warning }
INTERFACE
USES
System.SysUtils, System.Classes;
TYPE
TWinInetObj = class(TThread)
private
FUrl: String;
FData: TMemoryStream; { The downloaded content will be stored here }
FHttpRetCode: string; { Error message from the download engine. Empty = success }
FOnDownloadDone: TNotifyEvent;
procedure SetURL(CONST Value: string);
procedure DoDownloadDone;
protected
procedure Execute; override;
public
UserAgent: string; { Reserved for future use }
Header: string; { Header to send to the server. Example: 'Accept-Charset: utf-8'#13#10; Reserved for future use }
Referer: string; { Reserved for future use }
SSL: Boolean; { Reserved for future use }
constructor Create;
destructor Destroy; override;
function DownloadSuccess: Boolean; { TRUE when Data is not NIL and holds at least one byte }
property URL: String read FUrl write SetUrl;
property Data: TMemoryStream read FData;
property HttpRetCode: string read FHttpRetCode; { Error message from LightCore.Download.DownloadToStream (e.g., 'HTTP error 404: Not Found'). Empty = success. NOT a bare numeric code! }
property OnDownloadDone: TNotifyEvent read FOnDownloadDone write FOnDownloadDone;
end;
IMPLEMENTATION
USES
LightCore.Download, LightCore;
constructor TWinInetObj.Create;
begin
inherited Create(True); // Create suspended
FreeOnTerminate:= FALSE;
FData:= NIL;
FHttpRetCode:= '';
FUrl:= '';
UserAgent:= '';
Header:= '';
Referer:= '';
SSL:= FALSE;
end;
destructor TWinInetObj.Destroy;
begin
FreeAndNil(FData);
inherited;
end;
procedure TWinInetObj.Execute;
begin
FreeAndNil(FData); // Free any previous download data
FHttpRetCode:= '';
FData:= LightCore.Download.DownloadToStream(URL, FHttpRetCode);
// Synchronize, so the OnDownloadDone handler may touch the VCL
if Assigned(FOnDownloadDone)
then Synchronize(DoDownloadDone);
end;
{ Fires the OnDownloadDone event. Execute calls it through Synchronize, so it runs in the main thread. }
procedure TWinInetObj.DoDownloadDone;
begin
if Assigned(FOnDownloadDone)
then FOnDownloadDone(Self);
end;
function TWinInetObj.DownloadSuccess: Boolean;
begin
Result:= (FData <> NIL) AND (FData.Size > 0);
end;
procedure TWinInetObj.SetURL(const Value: string);
begin
Assert(Value <> '', 'TWinInetObj.SetURL: URL is empty');
if (Length(Value) < 10) // Minimum: http://x.co
OR (PosInsensitive('http', Value) <> 1)
then raise Exception.Create('Invalid URL address. The URL must start with ''http://'' or ''https://''.')
else FURL:= Value;
end;
end.