Skip to content
This repository has been archived by the owner on Feb 13, 2025. It is now read-only.

Commit

Permalink
Merge pull request #313 from Kinvey/MLIBZ-2575
Browse files Browse the repository at this point in the history
MLIBZ-2575: Calculate file size
  • Loading branch information
thomasconner authored Jun 27, 2018
2 parents 300b66b + 980062d commit 3c13cc2
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 21 deletions.
1 change: 0 additions & 1 deletion packages/kinvey-angular-sdk/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export {
DataStoreType,
SyncOperation,
LiveService,
Files,
Log,
Metadata,
Query,
Expand Down
1 change: 0 additions & 1 deletion packages/kinvey-angular2-sdk/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export {
DataStoreType,
SyncOperation,
LiveService,
Files,
Log,
Metadata,
Query,
Expand Down
1 change: 0 additions & 1 deletion packages/kinvey-html5-sdk/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export {
DataStoreType,
SyncOperation,
LiveService,
Files,
Log,
Metadata,
Query,
Expand Down
1 change: 0 additions & 1 deletion packages/kinvey-phonegap-sdk/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export {
DataStoreType,
SyncOperation,
LiveService,
Files,
Log,
Metadata,
Query,
Expand Down
4 changes: 4 additions & 0 deletions src/core/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions src/html5/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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) && !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 || file.length;
return super.upload(file, metadata, options);
}
}

export const Files = new FilesStore();
1 change: 1 addition & 0 deletions src/html5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 1 addition & 1 deletion src/nativescript/filestore/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
16 changes: 0 additions & 16 deletions test/integration/tests/files-common.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down

0 comments on commit 3c13cc2

Please sign in to comment.