| 1 |
{%MainUnit castledownload.pas} |
| 2 |
{ |
| 3 |
Copyright 2013-2020 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
{$endif} |
| 20 |
|
| 21 |
{$ifdef read_implementation} |
| 22 |
|
| 23 |
{ Handle data URI ("data URI" allows to embed data in a very long URI; |
| 24 |
this is *not* related to castle-data protocol). } |
| 25 |
|
| 26 |
{ TUrlData ------------------------------------------------------------------- } |
| 27 |
|
| 28 |
type |
| 29 |
TUrlData = class |
| 30 |
class function Read(const Url: String; out MimeType: string): TStream; |
| 31 |
class function MimeType(const URI: string; out Gzipped: Boolean): String; |
| 32 |
end; |
| 33 |
|
| 34 |
class function TUrlData.Read(const Url: String; out MimeType: string): TStream; |
| 35 |
var |
| 36 |
DataUri: TDataUri; |
| 37 |
begin |
| 38 |
DataUri := TDataUri.Create; |
| 39 |
try |
| 40 |
DataUri.Uri := Url; |
| 41 |
if not DataUri.Valid then |
| 42 |
raise EDownloadError.Create('Invalid data: URI scheme'); |
| 43 |
Result := DataUri.ExtractStream; |
| 44 |
MimeType := DataUri.MimeType; |
| 45 |
Assert(Result <> nil, 'DataUri.ExtractStream must be non-nil when DataUri.Valid is true'); |
| 46 |
finally FreeAndNil(DataUri) end; |
| 47 |
end; |
| 48 |
|
| 49 |
class function TUrlData.MimeType(const URI: string; out Gzipped: Boolean): String; |
| 50 |
var |
| 51 |
DataURI: TDataURI; |
| 52 |
begin |
| 53 |
Result := ''; |
| 54 |
Gzipped := false; |
| 55 |
|
| 56 |
DataURI := TDataURI.Create; |
| 57 |
try |
| 58 |
DataURI.URI := URI; |
| 59 |
if DataURI.Valid then Result := DataURI.MimeType; |
| 60 |
finally FreeAndNil(DataURI) end; |
| 61 |
end; |
| 62 |
|
| 63 |
{$endif} |