Path: blob/trunk/third_party/closure/goog/fs/blob.js
4079 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Wrappers for the HTML5 File API. These wrappers closely mirror8* the underlying APIs, but use Closure-style events and Deferred return values.9* Their existence also makes it possible to mock the FileSystem API for testing10* in browsers that don't support it natively.11*12* When adding public functions to anything under this namespace, be sure to add13* its mock counterpart to goog.testing.fs.14*/1516goog.provide('goog.fs.blob');17181920/**21* Concatenates one or more values together and converts them to a Blob.22*23* @param {...(string|!Blob|!ArrayBuffer)} var_args The values that will make up24* the resulting blob.25* @return {!Blob} The blob.26*/27goog.fs.blob.getBlob = function(var_args) {28'use strict';29const BlobBuilder = goog.global.BlobBuilder || goog.global.WebKitBlobBuilder;3031if (BlobBuilder !== undefined) {32const bb = new BlobBuilder();33for (let i = 0; i < arguments.length; i++) {34bb.append(arguments[i]);35}36return bb.getBlob();37} else {38return goog.fs.blob.getBlobWithProperties(39Array.prototype.slice.call(arguments));40}41};424344/**45* Creates a blob with the given properties.46* See https://developer.mozilla.org/en-US/docs/Web/API/Blob for more details.47*48* @param {!Array<string|!Blob|!ArrayBuffer>} parts The values that will make up49* the resulting blob (subset supported by both BlobBuilder.append() and50* Blob constructor).51* @param {string=} opt_type The MIME type of the Blob.52* @param {string=} opt_endings Specifies how strings containing newlines are to53* be written out.54* @return {!Blob} The blob.55*/56goog.fs.blob.getBlobWithProperties = function(parts, opt_type, opt_endings) {57'use strict';58const BlobBuilder = goog.global.BlobBuilder || goog.global.WebKitBlobBuilder;5960if (BlobBuilder !== undefined) {61const bb = new BlobBuilder();62for (let i = 0; i < parts.length; i++) {63bb.append(parts[i], opt_endings);64}65return bb.getBlob(opt_type);66} else if (goog.global.Blob !== undefined) {67const properties = {};68if (opt_type) {69properties['type'] = opt_type;70}71if (opt_endings) {72properties['endings'] = opt_endings;73}74return new Blob(parts, properties);75} else {76throw new Error('This browser doesn\'t seem to support creating Blobs');77}78};798081