Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MLIBZ-2575: Calculate file size #313

Merged
merged 5 commits into from
Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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