| 1 |
{%MainUnit castledownload.pas} |
| 2 |
{ |
| 3 |
Copyright 2013-2024 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 file URLs. } |
| 24 |
|
| 25 |
{ TUrlFile ------------------------------------------------------------------- } |
| 26 |
|
| 27 |
type |
| 28 |
TUrlFile = class |
| 29 |
class function Read(const Url: String; out MimeType: string): TStream; |
| 30 |
class function Write(const Url: String): TStream; |
| 31 |
{ Determine MIME type from URL extension. } |
| 32 |
class function MimeType(const URI: string; out Gzipped: Boolean): String; |
| 33 |
end; |
| 34 |
|
| 35 |
class function TUrlFile.Read(const Url: String; out MimeType: string): TStream; |
| 36 |
var |
| 37 |
FileName: String; |
| 38 |
begin |
| 39 |
CheckFileAccessSafe(URL); |
| 40 |
|
| 41 |
FileName := UriToFilenameSafe(URL); |
| 42 |
if FileName = '' then |
| 43 |
raise EDownloadError.CreateFmt('Cannot convert URL "%s" to filename', [URL]); |
| 44 |
|
| 45 |
Result := TFileStream.Create(FileName, fmOpenRead); |
| 46 |
MimeType := UriMimeType(URL); |
| 47 |
end; |
| 48 |
|
| 49 |
class function TUrlFile.Write(const Url: String): TStream; |
| 50 |
var |
| 51 |
FileName, Dir: String; |
| 52 |
begin |
| 53 |
FileName := UriToFilenameSafe(URL); |
| 54 |
if FileName = '' then |
| 55 |
raise ESaveError.CreateFmt('Cannot convert URL to a filename: "%s"', [URL]); |
| 56 |
|
| 57 |
{ We need to create subdirectory for the file, |
| 58 |
testcase: https://github.com/michaliskambi/test_config_subdir } |
| 59 |
Dir := ExtractFilePath(FileName); |
| 60 |
{ Ignore Dir = ''. As delphi_tests show, it is possible to get here Url |
| 61 |
that is relative, so also FileName that is relative, like 'aaa.castle-transform', |
| 62 |
and then Dir is ''. It's OK to ignore it -- it means the target directory |
| 63 |
is current directory, no need to create anything. } |
| 64 |
if (Dir <> '') and (not ForceDirectories(Dir)) then |
| 65 |
raise Exception.CreateFmt('Cannot create directory "%s" for filename "%s"', [ |
| 66 |
Dir, |
| 67 |
FileName |
| 68 |
]); |
| 69 |
|
| 70 |
Result := TFileStream.Create(FileName, fmCreate); |
| 71 |
end; |
| 72 |
|
| 73 |
class function TUrlFile.MimeType(const URI: string; out Gzipped: Boolean): String; |
| 74 |
var |
| 75 |
U: TURI; |
| 76 |
Ext, ExtExt, URIName, UriPathDocument: String; |
| 77 |
begin |
| 78 |
Result := ''; |
| 79 |
Gzipped := false; |
| 80 |
|
| 81 |
U := ParseURI(URI); |
| 82 |
|
| 83 |
{ Deconstruct first URL properly using ParseURI. |
| 84 |
This allows to strip the HTTP query part "?xxx", see |
| 85 |
https://github.com/castle-engine/castle-engine/issues/547 . |
| 86 |
|
| 87 |
Note that ParseURI also strips anchor, like #xxx . |
| 88 |
Otherwise 'xxx.json#skinname' would not be detected as Spine JSON. |
| 89 |
So there's no need for URIDeleteAnchor lower, UriPathDocument is already |
| 90 |
without anchor. |
| 91 |
The anchor here is stripped, so all loaders |
| 92 |
|
| 93 |
- should strip anchors, e.g. both LoadImageAsNode and LoadImage must strip anchors. |
| 94 |
- can use anchors for extra info, like Spine, sprite sheets and images |
| 95 |
already do. |
| 96 |
} |
| 97 |
UriPathDocument := U.Path + U.Document; |
| 98 |
|
| 99 |
{ We're consciously using here ExtractFileExt and ExtractFileDoubleExt on URIs, |
| 100 |
although they should be used for filenames. |
| 101 |
Note that this unit does not define public functions like ExtractURIExt |
| 102 |
or ExtractURIDoubleExt: *everything* should operate on MIME types instead. } |
| 103 |
Ext := LowerCase(ExtractFileExt(UriPathDocument)); |
| 104 |
ExtExt := LowerCase(ExtractFileDoubleExt(UriPathDocument)); |
| 105 |
|
| 106 |
URIName := LowerCase(ExtractURIName(UriPathDocument)); |
| 107 |
|
| 108 |
{ This list is based on |
| 109 |
http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/lcl/interfaces/customdrawn/customdrawnobject_android.inc?root=lazarus&view=co&content-type=text%2Fplain |
| 110 |
(license is LGPL with static linking exception, just like our engine). |
| 111 |
See also various resources linked from |
| 112 |
"Function to get the mimetype from a file extension" thread on Lazarus |
| 113 |
mailing list: |
| 114 |
http://comments.gmane.org/gmane.comp.ide.lazarus.general/62738 |
| 115 |
|
| 116 |
We somewhat cleaned it up (e.g. "postscript" and "mpeg" lowercase), |
| 117 |
fixed categorization, and fixed/added many types looking at |
| 118 |
/etc/mime.types and |
| 119 |
/usr/share/mime/packages/freedesktop.org.xml on Debian. |
| 120 |
|
| 121 |
For description of MIME content types see also |
| 122 |
https://en.wikipedia.org/wiki/Internet_media_type |
| 123 |
http://en.wikipedia.org/wiki/MIME |
| 124 |
http://tools.ietf.org/html/rfc4288 } |
| 125 |
|
| 126 |
// 3D models (see also castle-model-viewer MIME specification in castle-model-viewer/desktop/castle-model-viewer.xml) |
| 127 |
if Ext = '.wrl' then Result := 'model/vrml' else |
| 128 |
if Ext = '.wrz' then begin Result := 'model/vrml'; Gzipped := true; end else |
| 129 |
if ExtExt = '.wrl.gz' then begin Result := 'model/vrml'; Gzipped := true; end else |
| 130 |
if Ext = '.x3dv' then Result := 'model/x3d+vrml' else |
| 131 |
if Ext = '.x3dvz' then begin Result := 'model/x3d+vrml'; Gzipped := true; end else |
| 132 |
if ExtExt = '.x3dv.gz' then begin Result := 'model/x3d+vrml'; Gzipped := true; end else |
| 133 |
if Ext = '.x3d' then Result := 'model/x3d+xml' else |
| 134 |
if Ext = '.x3dz' then begin Result := 'model/x3d+xml'; Gzipped := true; end else |
| 135 |
if ExtExt = '.x3d.gz' then begin Result := 'model/x3d+xml'; Gzipped := true; end else |
| 136 |
if Ext = '.x3db' then Result := 'model/x3d+binary' else |
| 137 |
if ExtExt = '.x3db.gz' then begin Result := 'model/x3d+binary'; Gzipped := true; end else |
| 138 |
if Ext = '.dae' then Result := 'model/vnd.collada+xml' else |
| 139 |
{ See http://en.wikipedia.org/wiki/.3ds about 3ds mime type. |
| 140 |
application/x-3ds is better (3DS is hardly an "image"), |
| 141 |
but Debian /usr/share/mime/packages/freedesktop.org.xml also uses |
| 142 |
image/x-3ds, so I guess image/x-3ds is more popular. } |
| 143 |
if Ext = '.3ds' then Result := 'image/x-3ds' else |
| 144 |
if Ext = '.max' then Result := 'image/x-3ds' else |
| 145 |
if Ext = '.iv' then Result := 'application/x-inventor' else |
| 146 |
if Ext = '.md3' then Result := 'application/x-md3' else |
| 147 |
if Ext = '.obj' then Result := 'application/x-wavefront-obj' else |
| 148 |
if Ext = '.geo' then Result := 'application/x-geo' else |
| 149 |
if Ext = '.kanim' then Result := 'application/x-castle-anim-frames' else |
| 150 |
if Ext = '.castle-anim-frames' then Result := 'application/x-castle-anim-frames' else |
| 151 |
if Ext = '.json' then Result := 'application/json' else |
| 152 |
{ Various sites propose various MIME types for STL: |
| 153 |
https://gist.github.com/allysonsouza/1bf9d4a0295a14373979cd23d15df0a9 |
| 154 |
application/wavefront-stl |
| 155 |
application/vnd.ms-pki.stl } |
| 156 |
if Ext = '.stl' then Result := 'application/x-stl' else |
| 157 |
if Ext = '.glb' then Result := 'model/gltf-binary' else |
| 158 |
if Ext = '.gltf' then Result := 'model/gltf+json' else |
| 159 |
// Images. |
| 160 |
{ Only images that we cannot handle in CastleImages unit are listed below. |
| 161 |
For handled images, their extensions and mime types are recorded |
| 162 |
by CastleImages inside the URIMimeExtensions. } |
| 163 |
if Ext = '.svg' then Result := 'image/svg+xml' else |
| 164 |
if Ext = '.ico' then Result := 'image/x-icon' else |
| 165 |
if Ext = '.icns' then Result := 'image/icns' else |
| 166 |
if Ext = '.castle-sprite-sheet' then Result := 'application/x-castle-sprite-sheet' else |
| 167 |
{ I didn't found real MIME type for Starling Texture Atlas. |
| 168 |
Created as image type based on |
| 169 |
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types |
| 170 |
} |
| 171 |
if Ext = '.starling-xml' then Result := 'application/x-starling-sprite-sheet' else |
| 172 |
if Ext = '.cocos2d-plist' then Result := 'application/x-cocos2d-sprite-sheet' else |
| 173 |
if Ext = '.plist' then Result := 'application/x-plist' else |
| 174 |
|
| 175 |
// HTML |
| 176 |
if Ext = '.htm' then Result := 'text/html' else |
| 177 |
if Ext = '.html' then Result := 'text/html' else |
| 178 |
if Ext = '.shtml' then Result := 'text/html' else |
| 179 |
if Ext = '.css' then Result := 'text/css' else |
| 180 |
if Ext = '.php' then Result := 'text/php' else |
| 181 |
// Plain text |
| 182 |
if Ext = '.txt' then Result := 'text/plain' else |
| 183 |
if Ext = '.pas' then Result := 'text/plain' else |
| 184 |
if Ext = '.pp' then Result := 'text/plain' else |
| 185 |
if Ext = '.inc' then Result := 'text/plain' else |
| 186 |
if Ext = '.c' then Result := 'text/plain' else |
| 187 |
if Ext = '.cpp' then Result := 'text/plain' else |
| 188 |
if Ext = '.java' then Result := 'text/plain' else |
| 189 |
if Ext = '.log' then Result := 'text/plain' else |
| 190 |
if Ext = '.md' then Result := 'text/plain' else |
| 191 |
if URIName = '.gitignore' then Result := 'text/plain' else |
| 192 |
// Videos |
| 193 |
if Ext = '.mp4' then Result := 'video/mp4' else |
| 194 |
if Ext = '.avi' then Result := 'video/x-msvideo' else |
| 195 |
if Ext = '.mpeg' then Result := 'video/mpeg' else |
| 196 |
if Ext = '.mpg' then Result := 'video/mpeg' else |
| 197 |
if Ext = '.mpe' then Result := 'video/mpeg' else |
| 198 |
if Ext = '.ogv' then Result := 'video/ogg' else |
| 199 |
if Ext = '.mov' then Result := 'video/quicktime' else |
| 200 |
if Ext = '.flv' then Result := 'video/x-flv' else |
| 201 |
if Ext = '.swf' then Result := 'application/x-shockwave-flash' else |
| 202 |
if Ext = '.swfl' then Result := 'application/x-shockwave-flash' else |
| 203 |
// Sounds |
| 204 |
if Ext = '.mp3' then Result := 'audio/mpeg' else |
| 205 |
if Ext = '.ogg' then Result := 'audio/ogg' else |
| 206 |
if Ext = '.oga' then Result := 'audio/ogg' else |
| 207 |
if Ext = '.wav' then Result := 'audio/wav' else |
| 208 |
if Ext = '.mid' then Result := 'audio/midi' else |
| 209 |
if Ext = '.midi' then Result := 'audio/midi' else |
| 210 |
if Ext = '.au' then Result := 'audio/basic' else |
| 211 |
if Ext = '.snd' then Result := 'audio/basic' else |
| 212 |
if Ext = '.mp2' then Result := 'audio/mpeg' else |
| 213 |
// Documents |
| 214 |
if Ext = '.rtf' then Result := 'text/rtf' else |
| 215 |
if Ext = '.eps' then Result := 'application/postscript' else |
| 216 |
if Ext = '.ps' then Result := 'application/postscript' else |
| 217 |
if Ext = '.pdf' then Result := 'application/pdf' else |
| 218 |
if Ext = '.csv' then Result := 'application/csv' else |
| 219 |
// Documents - old MS Office |
| 220 |
if Ext = '.xls' then Result := 'application/vnd.ms-excel' else |
| 221 |
if Ext = '.doc' then Result := 'application/msword' else |
| 222 |
if Ext = '.ppt' then Result := 'application/vnd.ms-powerpoint' else |
| 223 |
// Documents - open standards |
| 224 |
if Ext = '.odt' then Result := 'application/vnd.oasis.opendocument.text' else |
| 225 |
if Ext = '.ods' then Result := 'application/vnd.oasis.opendocument.spreadsheet' else |
| 226 |
if Ext = '.odp' then Result := 'application/vnd.oasis.opendocument.presentation' else |
| 227 |
if Ext = '.odg' then Result := 'application/vnd.oasis.opendocument.graphics' else |
| 228 |
if Ext = '.odc' then Result := 'application/vnd.oasis.opendocument.chart' else |
| 229 |
if Ext = '.odf' then Result := 'application/vnd.oasis.opendocument.formula' else |
| 230 |
if Ext = '.odi' then Result := 'application/vnd.oasis.opendocument.image' else |
| 231 |
// Documents - new MS Office |
| 232 |
if Ext = '.xlsx' then Result := 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' else |
| 233 |
if Ext = '.pptx' then Result := 'application/vnd.openxmlformats-officedocument.presentationml.presentation' else |
| 234 |
if Ext = '.docx' then Result := 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' else |
| 235 |
// Compressed archives |
| 236 |
if Ext = '.zip' then Result := 'application/zip' else |
| 237 |
if Ext = '.tar' then Result := 'application/x-tar' else |
| 238 |
if Ext = '.rar' then Result := 'application/x-rar-compressed' else |
| 239 |
if Ext = '.gz' then begin Result := 'application/gzip'; Gzipped := true; end else |
| 240 |
// Various |
| 241 |
if Ext = '.xml' then Result := 'application/xml' else |
| 242 |
if Ext = '.castlescript' then Result := 'text/x-castlescript' else |
| 243 |
if Ext = '.kscript' then Result := 'text/x-castlescript' else |
| 244 |
if Ext = '.js' then Result := 'application/javascript' else |
| 245 |
if Ext = '.castle-user-interface' then Result := 'text/x-castle-user-interface' else |
| 246 |
if Ext = '.castle-transform' then Result := 'text/x-castle-transform' else |
| 247 |
if Ext = '.castle-component' then Result := 'text/x-castle-component' else |
| 248 |
if Ext = '.tmx' then Result := 'application/x-tiled-map' else |
| 249 |
{ as a last resort, check URIMimeExtensions } |
| 250 |
URIMimeExtensions.TryGetValue(Ext, Result); |
| 251 |
end; |
| 252 |
|
| 253 |
{$endif} |