2018-04-16 22:10:14 +00:00
|
|
|
{ Map Viewer Download Engine
|
2018-04-16 13:59:19 +00:00
|
|
|
Copyright (C) 2011 Maciej Kaczkowski / keit.co
|
|
|
|
|
2020-04-09 11:06:15 +00:00
|
|
|
License: modified LGPL with linking exception (like RTL, FCL and LCL)
|
2018-04-16 13:59:19 +00:00
|
|
|
|
2020-04-09 11:06:15 +00:00
|
|
|
See the file COPYING.modifiedLGPL.txt, included in the Lazarus distribution,
|
|
|
|
for details about the license.
|
2018-04-16 13:59:19 +00:00
|
|
|
|
2020-04-09 11:06:15 +00:00
|
|
|
See also: https://wiki.lazarus.freepascal.org/FPC_modified_LGPL
|
2018-04-16 13:59:19 +00:00
|
|
|
}
|
2020-04-09 11:06:15 +00:00
|
|
|
|
2018-04-16 13:59:19 +00:00
|
|
|
unit mvDownloadEngine;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils;
|
|
|
|
|
2018-04-16 15:15:27 +00:00
|
|
|
type
|
2018-04-16 13:59:19 +00:00
|
|
|
|
2018-04-16 15:15:27 +00:00
|
|
|
{ TMvCustomDownloadEngine }
|
|
|
|
|
|
|
|
TMvCustomDownloadEngine = class(TComponent)
|
2023-02-12 13:28:53 +00:00
|
|
|
protected
|
|
|
|
function GetLocalFileName(const Url: String): String;
|
|
|
|
procedure InternalDownloadFile(const Url: String; AStream: TStream); virtual; abstract;
|
|
|
|
procedure LoadFromLocalFile(const AFileName: String; AStream: TStream);
|
2018-04-16 13:59:19 +00:00
|
|
|
public
|
2023-02-12 13:28:53 +00:00
|
|
|
procedure DownloadFile(const Url: string; AStream: TStream); virtual;
|
2018-04-16 13:59:19 +00:00
|
|
|
end;
|
|
|
|
|
2018-04-16 22:10:14 +00:00
|
|
|
|
2018-04-16 13:59:19 +00:00
|
|
|
implementation
|
|
|
|
|
2023-02-12 13:28:53 +00:00
|
|
|
uses
|
|
|
|
StrUtils;
|
|
|
|
|
|
|
|
const
|
|
|
|
FILE_SCHEME = 'file://';
|
|
|
|
|
2018-04-16 15:15:27 +00:00
|
|
|
{ TMvCustomDownloadEngine }
|
2018-04-16 13:59:19 +00:00
|
|
|
|
2018-04-16 15:15:27 +00:00
|
|
|
procedure TMvCustomDownloadEngine.DownloadFile(const Url: string; AStream: TStream);
|
2018-04-16 13:59:19 +00:00
|
|
|
begin
|
2023-02-12 13:28:53 +00:00
|
|
|
if AnsiStartsText(FILE_SCHEME, Url) then
|
|
|
|
LoadFromLocalFile(GetLocalFileName(Url), AStream)
|
|
|
|
else
|
|
|
|
InternalDownloadFile(Url, AStream);
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Chops the "file://" off of a local file name.
|
|
|
|
// Note: Does not check whether the Url really begins with "file://".
|
|
|
|
function TMvCustomDownloadEngine.GetLocalFileName(const Url: String): String;
|
|
|
|
begin
|
|
|
|
Result := Copy(Url, Length(FILE_SCHEME) + 1, MaxInt);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMvCustomDownloadEngine.LoadFromLocalFile(const AFileName: String;
|
|
|
|
AStream: TStream);
|
|
|
|
var
|
|
|
|
fs: TFileStream;
|
|
|
|
begin
|
|
|
|
fs := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
|
|
|
|
try
|
|
|
|
AStream.CopyFrom(fs, fs.Size);
|
|
|
|
AStream.Position := 0;
|
|
|
|
finally
|
|
|
|
fs.Free;
|
|
|
|
end;
|
2018-04-16 13:59:19 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|