From 4a1e08d8cb6ee04d94c88e412430e9183c4cf4b0 Mon Sep 17 00:00:00 2001 From: Thomas Conner Date: Fri, 22 Jun 2018 10:19:15 -0400 Subject: [PATCH 1/3] MLIBZ-2575: Calculate file size --- packages/kinvey-angular-sdk/src/bundle.js | 1 - packages/kinvey-angular2-sdk/src/bundle.js | 1 - packages/kinvey-html5-sdk/src/bundle.js | 1 - packages/kinvey-phonegap-sdk/src/bundle.js | 1 - src/core/files.js | 4 ++++ src/html5/files.js | 15 +++++++++++++++ src/html5/index.js | 1 + src/nativescript/filestore/common.ts | 2 +- 8 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 src/html5/files.js diff --git a/packages/kinvey-angular-sdk/src/bundle.js b/packages/kinvey-angular-sdk/src/bundle.js index 8457a691f..8e4b4dc4f 100644 --- a/packages/kinvey-angular-sdk/src/bundle.js +++ b/packages/kinvey-angular-sdk/src/bundle.js @@ -25,7 +25,6 @@ export { DataStoreType, SyncOperation, LiveService, - Files, Log, Metadata, Query, diff --git a/packages/kinvey-angular2-sdk/src/bundle.js b/packages/kinvey-angular2-sdk/src/bundle.js index 8457a691f..8e4b4dc4f 100644 --- a/packages/kinvey-angular2-sdk/src/bundle.js +++ b/packages/kinvey-angular2-sdk/src/bundle.js @@ -25,7 +25,6 @@ export { DataStoreType, SyncOperation, LiveService, - Files, Log, Metadata, Query, diff --git a/packages/kinvey-html5-sdk/src/bundle.js b/packages/kinvey-html5-sdk/src/bundle.js index c339129b5..b1592b440 100644 --- a/packages/kinvey-html5-sdk/src/bundle.js +++ b/packages/kinvey-html5-sdk/src/bundle.js @@ -26,7 +26,6 @@ export { DataStoreType, SyncOperation, LiveService, - Files, Log, Metadata, Query, diff --git a/packages/kinvey-phonegap-sdk/src/bundle.js b/packages/kinvey-phonegap-sdk/src/bundle.js index acb5679e8..b5cd41338 100644 --- a/packages/kinvey-phonegap-sdk/src/bundle.js +++ b/packages/kinvey-phonegap-sdk/src/bundle.js @@ -27,7 +27,6 @@ export { DataStoreType, SyncOperation, LiveService, - Files, Log, Metadata, Query, diff --git a/src/core/files.js b/src/core/files.js index ad1ecb25a..46ed4cbe8 100644 --- a/src/core/files.js +++ b/src/core/files.js @@ -288,6 +288,10 @@ export class FileStore { * @private */ saveFileMetadata(options, metadata) { + if (metadata.size <= 0) { + return Promise.reject(new KinveyError('Unable to create a file with a size of 0.', metadata)); + } + const isUpdate = isDefined(metadata._id); const request = new KinveyRequest({ method: isUpdate ? RequestMethod.PUT : RequestMethod.POST, diff --git a/src/html5/files.js b/src/html5/files.js new file mode 100644 index 000000000..88a7054af --- /dev/null +++ b/src/html5/files.js @@ -0,0 +1,15 @@ +import { FileStore as CoreFilesStore } from '../core/files'; +import { KinveyError } from '../core/errors'; + +export class FilesStore extends CoreFilesStore { + upload(file, metadata = {}, options) { + if (!(file instanceof global.Blob)) { + return Promise.reject(new KinveyError('File must be an instance of a Blob.')); + } + + metadata.size = file.size; + return super.upload(file, metadata, options); + } +} + +export const Files = new FilesStore(); diff --git a/src/html5/index.js b/src/html5/index.js index 50678550a..0e3373ebc 100644 --- a/src/html5/index.js +++ b/src/html5/index.js @@ -3,6 +3,7 @@ import { StorageProvider as StorageProviderEnum, repositoryProvider } from '../c import './offline-data-storage'; export * from './kinvey'; +export { Files } from './files'; const supportedStorageProviders = repositoryProvider.getSupportedStorages(); export const StorageProvider = pick(StorageProviderEnum, supportedStorageProviders); diff --git a/src/nativescript/filestore/common.ts b/src/nativescript/filestore/common.ts index e1f71b77e..bcaffffae 100644 --- a/src/nativescript/filestore/common.ts +++ b/src/nativescript/filestore/common.ts @@ -34,7 +34,7 @@ export class CommonFileStore extends CoreFileStore { return Promise.reject(new KinveyError('File does not exist')); } - metadata.size = metadata.size || this.getFileSize(filePath); + metadata.size = this.getFileSize(filePath); return super.upload(filePath, metadata, options); } From b2e1ed4fa756e9692ecb346e150f43444473ac32 Mon Sep 17 00:00:00 2001 From: Thomas Conner Date: Mon, 25 Jun 2018 10:02:31 -0400 Subject: [PATCH 2/3] MLIBZ-2575: Add support to upload a file using just the content as a string --- src/html5/files.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/html5/files.js b/src/html5/files.js index 88a7054af..aaaa602cb 100644 --- a/src/html5/files.js +++ b/src/html5/files.js @@ -1,13 +1,14 @@ +import isString from 'lodash/isString'; import { FileStore as CoreFilesStore } from '../core/files'; import { KinveyError } from '../core/errors'; export class FilesStore extends CoreFilesStore { upload(file, metadata = {}, options) { - if (!(file instanceof global.Blob)) { - return Promise.reject(new KinveyError('File must be an instance of a Blob.')); + if (!(file instanceof global.Blob) && !isString(file)) { + return Promise.reject(new KinveyError('File must be an instance of a Blob or the content of the file as a string.')); } - metadata.size = file.size; + metadata.size = file.size || file.length; return super.upload(file, metadata, options); } } From 8e92f52e345f58eb619b1db41cc73ffc5661b465 Mon Sep 17 00:00:00 2001 From: Thomas Conner Date: Mon, 25 Jun 2018 10:03:24 -0400 Subject: [PATCH 3/3] MLIBZ-2575: Remove unnecesary tests The file size is calculated by the SDK now. Any value passed into upload() will be overrriden. --- test/integration/tests/files-common.tests.js | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/test/integration/tests/files-common.tests.js b/test/integration/tests/files-common.tests.js index 0ea4beba1..c58d11c72 100644 --- a/test/integration/tests/files-common.tests.js +++ b/test/integration/tests/files-common.tests.js @@ -351,22 +351,6 @@ function testFunc() { utilities.testFileUpload(fileToUpload1, metadata, metadata, fileContent1, undefined, done); }) - it('should be able to upload if the submitted size is correct', (done) => { - const metadata = { size: fileContent1.length }; - utilities.testFileUpload(fileToUpload1, metadata, metadata, fileContent1, undefined, done); - }) - - it('should return an error if the submitted size does not match the content length', (done) => { - const metadata = { size: fileContent1.length + 100 }; - Kinvey.Files.upload(fileToUpload1, metadata) - .then(() => done(new Error(shouldNotBeCalledMessage))) - .catch((error) => { - expect(error).to.exist; - done(); - }) - .catch(done); - }) - it('should set _acl', (done) => { const randomId = utilities.randomString(); const acl = new Kinvey.Acl({});