Files
lazarus-ccr/components/lazmapviewer/source/mvdownloadengine.pas

76 lines
1.7 KiB
ObjectPascal
Raw Normal View History

{ Map Viewer Download Engine
Copyright (C) 2011 Maciej Kaczkowski / keit.co
License: modified LGPL with linking exception (like RTL, FCL and LCL)
See the file COPYING.modifiedLGPL.txt, included in the Lazarus distribution,
for details about the license.
See also: https://wiki.lazarus.freepascal.org/FPC_modified_LGPL
}
unit mvDownloadEngine;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
{ TMvCustomDownloadEngine }
TMvCustomDownloadEngine = class(TComponent)
protected
function GetLocalFileName(const Url: String): String;
procedure InternalDownloadFile(const Url: String; AStream: TStream); virtual; abstract;
procedure LoadFromLocalFile(const AFileName: String; AStream: TStream);
public
procedure DownloadFile(const Url: string; AStream: TStream); virtual;
end;
implementation
uses
StrUtils;
const
FILE_SCHEME = 'file://';
{ TMvCustomDownloadEngine }
procedure TMvCustomDownloadEngine.DownloadFile(const Url: string; AStream: TStream);
begin
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;
end;
end.