net.FileDownloader Extends goog.Disposable
A class for downloading remote files and storing them locally using the HTML5 filesystem API.

Inheritance

Constructor

goog.net.FileDownloader(diropt_pool)

Parameters

dir : !goog.fs.DirectoryEntry
The directory in which the downloaded files are stored. This directory should be solely managed by FileDownloader.
opt_pool : goog.net.XhrIoPool=
The pool of XhrIo objects to use for downloading files.

Instance Methods

Public Protected Private
cancel_(download)
Abort the download of the given URL.
Arguments:
download : !goog.net.FileDownloader.Download_
The download to abort.
code »
disposeInternal()
No description.
code »
download(url) !goog.async.Deferred
Download a remote file and save its contents to the filesystem. A given file is uniquely identified by its URL string; this means that the relative and absolute URLs for a single file are considered different for the purposes of the FileDownloader. Returns a Deferred that will contain the downloaded blob. If there's an error while downloading the URL, this Deferred will be passed the goog.net.FileDownloader.Error object as an errback. If a download is already in progress for the given URL, this will return the deferred blob for that download. If the URL has already been downloaded, this will fail once it tries to save the downloaded blob. When a download is in progress, all Deferreds returned for that download will be branches of a single parent. If all such branches are cancelled, or if one is cancelled with opt_deepCancel set, then the download will be cancelled as well.
Arguments:
url : string
The URL of the file to download.
Returns: !goog.async.Deferred  The deferred result blob.
code »
error_(downloadopt_err)
The error callback for all asynchronous operations. Ensures that all stages of a given download are cleaned up, and emits the error event.
Arguments:
download : !goog.net.FileDownloader.Download_
The download object for this download.
opt_err : goog.fs.Error=
The file error object. Only defined if the error was raised by the file API.
code »
fileSuccess_(downloadfile)
The callback called when a file that will be used for saving a file is successfully opened.
Arguments:
download : !goog.net.FileDownloader.Download_
The download object for this download.
file : !goog.fs.FileEntry
The newly-opened file object.
code »
fileWriterSuccess_(downloadwriter)
The callback called when a file writer is succesfully created for writing a file to the filesystem.
Arguments:
download : !goog.net.FileDownloader.Download_
The download object for this download.
writer : !goog.fs.FileWriter
The newly-created file writer object.
code »
freeXhr_(xhr)
Remove all event listeners for an XHR and release it back into the pool.
Arguments:
xhr : !goog.net.XhrIo
The XHR to free.
code »
getDir_(urlbehavior) !goog.async.Deferred
Get the directory for a given URL. If the directory already exists when this is called, it will contain exactly one file: the downloaded file. This not only calls the FileSystem API's getFile method, but attempts to distribute the files so that they don't overload the filesystem. The spec says directories can't contain more than 5000 files (http://www.w3.org/TR/file-system-api/#directories), so this ensures that each file is put into a subdirectory based on its SHA1 hash. All parameters are the same as in the FileSystem API's Entry#getFile method.
Arguments:
url : string
The URL corresponding to the directory to get.
behavior : goog.fs.DirectoryEntry.Behavior
The behavior to pass to the underlying method.
Returns: !goog.async.Deferred  The deferred DirectoryEntry object.
code »
getDownloadedBlob(url) !goog.async.Deferred
Load a downloaded blob from the filesystem. Will fire a deferred error if the given URL has not yet been downloaded.
Arguments:
url : string
The URL of the blob to load.
Returns: !goog.async.Deferred  The deferred Blob object. The callback will be passed the blob. If a file API error occurs while loading the blob, that error will be passed to the errback.
code »
getFile_(url) !goog.async.Deferred
Get the file for a given URL. This will only retrieve files that have already been saved; it shouldn't be used for creating the file in the first place. This is because the filename isn't necessarily determined by the URL, but by the headers of the XHR response.
Arguments:
url : string
The URL corresponding to the file to get.
Returns: !goog.async.Deferred  The deferred FileEntry object.
code »
getLocalUrl(url) !goog.async.Deferred
Get the local filesystem: URL for a downloaded file. This is different from the blob: URL that's available from getDownloadedBlob(). If the end user accesses the filesystem: URL, the resulting file's name will be determined by the download filename as opposed to an arbitrary GUID. In addition, the filesystem: URL is connected to a filesystem location, so if the download is removed then that URL will become invalid. Warning: in Chrome 12, some filesystem: URLs are opened inline. This means that e.g. HTML pages given to the user via filesystem: URLs will be opened and processed by the browser.
Arguments:
url : string
The URL of the file to get the URL of.
Returns: !goog.async.Deferred  The deferred filesystem: URL. The callback will be passed the URL. If a file API error occurs while loading the blob, that error will be passed to the errback.
code »
getName_(xhr) string
Gets the filename specified by the XHR. This first attempts to parse the Content-Disposition header for a filename and, failing that, falls back on deriving the filename from the URL.
Arguments:
xhr : !goog.net.XhrIo
The XHR containing the response headers.
Returns: string  The filename.
code »
gotXhr_(downloadxhr)
The callback called when an XHR becomes available from the XHR pool.
Arguments:
download : !goog.net.FileDownloader.Download_
The download object for this download.
xhr : !goog.net.XhrIo
The XhrIo object for downloading the page.
code »
isDownloaded(url) !goog.async.Deferred
Return (deferred) whether or not a URL has been downloaded. Will fire a deferred error if something goes wrong when determining this.
Arguments:
url : string
The URL to check.
Returns: !goog.async.Deferred  The deferred boolean. The callback will be passed the boolean. If a file API error occurs while checking the existence of the downloaded URL, that error will be passed to the errback.
code »
isDownloading(url) boolean
Returns whether or not there is an active download for a given URL.
Arguments:
url : string
The URL of the download to check.
Returns: boolean  Whether or not there is an active download for the URL.
code »
remove(url) !goog.async.Deferred
Remove a URL from the FileDownloader. This returns a Deferred. If the removal is completed successfully, its callback will be called without any value. If the removal fails, its errback will be called with the goog.fs.Error.
Arguments:
url : string
The URL to remove.
Returns: !goog.async.Deferred  The deferred used for registering callbacks on success or on error.
code »
sanitize_(str) string
Sanitize a string so it can be safely used as a file or directory name for the FileSystem API.
Arguments:
str : string
The string to sanitize.
Returns: string  The sanitized string.
code »
setBlob(urlblobopt_name) !goog.async.Deferred
Save a blob for a given URL. This works just as through the blob were downloaded form that URL, except you specify the blob and no HTTP request is made. If the URL is currently being downloaded, it's indeterminate whether the blob being set or the blob being downloaded will end up in the filesystem. Whichever one doesn't get saved will have an error. To ensure that one or the other takes precedence, use #waitForDownload to allow the download to complete before setting the blob.
Arguments:
url : string
The URL at which to set the blob.
blob : !Blob
The blob to set.
opt_name : string=
The name of the file. If this isn't given, it's determined from the URL.
Returns: !goog.async.Deferred  The deferred used for registering callbacks on success or on error. This can be cancelled just like a #download Deferred. The objects passed to the errback will be goog.net.FileDownloader.Errors.
code »
urlToName_(url) string
Extracts the basename from a URL.
Arguments:
url : string
The URL.
Returns: string  The basename.
code »
waitForDownload(url) !goog.async.Deferred
Return a Deferred that will fire once no download is active for a given URL. If there's no download active for that URL when this is called, the deferred will fire immediately; otherwise, it will fire once the download is complete, whether or not it succeeds.
Arguments:
url : string
The URL of the download to wait for.
Returns: !goog.async.Deferred  The Deferred that will fire when the download is complete.
code »
writeEnd_(download)
The callback called when file writing ends, whether or not it's successful.
Arguments:
download : !goog.net.FileDownloader.Download_
The download object for this download.
code »
xhrSuccess_(download)
The callback called when an XHR succeeds in downloading a remote file.
Arguments:
download : !goog.net.FileDownloader.Download_
The download object for this download.
code »
addOnDisposeCallback(callbackopt_scope)
Invokes a callback function when this object is disposed. Callbacks are invoked in the order in which they were added.
Arguments:
callback : function(this:T):?
The callback function.
opt_scope : T=
An optional scope to call the callback in.
code »
dispose() void
Disposes of the object. If the object hasn't already been disposed of, calls #disposeInternal. Classes that extend goog.Disposable should override #disposeInternal in order to delete references to COM objects, DOM nodes, and other disposable objects. Reentrant.
Returns: void  Nothing.
code »
disposeInternal()
Deletes or nulls out any references to COM objects, DOM nodes, or other disposable objects. Classes that extend goog.Disposable should override this method. Not reentrant. To avoid calling it twice, it must only be called from the subclass' disposeInternal method. Everywhere else the public dispose method must be used. For example:
  mypackage.MyClass = function() {
    mypackage.MyClass.base(this, 'constructor');
    // Constructor logic specific to MyClass.
    ...
  };
  goog.inherits(mypackage.MyClass, goog.Disposable);

  mypackage.MyClass.prototype.disposeInternal = function() {
    // Dispose logic specific to MyClass.
    ...
    // Call superclass's disposeInternal at the end of the subclass's, like
    // in C++, to avoid hard-to-catch issues.
    mypackage.MyClass.base(this, 'disposeInternal');
  };
code »
getDisposed() boolean
Use #isDisposed instead. No description.
Returns: boolean  Whether the object has been disposed of.
code »
isDisposed() boolean
No description.
Returns: boolean  Whether the object has been disposed of.
code »
registerDisposable(disposable)
Associates a disposable object with this object so that they will be disposed together.
Arguments:
disposable : goog.disposable.IDisposable
that will be disposed when this object is disposed.
code »

Instance Properties

constructor :
No description.
Code »
The directory in which the downloaded files are stored.
Code »
downloads_ :
A map from URLs to active downloads running for those URLs.
Code »
eventHandler_ : goog.events.EventHandler
The handler for URL capturing events.
Code »
The pool of XHRs to use for capturing.
Code »
creationStack :
If monitoring the goog.Disposable instances is enabled, stores the creation stack trace of the Disposable instance.
Code »
disposed_ :
Whether the object has been disposed of.
Code »
onDisposeCallbacks_ :
Callbacks to invoke when this object is disposed.
Code »

Static Properties

goog.net.FileDownloader.superClass_ :
No description.
Code »

Package net

Package Reference