diff --git a/lib/stores/DataStore.js b/lib/stores/DataStore.js index 7119f62a..885ff7a7 100644 --- a/lib/stores/DataStore.js +++ b/lib/stores/DataStore.js @@ -81,7 +81,7 @@ class DataStore extends EventEmitter { */ write(req) { log('[DataStore] write'); - return new Promise((resolve, reject) => { + return new Promise((resolve) => { // Stub resolve for tests const offset = 0; @@ -92,7 +92,7 @@ class DataStore extends EventEmitter { /** * Called in HEAD requests. This method should return the bytes - * writen to the DataStore, for the client to know where to resume + * written to the DataStore, for the client to know where to resume * the upload. * * @param {string} id filename @@ -115,6 +115,9 @@ class DataStore extends EventEmitter { * @return {Object} metadata as key-value pair */ _parseMetadataString(metadata_string) { + if (!metadata_string || !metadata_string.length) { + return {}; + } const kv_pair_list = metadata_string.split(','); return kv_pair_list.reduce((metadata, kv_pair) => { diff --git a/lib/stores/GCSDataStore.js b/lib/stores/GCSDataStore.js index 6008221b..5ac3f943 100644 --- a/lib/stores/GCSDataStore.js +++ b/lib/stores/GCSDataStore.js @@ -94,7 +94,7 @@ class GCSDataStore extends DataStore { const gcs_file = this.bucket.file(file.id); const parsedMetadata = this._parseMetadataString(upload_metadata); const options = { - contentType: parsedMetadata.type.decoded, + contentType: parsedMetadata.filetype && parsedMetadata.filetype.decoded, metadata: { metadata: { upload_length: file.upload_length, @@ -137,7 +137,7 @@ class GCSDataStore extends DataStore { const options = { offset, metadata: { - contentType: parsedMetadata.type.decoded, + contentType: parsedMetadata.filetype && parsedMetadata.filetype.decoded, metadata: { upload_length: data.upload_length, tus_version: TUS_RESUMABLE, diff --git a/test/Test-DataStore.js b/test/Test-DataStore.js index a376faad..3031fa46 100644 --- a/test/Test-DataStore.js +++ b/test/Test-DataStore.js @@ -68,7 +68,7 @@ describe('DataStore', () => { it('must have a _parseMetadataString method', (done) => { datastore.should.have.property('_parseMetadataString'); const uploadMetadata = 'type YXBwbGljYXRpb24vcGRm,name bXktZmlsZS5wZGY=,filetype YXBwbGljYXRpb24vcGRm,filename bXktZmlsZS5wZGY=' - const parsed = datastore._parseMetadataString(uploadMetadata); + let parsed = datastore._parseMetadataString(uploadMetadata); parsed.should.deepEqual({ "filename": { "decoded": "my-file.pdf", @@ -87,6 +87,8 @@ describe('DataStore', () => { "encoded": "YXBwbGljYXRpb24vcGRm" } }) + parsed = datastore._parseMetadataString(null); + parsed.should.deepEqual({}) done(); }); }); diff --git a/test/Test-GCSDataStore.js b/test/Test-GCSDataStore.js index 9e85d05b..2b4ef34b 100644 --- a/test/Test-GCSDataStore.js +++ b/test/Test-GCSDataStore.js @@ -128,9 +128,11 @@ describe('GCSDataStore', () => { }); it('should create a file', (done) => { + const uploadMetadata = 'type YXBwbGljYXRpb24vcGRm,name bXktZmlsZS5wZGY=,filetype YXBwbGljYXRpb24vcGRm,filename bXktZmlsZS5wZGY=' const req = { headers: { 'upload-length': TEST_FILE_SIZE, + 'upload-metadata': uploadMetadata, }, }; server.datastore.create(req) @@ -144,6 +146,7 @@ describe('GCSDataStore', () => { it(`should fire the ${EVENTS.EVENT_FILE_CREATED} event`, (done) => { + const uploadMetadata = 'type YXBwbGljYXRpb24vcGRm,name bXktZmlsZS5wZGY=,filetype YXBwbGljYXRpb24vcGRm,filename bXktZmlsZS5wZGY=' server.datastore.on(EVENTS.EVENT_FILE_CREATED, (event) => { event.should.have.property('file'); assert.equal(event.file instanceof File, true); @@ -153,6 +156,7 @@ describe('GCSDataStore', () => { const req = { headers: { 'upload-length': TEST_FILE_SIZE, + 'upload-metadata': uploadMetadata, }, }; server.datastore.create(req) @@ -199,8 +203,7 @@ describe('GCSDataStore', () => { // TODO: upload this file to the bucket first return server.datastore.getOffset(FILE_ALREADY_IN_BUCKET) .should.be.fulfilledWith({ - size: TEST_FILE_SIZE, - upload_length: TEST_FILE_SIZE, + size: TEST_FILE_SIZE }); }); });