Unit CastleDownload

DescriptionUsesClasses, Interfaces, Objects and RecordsFunctions and ProceduresTypesConstantsVariables

Description

Download URLs.

Uses

  • SysUtils
  • Classes
  • FpHttpClient

Overview

Classes, Interfaces, Objects and Records

Name Description
Class EDownloadError  

Functions and Procedures

function Download(const URL: string; const Options: TStreamOptions = []): TStream;
function Download(const URL: string; const Options: TStreamOptions; out MimeType: string): TStream;
function URLSaveStream(const URL: string; const Options: TStreamOptions = []): TStream;

Types

TStreamOption = (...);
TStreamOptions = set of TStreamOption;

Constants

DefaultEnableNetwork = false;

Variables

EnableNetwork: boolean = DefaultEnableNetwork;

Description

Functions and Procedures

function Download(const URL: string; const Options: TStreamOptions = []): TStream;

Return a stream to read given URL. Returned stream is suitable only for reading, and the initial position is always at the beginning. Overloaded version also returns a MIME type (or '' if unknown).

All errors are reported by raising exceptions.

A local file URL is always supported, without using any networking library. URL without any protocol is always treated like a local filename (absolute or relative to current dir), so this function can be a drop-in replacement for normal file reading. The MIME type for local files is guessed based on their extension.

A data URI scheme (http://en.wikipedia.org/wiki/Data_URI_scheme) is also always supported. The MIME type for such content is specified explicitly in URI. TODO: right now, soGzip is ignored for data URIs, we never filter them through gunzip.

It also automatically supports protocols to embed script contents: ecmascript, javascript (see VRML and X3D specifications), castlescript, kambiscript (see http://castle-engine.sourceforge.net/castle_script.php), compiled (http://castle-engine.sourceforge.net/x3d_extensions.php#section_ext_script_compiled). The MIME type for these is implied by the protocol (like "application/javascript" for ecmascript/javascript), and the returned stream simply contains script code.

Set EnableNetwork to True to have also support for network protocols. Right now this means only http, handled by FpHttpClient. The MIME type for such content is usually reported by the http server (but if the server doesn't report MIME type, we still try to guess it, looking at URL using URIMimeType).

On Android, URLs that indicate assets (files packaged inside .apk) are also supported, as assets:/my_file.png.

function Download(const URL: string; const Options: TStreamOptions; out MimeType: string): TStream;
 
function URLSaveStream(const URL: string; const Options: TStreamOptions = []): TStream;

Create a stream to save a given URL, for example create a TFileStream to save a file for a file URL. In other words, perform upload. Right now, this only works for file URLs, and the only advantage it has over manually creating TFileStream is that this accepts URLs.

Types

TStreamOption = (...);

Options for the Download and URLSaveStream functions.

Values
  • soForceMemoryStream: Force result to be a TMemoryStream, with contents fully loaded to the memory, and freely seekable (you can move back and forth within). Without this option, Download may return other streams, for example TFileStream (that may not have good buffering, depending on OS) or TBase64DecodingStream (that may not allow seeking).

    Using TMemoryStream means that reading is fast and comfortable, but eats memory and doesn't allow to simultaneously read and process the contents (the file must be fully loaded, e.g. downloaded from the Internet, and ungzipped, before this function returns). So use this option only for files that aren't too big.

    For larger files, you usually don't want to use this option, instead wrap result in TBufferedReadStream.

  • soGzip: Filter the contents through gzip decompression (for Download) or compression (for URLSaveStream).
TStreamOptions = set of TStreamOption;
 

Constants

DefaultEnableNetwork = false;
 

Variables

EnableNetwork: boolean = DefaultEnableNetwork;

Can Download actually use the network. As all the downloading is blocking for now, this is initially False. If you want to really use the network, change it to True.