From f083df12eb27599898dabc2b53e830f24d0586c0 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sun, 12 Feb 2023 13:28:53 +0000 Subject: [PATCH] LazMapViewer: Extend download engines to be able to handle local files (https://www.lazarusforum.de/viewtopic.php?f=18&t=14790). git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8701 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../synapse_downloadengine/mvdlesynapse.pas | 6 +-- components/lazmapviewer/source/mvdlefpc.pas | 5 ++- .../lazmapviewer/source/mvdownloadengine.pas | 38 ++++++++++++++++++- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/components/lazmapviewer/source/addons/synapse_downloadengine/mvdlesynapse.pas b/components/lazmapviewer/source/addons/synapse_downloadengine/mvdlesynapse.pas index 61755946d..cb6c38d59 100644 --- a/components/lazmapviewer/source/addons/synapse_downloadengine/mvdlesynapse.pas +++ b/components/lazmapviewer/source/addons/synapse_downloadengine/mvdlesynapse.pas @@ -29,8 +29,8 @@ type FProxyPort: Integer; FProxyUsername: string; FUseProxy: Boolean; - public - procedure DownloadFile(const Url: string; str: TStream); override; + protected + procedure InternalDownloadFile(const Url: string; str: TStream); override; published property UseProxy: Boolean read FUseProxy write FUseProxy default false; @@ -55,7 +55,7 @@ end; { TMvDESynapse } -procedure TMvDESynapse.DownloadFile(const Url: string; str: TStream); +procedure TMvDESynapse.InternalDownloadFile(const Url: string; str: TStream); var FHttp: THTTPSend; realURL: String; diff --git a/components/lazmapviewer/source/mvdlefpc.pas b/components/lazmapviewer/source/mvdlefpc.pas index c09103970..7f692954d 100644 --- a/components/lazmapviewer/source/mvdlefpc.pas +++ b/components/lazmapviewer/source/mvdlefpc.pas @@ -37,8 +37,9 @@ type FProxyUserName: String; FProxyPassWord: String; {$IFEND} + protected + procedure InternalDownloadFile(const Url: string; AStream: TStream); override; public - procedure DownloadFile(const Url: string; AStream: TStream); override; {$IF FPC_FullVersion >= 30101} published property UseProxy: Boolean read FUseProxy write FUseProxy default false; @@ -63,7 +64,7 @@ uses { TMVDEFPC } -procedure TMVDEFPC.DownloadFile(const Url: string; AStream: TStream); +procedure TMVDEFPC.InternalDownloadFile(const Url: string; AStream: TStream); var http: TFpHttpClient; begin diff --git a/components/lazmapviewer/source/mvdownloadengine.pas b/components/lazmapviewer/source/mvdownloadengine.pas index da2e4b970..33829a89e 100644 --- a/components/lazmapviewer/source/mvdownloadengine.pas +++ b/components/lazmapviewer/source/mvdownloadengine.pas @@ -23,18 +23,52 @@ 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 {%H-}Url: string; {%H-}AStream: TStream); virtual; + 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 - // to be overridden... + 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.