initial commit
This commit is contained in:
Executable
+79
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright The Closure Library Authors.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Wrappers for the HTML5 File API. These wrappers closely mirror
|
||||
* the underlying APIs, but use Closure-style events and Deferred return values.
|
||||
* Their existence also makes it possible to mock the FileSystem API for testing
|
||||
* in browsers that don't support it natively.
|
||||
*
|
||||
* When adding public functions to anything under this namespace, be sure to add
|
||||
* its mock counterpart to goog.testing.fs.
|
||||
*/
|
||||
|
||||
goog.provide('goog.fs.blob');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Concatenates one or more values together and converts them to a Blob.
|
||||
*
|
||||
* @param {...(string|!Blob|!ArrayBuffer)} var_args The values that will make up
|
||||
* the resulting blob.
|
||||
* @return {!Blob} The blob.
|
||||
*/
|
||||
goog.fs.blob.getBlob = function(var_args) {
|
||||
'use strict';
|
||||
const BlobBuilder = goog.global.BlobBuilder || goog.global.WebKitBlobBuilder;
|
||||
|
||||
if (BlobBuilder !== undefined) {
|
||||
const bb = new BlobBuilder();
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
bb.append(arguments[i]);
|
||||
}
|
||||
return bb.getBlob();
|
||||
} else {
|
||||
return goog.fs.blob.getBlobWithProperties(
|
||||
Array.prototype.slice.call(arguments));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Creates a blob with the given properties.
|
||||
* See https://developer.mozilla.org/en-US/docs/Web/API/Blob for more details.
|
||||
*
|
||||
* @param {!Array<string|!Blob|!ArrayBuffer>} parts The values that will make up
|
||||
* the resulting blob (subset supported by both BlobBuilder.append() and
|
||||
* Blob constructor).
|
||||
* @param {string=} opt_type The MIME type of the Blob.
|
||||
* @param {string=} opt_endings Specifies how strings containing newlines are to
|
||||
* be written out.
|
||||
* @return {!Blob} The blob.
|
||||
*/
|
||||
goog.fs.blob.getBlobWithProperties = function(parts, opt_type, opt_endings) {
|
||||
'use strict';
|
||||
const BlobBuilder = goog.global.BlobBuilder || goog.global.WebKitBlobBuilder;
|
||||
|
||||
if (BlobBuilder !== undefined) {
|
||||
const bb = new BlobBuilder();
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
bb.append(parts[i], opt_endings);
|
||||
}
|
||||
return bb.getBlob(opt_type);
|
||||
} else if (goog.global.Blob !== undefined) {
|
||||
const properties = {};
|
||||
if (opt_type) {
|
||||
properties['type'] = opt_type;
|
||||
}
|
||||
if (opt_endings) {
|
||||
properties['endings'] = opt_endings;
|
||||
}
|
||||
return new Blob(parts, properties);
|
||||
} else {
|
||||
throw new Error('This browser doesn\'t seem to support creating Blobs');
|
||||
}
|
||||
};
|
||||
Executable
+112
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright The Closure Library Authors.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Wrapper for URL and its createObjectUrl and revokeObjectUrl
|
||||
* methods that are part of the HTML5 File API.
|
||||
*/
|
||||
|
||||
goog.provide('goog.fs.url');
|
||||
|
||||
|
||||
/**
|
||||
* Creates a blob URL for a blob object.
|
||||
* Throws an error if the browser does not support Object Urls.
|
||||
*
|
||||
* @param {!File|!Blob|!MediaSource|!MediaStream} obj The object for which
|
||||
* to create the URL.
|
||||
* @return {string} The URL for the object.
|
||||
*/
|
||||
goog.fs.url.createObjectUrl = function(obj) {
|
||||
'use strict';
|
||||
return goog.fs.url.getUrlObject_().createObjectURL(obj);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Revokes a URL created by {@link goog.fs.url.createObjectUrl}.
|
||||
* Throws an error if the browser does not support Object Urls.
|
||||
*
|
||||
* @param {string} url The URL to revoke.
|
||||
* @return {void}
|
||||
*/
|
||||
goog.fs.url.revokeObjectUrl = function(url) {
|
||||
'use strict';
|
||||
goog.fs.url.getUrlObject_().revokeObjectURL(url);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @record
|
||||
* @private
|
||||
*/
|
||||
goog.fs.url.UrlObject_ = function() {};
|
||||
|
||||
/**
|
||||
* @param {!File|!Blob|!MediaSource|!MediaStream} arg
|
||||
* @return {string}
|
||||
*/
|
||||
goog.fs.url.UrlObject_.prototype.createObjectURL = function(arg) {};
|
||||
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {void}
|
||||
*/
|
||||
goog.fs.url.UrlObject_.prototype.revokeObjectURL = function(s) {};
|
||||
|
||||
|
||||
/**
|
||||
* Get the object that has the createObjectURL and revokeObjectURL functions for
|
||||
* this browser.
|
||||
*
|
||||
* @return {!goog.fs.url.UrlObject_} The object for this browser.
|
||||
* @private
|
||||
*/
|
||||
goog.fs.url.getUrlObject_ = function() {
|
||||
'use strict';
|
||||
const urlObject = goog.fs.url.findUrlObject_();
|
||||
if (urlObject != null) {
|
||||
return urlObject;
|
||||
} else {
|
||||
throw new Error('This browser doesn\'t seem to support blob URLs');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Finds the object that has the createObjectURL and revokeObjectURL functions
|
||||
* for this browser.
|
||||
*
|
||||
* @return {?goog.fs.url.UrlObject_} The object for this browser or null if the
|
||||
* browser does not support Object Urls.
|
||||
* @private
|
||||
*/
|
||||
goog.fs.url.findUrlObject_ = function() {
|
||||
'use strict';
|
||||
// This is what the spec says to do
|
||||
// http://dev.w3.org/2006/webapi/FileAPI/#dfn-createObjectURL
|
||||
if (goog.global.URL !== undefined &&
|
||||
goog.global.URL.createObjectURL !== undefined) {
|
||||
return /** @type {!goog.fs.url.UrlObject_} */ (goog.global.URL);
|
||||
// This is what the spec used to say to do
|
||||
} else if (goog.global.createObjectURL !== undefined) {
|
||||
return /** @type {!goog.fs.url.UrlObject_} */ (goog.global);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Checks whether this browser supports Object Urls. If not, calls to
|
||||
* createObjectUrl and revokeObjectUrl will result in an error.
|
||||
*
|
||||
* @return {boolean} True if this browser supports Object Urls.
|
||||
*/
|
||||
goog.fs.url.browserSupportsObjectUrls = function() {
|
||||
'use strict';
|
||||
return goog.fs.url.findUrlObject_() != null;
|
||||
};
|
||||
Reference in New Issue
Block a user