-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathU_DownloadThread.pas
119 lines (106 loc) · 3.25 KB
/
U_DownloadThread.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
unit U_DownloadThread;
interface
uses
System.Classes, System.SysUtils,
Vcl.ExtCtrls, Vcl.Graphics, Vcl.Imaging.jpeg, Vcl.Imaging.pngimage,
IdIOHandler, IdIOHandlerSocket, IdURI, IdIOHandlerStack, IdSSL, IdSSLOpenSSL,
IdBaseComponent, IdComponent, IdException, IdTCPConnection, IdTCPClient, IdHTTP,
U_Resources;
type
TDOwnThread = class( TThread )
private
IndyHTTP: TIdHTTP;
IndySSL: TIdSSLIOHandlerSocketOpenSSL;
Img: TImage;
Graph: TGraphic;
procedure AddPicture;
protected
procedure Execute; override;
public
Url: string;
Ext: string;
constructor Create; reintroduce;
destructor Destroy; override;
end;
implementation
uses
F_Main;
constructor TDOwnThread.Create;
begin
inherited Create( True );
IndyHTTP:= TIdHTTP.Create;
IndySSL:= TIdSSLIOHandlerSocketOpenSSL.Create;
if Frm_Editor.FProxyUse then begin
IndyHTTP.ProxyParams.ProxyUsername:= Frm_Editor.FProxyUser;
IndyHTTP.ProxyParams.ProxyPassword:= Frm_Editor.FProxyPwd;
IndyHTTP.ProxyParams.ProxyServer:= Frm_Editor.FProxyServer;
IndyHTTP.ProxyParams.ProxyPort:= StrToInt( Frm_Editor.FProxyPort );
end else begin
IndyHTTP.ProxyParams.ProxyUsername:= '';
IndyHTTP.ProxyParams.ProxyPassword:= '';
IndyHTTP.ProxyParams.ProxyServer:= '';
IndyHTTP.ProxyParams.ProxyPort:= 0;
end;
IndyHTTP.IOHandler:= IndySSL;
IndyHTTP.Request.UserAgent:= 'Mozilla/3.0 (compatible; Indy Library)';
IndyHTTP.Request.Accept:= 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
Img:= TImage.Create( nil );
Img.AutoSize:= True;
Img.Center:= True;
Img.Proportional:= True;
Img.Visible:= False;
FreeOnTerminate:= True;
end;
destructor TDOwnThread.Destroy;
begin
IndyHTTP.Free;
IndySSL.Free;
inherited;
end;
procedure TDOwnThread.Execute;
var
Stream: TBytesStream;
begin
Stream:= TBytesStream.Create;
try
try
IndyHTTP.Get( Url, Stream );
if ( Stream.Size = 0 ) then begin
Frm_Editor.WarnUser( Rst_StreamError );
Exit;
end;
Stream.Position:= 0;
//on crée le graphic qui va bien en fonction de l'extension
if ( Ext = Cst_PngExt ) then Graph:= TPngImage.Create
else Graph:= TJPEGImage.create;
Graph.LoadFromStream( Stream );
Img.Picture.Graphic:= Graph;
except
//gestion des erreurs de connexion
on E: EIdHTTPProtocolException do begin
case E.ErrorCode of
400: Frm_Editor.WarnUser( Rst_ServerError1 );
401: Frm_Editor.WarnUser( Rst_ServerError2 );
403: Frm_Editor.WarnUser( Rst_ServerError3 );
404: Frm_Editor.WarnUser( Rst_ServerError4 );
423: Frm_Editor.WarnUser( Rst_ServerError5 );
426: Frm_Editor.WarnUser( Rst_ServerError6 );
429: Frm_Editor.WarnUser( Rst_ServerError7 );
end;
Exit;
end;
on E: EIdException do begin
Frm_Editor.WarnUser( Rst_ServerError8 );
Exit;
end;
end;
finally
Stream.Free;
end;
Synchronize( AddPicture );
end;
procedure TDOwnThread.AddPicture;
begin
Frm_Editor.FImgList.Add( Img );
end;
end.