From 5c324f0b08340a71015572bbb36de58f3fd35a2e Mon Sep 17 00:00:00 2001 From: popomore Date: Tue, 3 Apr 2018 17:23:41 +0800 Subject: [PATCH] Revert "feat: browser support blob (#409)" This reverts commit e8a78b58c10595e5aae3beb663af6eb609a1fa19. --- example/index.html | 26 +++-------------- example/index.js | 12 -------- lib/browser/managed_upload.js | 21 +++---------- lib/browser/object.js | 17 ++++------- test/browser/browser.test.js | 55 ----------------------------------- 5 files changed, 14 insertions(+), 117 deletions(-) diff --git a/example/index.html b/example/index.html index f3403a3f8..2f3e5a443 100644 --- a/example/index.html +++ b/example/index.html @@ -62,6 +62,7 @@

OSS in Browser

+
@@ -71,26 +72,6 @@

OSS in Browser

- -
-
Upload Blob
-
-
-
- - -
-
- - -
-
- -
-
-
-
-
Upload with base64 img
@@ -123,8 +104,7 @@

OSS in Browser

- - +
Download file
@@ -145,6 +125,8 @@

OSS in Browser

+ +
List files
diff --git a/example/index.js b/example/index.js index 2633e8b64..67fc4803f 100644 --- a/example/index.js +++ b/example/index.js @@ -174,14 +174,6 @@ const uploadContent = function (client) { return client.put(key, new Buffer(content)).then(res => listFiles(client)); }; -const uploadBlob = function (client) { - const content = document.getElementById('file-blob').value.trim(); - const key = document.getElementById('object-key-blob').value.trim() || 'blob'; - console.log(`content => ${key}`); - - return client.put(key, new Blob([content], { type: 'text/plain' })).then(res => listFiles(client)); -} - const downloadFile = function (client) { const object = document.getElementById('dl-object-key').value.trim(); @@ -217,10 +209,6 @@ window.onload = function () { applyTokenDo(uploadContent); }; - document.getElementById('blob-button').onclick = function () { - applyTokenDo(uploadBlob); - }; - document.getElementById('list-files-button').onclick = function () { applyTokenDo(listFiles); }; diff --git a/lib/browser/managed_upload.js b/lib/browser/managed_upload.js index f7c47f0bb..5c0f6fbb6 100644 --- a/lib/browser/managed_upload.js +++ b/lib/browser/managed_upload.js @@ -36,17 +36,8 @@ proto.multipartUpload = function* multipartUpload(name, file, options) { } const minPartSize = 100 * 1024; - - if (!options.mime) { - if (is.file(file)) { - options.mime = mime.getType(path.extname(file.name)); - } else if (is.blob(file)) { - options.mime = file.type; - } else { - options.mime = mime.getType(path.extname(file)); - } - } - + const filename = is.file(file) ? file.name : file; + options.mime = options.mime || mime.getType(path.extname(filename)); options.headers = options.headers || {}; this._convertMetaToHeaders(options.meta, options.headers); @@ -191,17 +182,13 @@ is.file = function file(obj) { return typeof (File) !== 'undefined' && obj instanceof File; }; -is.blob = function (blob) { - return typeof (Blob) !== 'undefined' && blob instanceof Blob; -}; - /** * Get file size */ proto._getFileSize = function* _getFileSize(file) { if (is.buffer(file)) { return file.length; - } else if (is.blob(file) || is.file(file)) { + } else if (is.file(file)) { return file.size; } if (is.string(file)) { const stat = yield this._statFile(file); @@ -274,7 +261,7 @@ WebFileReadStream.prototype._read = function _read(size) { }; proto._createStream = function _createStream(file, start, end) { - if (is.blob(file) || is.file(file)) { + if (is.file(file)) { return new WebFileReadStream(file.slice(start, end)); } // else if (is.string(file)) { diff --git a/lib/browser/object.js b/lib/browser/object.js index 23fd543d0..6ceed25ac 100644 --- a/lib/browser/object.js +++ b/lib/browser/object.js @@ -65,20 +65,15 @@ proto.put = function* put(name, file, options) { options = options || {}; if (is.buffer(file)) { content = file; - } else if (is.blob(file) || is.file(file)) { - if (!options.mime) { - if (is.file(file)) { - options.mime = mime.getType(path.extname(file.name)); - } else { - options.mime = file.type; - } - } - - const stream = this._createStream(file, 0, file.size); + } else if (is.string(file)) { + options.mime = options.mime || mime.getType(path.extname(file)); + const stream = fs.createReadStream(file); options.contentLength = yield this._getFileSize(file); return yield this.putStream(name, stream, options); + } else if (is.readableStream(file)) { + return yield this.putStream(name, file, options); } else { - throw new TypeError('Must provide Buffer/Blob for put.'); + throw new TypeError('Must provide String/Buffer/ReadableStream for put.'); } options.headers = options.headers || {}; diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index 48d0747aa..e169bc184 100644 --- a/test/browser/browser.test.js +++ b/test/browser/browser.test.js @@ -471,28 +471,6 @@ describe('browser', () => { assert.equal(resultGet.content.toString(), body.toString()); - const resultDel = yield this.store.delete(name); - assert.equal(resultDel.res.status, 204); - }); - it('GETs and PUTs blob to a bucket', function* () { - const name = `${prefix}put/test`; - const body = new Blob(['blobBody'], { type: 'text/plain' }); - const resultPut = yield this.store.put(name, body); - assert.equal(resultPut.res.status, 200); - const resultGet = yield this.store.get(name); - assert.equal(resultGet.res.status, 200); - - - yield new Promise((resolve) => { - const fr = new FileReader(); - fr.onload = function () { - console.log(fr.result); - assert.equal(resultGet.content.toString(), fr.result); - resolve(); - }; - fr.readAsText(body, 'utf-8'); - }); - const resultDel = yield this.store.delete(name); assert.equal(resultDel.res.status, 204); }); @@ -784,39 +762,6 @@ describe('browser', () => { assert.deepEqual(md5(object.content), md5(fileBuf)); }); - it('should upload file using multipart upload', function* () { - // create a file with 1M random data - const blobContent = Array(1024 * 1024).fill('a').join(''); - const blob = new Blob([blobContent], { type: 'text/plain' }); - - const name = `${prefix}multipart/upload-blob.js`; - let progress = 0; - const result = yield this.store.multipartUpload(name, blob, { - partSize: 100 * 1024, - progress() { - return function (done) { - progress++; - done(); - }; - }, - }); - sinon.restore(); - assert.equal(result.res.status, 200); - assert.equal(progress, 12); - - const object = yield this.store.get(name); - assert.equal(object.res.status, 200); - - const blobBuf = new Uint8Array(blobContent.length); - for (let i = 0, j = blobContent.length; i < j; ++i) { - blobBuf[i] = blobContent.charCodeAt(i); - } - - assert.equal(object.content.length, blobBuf.length); - // avoid comparing buffers directly for it may hang when generating diffs - assert.deepEqual(md5(object.content), md5(blobBuf)); - }); - it('should return requestId in init, upload part, complete', function* () { const fileContent = Array(1024 * 1024).fill('a').join(''); const file = new File([fileContent], 'multipart-fallback');