-
Notifications
You must be signed in to change notification settings - Fork 1
/
sm_web.pas
189 lines (164 loc) · 3.97 KB
/
sm_web.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
unit sm_web;
//{$mode objfpc}{$H+}
{$mode delphi}
interface
uses
Classes, SysUtils,HttpSend,sm_types,FileUtil,{remove it when we integrate that to simba}bzip2, bzip2comn,bzip2stream,libtar{mmisc};
Type
{ TDownloader }
TStringArray = array of string;
TDownloader = Class(TObject)
private
FUrl: string;
//this is not useable variables
//end not useable block
public
procedure Download(var st: TFileStream);overload;
procedure Download(var st: TMemoryStream);overload;
function GetFile(URL: String): TMemoryStream;
//working in windows perfectly
procedure DecompressBZip2(const Input: TStream; var Output: TMemoryStream; const BlockSize: Cardinal = 4096);
//
function Decompress(const SourceFile: TStringStream;TargetFile: string): boolean;
constructor Create(url: string);
destructor Destroy; override;
end;
implementation
{ TDownloader }
constructor TDownloader.Create(url: string);
begin
inherited Create;
FURl:=url;
end;
function TDownloader.GetFile(URL: String): TMemoryStream;
var
HTTP : THTTPSend;
S: string;
i: integer;
begin;
HTTP := THTTPSend.Create;
HTTP.UserAgent := 'Mozilla 4.0/ (compatible Synapse)';
try
if HTTP.HTTPMethod('GET', URL) then
begin
// SetLength(result,HTTP.Document.Size);
//HTTP.Document.Read(result[1],length(result));
s:=URL;
i:=HTTP.Document.Size;
DecompressBZIP2(HTTP.Document,result);
end;
finally
HTTP.Free;
end;
end;
procedure TDownloader.Download(var st: TFileStream); overload;
var
http: THttpSend;
begin
http:=THttpSend.Create;
try
HttpGetBinary(FUrl,st);
finally
http.Free;
end;
end;
procedure TDownloader.Download(var st: TMemoryStream); overload;
var
http: THttpSend;
begin
http:=THttpSend.Create;
try
HttpGetBinary(FUrl,st);
finally
http.Free;
end;
end;
{procedure TDownloader.DecompressBZip2(const input: TStream; var res: TMemoryStream;
const BlockSize: Cardinal);
{var
Unzipper : TDecompressBzip2Stream;
Blocks : array [0..4096] of cardinal;
ReadSize : cardinal;
i,j: integer;
begin
// SetLength(Blocks,BlockSize);
try
Unzipper := TDecompressBzip2Stream.Create(input);
except
on e : exception do
begin;
// mDebugLn(e.message);
exit;
end;
end;
try
repeat
ReadSize := BlockSize;
ReadSize := Unzipper.Read(blocks[0],readsize); //Read ReadSize amount of bytes.
Res.Write(Blocks[0],ReadSize);
until readsize = 0;
except
on e : EBzip2 do
if E.ErrCode <> bzip2_endoffile then
raise Exception.CreateFmt('Decompression error: %s %d',[e.message,e.errcode]);
end;
Unzipper.Free;
end; }
procedure TDownloader.DecompressBZip2(const Input: TStream; var Output: TMemoryStream; const BlockSize: Cardinal = 4096);
var
Buffer: Pointer;
ReadSize: Cardinal;
temp: TMemoryStream;
begin
temp:=TMemoryStream.Create;
with TDecompressBzip2Stream.Create(Input) do
try
GetMem(Buffer, BlockSize+1);
try
repeat
ReadSize := Read(Buffer^, BlockSize);
temp.write(Buffer^, ReadSize);
until (ReadSize = 0);
temp.Position:=0;
OutPut:=TMemoryStream.Create;
OutPut.LoadFromStream(temp);
finally
temp.Free;
Freemem(Buffer);
end;
finally
Free;
end;
end;
function TDownloader.Decompress(const SourceFile: TStringStream;TargetFile: string): boolean;
var
Decompressed:TDecompressBzip2Stream;
OutFile:TFileStream;
Buffer: Pointer;
i:integer;
const buffersize=$2000;
begin
result:=false;
try
Decompressed:=TDecompressBzip2Stream.Create(SourceFile);
OutFile:=TFileStream.Create(TargetFile, fmCreate);
try
GetMem(Buffer,BufferSize);
repeat
i:=Decompressed.Read(buffer^,BufferSize);
if i>0 then
OutFile.WriteBuffer(buffer^,i);
until i<BufferSize;
result:=true;
finally
Decompressed.Free;
OutFile.Free;
end;
finally
end;
end;
destructor TDownloader.Destroy;
begin
inherited Destroy;
end;
end.