-
Notifications
You must be signed in to change notification settings - Fork 1
/
wgetfileu.pas
197 lines (177 loc) · 4.65 KB
/
wgetfileu.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
unit WGetFileU;
{
Author: Miguel A. Risco-Castillo
Version 1.0
use WGET tool
}
{$mode objfpc}{$H+}
interface
uses
Classes, Forms, SysUtils, Dialogs, Strutils,
Process, AsyncProcess;
type
tDwStatus=(dwStart,dwIdle,dwDownloading,dwTimeOut,dwError);
{ TDwProcess }
TDwProcess = class(TAsyncProcess)
private
FFileDestiny: String;
FStatus: tDwStatus;
FUrlSource: String;
WaitingforIdle:Boolean;
procedure SetFileDestiny(AValue: String);
procedure SetStatus(AValue: tDwStatus);
procedure SetUrlSource(AValue: String);
procedure DwTerminate(Sender: TObject);
procedure DwReadData(Sender: TObject);
public
Constructor Create (AOwner : TComponent);override;
function WGET(url, f: string): boolean;
function WaitforIdle(waitsec:integer):boolean;
published
property Status:tDwStatus read FStatus write SetStatus;
property UrlSource:String read FUrlSource write SetUrlSource;
property FileDestiny:String read FFileDestiny write SetFileDestiny;
end;
implementation
uses DebugFormU;
{ TDwProcess }
constructor TDwProcess.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Options:=[poUsePipes,poStderrToOutPut];
PipeBufferSize:=4096;
ShowWindow:=swoMinimize;
WaitingforIdle:=false;
FStatus:=dwIdle;
OnTerminate:=@DwTerminate;
OnReadData:=@DwReadData;
end;
function TDwProcess.WGET(url, f: string): boolean;
begin
result:=false;
if not WaitforIdle(5) then exit;
FUrlSource:=url;
FFileDestiny:=f;
Info('DwProcess.WGET','WGET started');
Info('DwProcess.WGET','Url= '+FUrlSource);
Info('DwProcess.WGET','file destiny= '+FFileDestiny);
DeleteFile(FFileDestiny);
Parameters.Clear;
Info('DwProcess.WGET','Current folder: '+CurrentDirectory);
{$IFDEF WINDOWS}
Executable:=Application.location+'tools'+PathDelim+'wget.exe';
{$ENDIF}
{$IFDEF LINUX}
Executable:='wget';
{$ENDIF}
{$IFDEF DARWIN}
Executable:='curl';
{$ENDIF}
{$IFNDEF DEBUG}
{$IFDEF DARWIN}
Parameters.Add('-s');
{$ELSE}
Parameters.Add('-q');
{$ENDIF}
{$ENDIF}
{$IFDEF WINDOWS}
Parameters.Add('-O');
Parameters.Add('"'+FFileDestiny+'"');
Parameters.Add('--no-check-certificate');
Parameters.Add(FUrlSource);
{$ENDIF}
{$IFDEF LINUX}
Parameters.Add('-O');
Parameters.Add(FFileDestiny);
Parameters.Add('--no-check-certificate');
Parameters.Add(FUrlSource);
{$ENDIF}
{$IFDEF DARWIN}
Parameters.Add('-L');
Parameters.Add(FUrlSource);
Parameters.Add('-o');
Parameters.Add(FFileDestiny);
{$ENDIF}
info('DwProcess.WGET',Parameters);
FStatus:=dwDownloading;
try
Execute;
result:=true;
except
On E:Exception do ShowMessage('The web download tool can not be used: '+E.Message);
end;
end;
function TDwProcess.WaitforIdle(waitsec:integer):boolean;
var
TiO:integer;
PStr:String;
begin
result:=false;
if WaitingforIdle then exit;
WaitingforIdle:=true;
WriteStr(PStr,FStatus);
info('TDwProcess.WaitforIdle','Status='+PStr);
TiO:=waitsec*10; // Wait for 20 seconds
{$IFDEF UNIX}
While (TiO>0) and (FStatus<>dwIdle) and Running do
{$ELSE}
While (TiO>0) and (FStatus<>dwIdle) do // LLamar a Running en el loop en Windows da Error
{$ENDIF}
begin
Dec(TiO);
Application.ProcessMessages;
Sleep(100);
end;
//
if (FStatus<>dwIdle) and not Running then
begin
info('TDwProcess.WaitforIdle','el proceso ya no está en ejecución, forzando un Onterminate');
if OnTerminate<>nil then OnTerminate(Self);
info('TDwProcess.WaitforIdle','---- Terminate forzado----');
end;
//
result:=FStatus=dwIdle;
Info('TDwProcess.WaitforIdle',IFTHEN(result,'is Idle','can not terminate: Time Out'));
if not result then
begin
Info('TDwProcess.WaitforIdle','timeout in Url: '+FUrlSource);
//Terminar el proceso que ejecuta WGet si este falla
Terminate(0);
FStatus:=dwTimeOut;
end;
WriteStr(PStr,FStatus);
info('TDwProcess.WaitforIdle','Exit Status='+PStr);
WaitingforIdle:=false;
end;
procedure TDwProcess.DwTerminate(Sender: TObject);
begin
Info('TDWProcess.DWterminate: NumBytesAvailable',NumBytesAvailable);
if NumBytesAvailable>0 then OnReadData(Sender);
info('TDwProcess.DWterminate',FFileDestiny);
FStatus:=dwIdle;
end;
procedure TDwProcess.DwReadData(Sender: TObject);
var
t:TStringList;
begin
t:=TStringList.Create;
t.LoadFromStream(Output);
info('DwReadData',t.Text);
t.free;
end;
procedure TDwProcess.SetStatus(AValue: tDwStatus);
begin
if FStatus=AValue then Exit;
FStatus:=AValue;
end;
procedure TDwProcess.SetFileDestiny(AValue: String);
begin
if FFileDestiny=AValue then Exit;
FFileDestiny:=AValue;
end;
procedure TDwProcess.SetUrlSource(AValue: String);
begin
if FUrlSource=AValue then Exit;
FUrlSource:=AValue;
end;
end.