2009-12-31 13:17:49 -05:00
|
|
|
unit updater;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2010-01-01 10:32:23 -05:00
|
|
|
Classes, SysUtils, httpsend,blcksock
|
|
|
|
{$IFDEF LINUX}
|
2010-01-07 13:58:00 -05:00
|
|
|
,BaseUnix
|
2010-01-01 10:32:23 -05:00
|
|
|
{$ENDIF};
|
2009-12-31 13:17:49 -05:00
|
|
|
|
|
|
|
type
|
|
|
|
{ TMMLUpdateThread = class(TThread)
|
|
|
|
procedure Execute; override;
|
|
|
|
constructor Create(CreateSuspended: Boolean);
|
|
|
|
destructor Destroy; override;
|
|
|
|
end; }
|
|
|
|
TMemory = pointer;
|
2010-01-01 10:32:23 -05:00
|
|
|
TMMLFunctionBoolean = function: boolean of object;
|
2009-12-31 15:38:40 -05:00
|
|
|
|
2009-12-31 13:17:49 -05:00
|
|
|
|
|
|
|
TMMLFileDownloader = class(TObject)
|
|
|
|
private
|
|
|
|
FFileURL: String;
|
2010-01-02 20:36:54 -05:00
|
|
|
FBasePath: String;
|
2009-12-31 13:17:49 -05:00
|
|
|
FReplacementFile: String;
|
|
|
|
FTotal: Integer;
|
|
|
|
HTTPSend: THTTPSend;
|
|
|
|
FFileSize: Integer;
|
|
|
|
FDownloaded: Boolean;
|
2010-01-01 10:32:23 -05:00
|
|
|
FOnChange: TMMLFunctionBoolean;
|
|
|
|
FOnBeat: TMMLFunctionBoolean;
|
2010-01-02 20:36:54 -05:00
|
|
|
private
|
|
|
|
procedure SetBasePath(s: string);
|
2009-12-31 13:17:49 -05:00
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
2009-12-31 15:38:40 -05:00
|
|
|
{ Download and Save the file }
|
2009-12-31 13:17:49 -05:00
|
|
|
function DownloadAndSave: Boolean;
|
2009-12-31 15:38:40 -05:00
|
|
|
|
|
|
|
{ Replace the new file with the one we downloaded }
|
2009-12-31 13:17:49 -05:00
|
|
|
function Replace: Boolean;
|
|
|
|
|
2009-12-31 15:38:40 -05:00
|
|
|
{ Where do we get the file from? }
|
2009-12-31 13:17:49 -05:00
|
|
|
property FileURL: String read FFileURL write FFileURL;
|
2009-12-31 15:38:40 -05:00
|
|
|
|
|
|
|
{ The file to write to. Note that downloading downloads to this
|
|
|
|
file with a '_' added to the end of the FileName;
|
|
|
|
Replace; is called to replace the old file with the downloaded
|
|
|
|
one }
|
2009-12-31 13:17:49 -05:00
|
|
|
property ReplacementFile: String read FReplacementFile write FReplacementFile;
|
2009-12-31 15:38:40 -05:00
|
|
|
|
|
|
|
{ Return true if we have downloaded the file completely }
|
2009-12-31 13:17:49 -05:00
|
|
|
property Downloaded: Boolean read FDownloaded;
|
|
|
|
|
2010-01-02 20:36:54 -05:00
|
|
|
property BasePath: String read FBasePath write SetBasePath;
|
|
|
|
|
2009-12-31 15:38:40 -05:00
|
|
|
{ If either of these events return "True", an exception is thrown
|
|
|
|
and the download is cancelled. This way we can easily `cancel'
|
|
|
|
a download. }
|
2010-01-01 10:32:23 -05:00
|
|
|
property OnChange: TMMLFunctionBoolean read FOnChange write FOnChange;
|
2009-12-31 13:17:49 -05:00
|
|
|
|
2009-12-31 15:38:40 -05:00
|
|
|
{ Called every 50ms }
|
2010-01-01 10:32:23 -05:00
|
|
|
property OnBeat: TMMLFunctionBoolean read FOnBeat write FOnBeat;
|
2009-12-31 13:17:49 -05:00
|
|
|
|
2010-01-18 06:52:02 -05:00
|
|
|
property DownloadedSize : Integer read FTotal;
|
|
|
|
property FileSize : integer read FFileSize;
|
2009-12-31 15:38:40 -05:00
|
|
|
function GetPercentage: Integer;
|
2009-12-31 13:17:49 -05:00
|
|
|
private
|
|
|
|
procedure TryToGetFileSize;
|
|
|
|
|
|
|
|
procedure OnMonitor(Sender: TObject; Writing: Boolean;
|
|
|
|
const Buffer: TMemory; Len: Integer);
|
|
|
|
procedure OnStatus(Sender: TObject; Reason: THookSocketReason;
|
|
|
|
const Value: String);
|
2009-12-31 15:38:40 -05:00
|
|
|
procedure OnHeartBeat(Sender: TObject);
|
2009-12-31 13:17:49 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
2010-01-02 20:36:54 -05:00
|
|
|
uses
|
|
|
|
strings;
|
2009-12-31 13:17:49 -05:00
|
|
|
|
2010-01-02 20:36:54 -05:00
|
|
|
procedure TMMLFileDownloader.SetBasePath(s: string);
|
|
|
|
begin
|
|
|
|
if s[length(s) - 1] <> DirectorySeparator then
|
|
|
|
s := s + DirectorySeparator;
|
|
|
|
FBasePath:=s;
|
|
|
|
end;
|
2009-12-31 13:17:49 -05:00
|
|
|
|
|
|
|
procedure TMMLFileDownloader.TryToGetFileSize;
|
|
|
|
var
|
|
|
|
i,p:integer;
|
|
|
|
begin
|
|
|
|
if assigned(HTTPSend.Headers) then
|
|
|
|
for i := 0 to HTTPSend.headers.count - 1 do
|
|
|
|
begin
|
|
|
|
p := Pos('Content-Length: ', HTTPSend.headers.strings[i]);
|
|
|
|
if p <> 0 then
|
|
|
|
FFileSize := StrToInt(Copy(HTTPSend.headers.strings[i],
|
|
|
|
p+length('Content-Length: '),length( HTTPSend.headers.strings[i]) - p) );
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMMLFileDownloader.OnStatus(Sender: TObject; Reason: THookSocketReason;
|
|
|
|
const Value: String);
|
|
|
|
begin
|
|
|
|
if FFileSize = 0 then
|
|
|
|
TryToGetFileSize;
|
|
|
|
if Assigned(FOnChange) then
|
2009-12-31 15:38:40 -05:00
|
|
|
if FOnChange() then
|
|
|
|
raise Exception.Create('OnChange event called for a quit');
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMMLFileDownloader.OnHeartBeat(Sender: TObject);
|
|
|
|
begin
|
|
|
|
if Assigned(FOnBeat) then
|
|
|
|
if FOnBeat() then
|
2010-01-01 10:32:23 -05:00
|
|
|
raise Exception.Create('OnBeat event called for a quit');
|
2009-12-31 13:17:49 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMMLFileDownloader.OnMonitor(Sender: TObject; Writing: Boolean;
|
|
|
|
const Buffer: TMemory; Len: Integer);
|
|
|
|
|
|
|
|
var
|
|
|
|
i,p:integer;
|
|
|
|
begin
|
|
|
|
if writing then exit;
|
|
|
|
Inc(FTotal, len);
|
|
|
|
|
|
|
|
if FFileSize = 0 then
|
|
|
|
TryToGetFileSize;
|
|
|
|
if Assigned(FOnChange) then
|
2009-12-31 15:38:40 -05:00
|
|
|
if FOnChange() then
|
|
|
|
raise Exception.Create('OnChange event called for a quit');
|
2009-12-31 13:17:49 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TMMLFileDownloader.GetPercentage: Integer;
|
|
|
|
begin
|
|
|
|
if FFileSize <> 0 then
|
|
|
|
Exit( Round( (FTotal / FFileSize) * 100.0) )
|
|
|
|
else
|
|
|
|
Exit(-1);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TMMLFileDownloader.DownloadAndSave: Boolean;
|
|
|
|
|
|
|
|
var
|
|
|
|
response: TStream;
|
|
|
|
i:integer;
|
|
|
|
f: TFileStream;
|
|
|
|
|
|
|
|
begin
|
2009-12-31 15:38:40 -05:00
|
|
|
Result := False;
|
2009-12-31 13:17:49 -05:00
|
|
|
HTTPSend := THTTPSend.Create;
|
|
|
|
HTTPSend.Sock.OnMonitor:=@Self.OnMonitor;
|
|
|
|
HTTPSend.Sock.OnStatus:=@Self.OnStatus;
|
2009-12-31 15:38:40 -05:00
|
|
|
HTTPSend.Sock.OnHeartbeat:=@Self.OnHeartBeat;
|
|
|
|
HTTPSend.Sock.HeartbeatRate:=50;
|
|
|
|
|
2009-12-31 13:17:49 -05:00
|
|
|
if FReplacementFile = '' then
|
|
|
|
raise Exception.Create('ReplacementFile not set');
|
|
|
|
if FileURL = '' then
|
|
|
|
raise Exception.Create('FileURL not set');
|
|
|
|
|
2010-01-02 20:36:54 -05:00
|
|
|
Response := TFileStream.Create(FBasePath + FReplacementFile + '_', fmCreate);
|
2009-12-31 13:17:49 -05:00
|
|
|
try
|
|
|
|
Result := HTTPSend.HTTPMethod('GET', FileURL);
|
|
|
|
|
|
|
|
if Result then
|
|
|
|
begin
|
|
|
|
Response.Seek(0, soFromBeginning);
|
|
|
|
Response.CopyFrom(HTTPSend.Document, 0);
|
|
|
|
end;
|
|
|
|
FDownloaded := True;
|
2009-12-31 15:38:40 -05:00
|
|
|
except
|
|
|
|
writeln('DownloadAndSave: Exception Occured');
|
2010-01-01 10:32:23 -05:00
|
|
|
Result := False;
|
2009-12-31 13:17:49 -05:00
|
|
|
end;
|
2010-01-01 10:32:23 -05:00
|
|
|
HTTPSend.Free;
|
|
|
|
Response.Free;
|
|
|
|
if not result then
|
|
|
|
Raise Exception.Create('Throwing it so we can catch it later');
|
2009-12-31 13:17:49 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TMMLFileDownloader.Replace: Boolean;
|
|
|
|
begin
|
2010-01-01 10:32:23 -05:00
|
|
|
{ Change to messages + Exit(False) instead of exceptions? }
|
2009-12-31 13:17:49 -05:00
|
|
|
if not Downloaded then
|
2010-01-01 10:32:23 -05:00
|
|
|
begin
|
|
|
|
writeln('Nothing downloaded');
|
|
|
|
exit(False);
|
|
|
|
// raise Exception.Create('Nothing downloaded');
|
|
|
|
end;
|
2009-12-31 13:17:49 -05:00
|
|
|
if FReplacementFile = '' then
|
2010-01-01 10:32:23 -05:00
|
|
|
begin
|
|
|
|
writeln('ReplacementFile not se');
|
|
|
|
exit(False);
|
|
|
|
//raise Exception.Create('ReplacementFile not set');
|
|
|
|
end;
|
2010-01-02 20:36:54 -05:00
|
|
|
if not FileExists(FBasePath + FReplacementFile) then
|
2010-01-01 10:32:23 -05:00
|
|
|
begin
|
|
|
|
writeln('ReplacementFile not found');
|
|
|
|
exit(False);
|
2010-01-02 08:02:57 -05:00
|
|
|
//raise Exception.Create('ReplacementFile not found');
|
2010-01-01 10:32:23 -05:00
|
|
|
end;
|
2010-01-02 20:36:54 -05:00
|
|
|
if not FileExists(FBasePath + FReplacementFile+ '_') then
|
2010-01-01 10:32:23 -05:00
|
|
|
begin
|
|
|
|
writeln('ReplacementFile + _ not found');
|
|
|
|
exit(False);
|
2010-01-02 08:02:57 -05:00
|
|
|
//raise Exception.Create('ReplacementFile + _ not found');
|
2010-01-01 10:32:23 -05:00
|
|
|
end;
|
2010-01-02 20:36:54 -05:00
|
|
|
RenameFile(FBasePath + FReplacementFile, FBasePath + FReplacementFile+'_old_');
|
|
|
|
RenameFile(FBasePath + FReplacementFile +'_', FBasePath + FReplacementFile);
|
|
|
|
DeleteFile(FBasePath + FReplacementFile+'_old_');
|
2010-01-01 10:32:23 -05:00
|
|
|
{$IFDEF LINUX}
|
2010-01-07 13:58:00 -05:00
|
|
|
fpchmod(FBasePath + FReplacementFile, S_IRUSR or S_IWUSR or S_IXUSR or S_IRGRP
|
|
|
|
or S_IXGRP or S_IROTH or S_IXOTH);
|
2010-01-01 10:32:23 -05:00
|
|
|
{$ENDIF}
|
|
|
|
|
2009-12-31 13:17:49 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TMMLFileDownloader.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FTotal := 0;
|
|
|
|
FFileSize := 0;
|
|
|
|
FDownloaded := False;
|
|
|
|
FReplacementFile:='';
|
|
|
|
FFileURL := '';
|
2010-01-02 20:36:54 -05:00
|
|
|
FBasePath:= '';
|
2010-01-01 10:32:23 -05:00
|
|
|
FOnChange := nil;
|
|
|
|
FOnBeat := nil;
|
2009-12-31 13:17:49 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
destructor TMMLFileDownloader.Destroy;
|
|
|
|
begin
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|