Skip to content

Commit

Permalink
GSC Datastore: Add support for contentType property
Browse files Browse the repository at this point in the history
close tus#57
  • Loading branch information
tkrugg committed Oct 7, 2018
1 parent 2b2c040 commit 871bc2c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
21 changes: 21 additions & 0 deletions lib/stores/DataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ class DataStore extends EventEmitter {
return resolve({ size: 0, upload_length: 1 });
});
}

/**
* Parses the Base64 encoded metadata received from the client.
*
* @param {String} metadata_string tus' standard upload metadata
* @return {Object} metadata as key-value pair
*/
_parseMetadataString(metadata_string) {
const kv_pair_list = metadata_string.split(',');

return kv_pair_list.reduce((metadata, kv_pair) => {
const [key, base64_value] = kv_pair.split(' ');

metadata[key] = {
encoded: base64_value,
decoded: Buffer.from(base64_value, 'base64').toString('ascii'),
};

return metadata;
}, {});
}
}

module.exports = DataStore;
4 changes: 4 additions & 0 deletions lib/stores/GCSDataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class GCSDataStore extends DataStore {

const file = new File(file_id, upload_length, upload_defer_length, upload_metadata);
const gcs_file = this.bucket.file(file.id);
const parsedMetadata = this._parseMetadataString(upload_metadata);
const options = {
contentType: parsedMetadata.type.decoded,
metadata: {
metadata: {
upload_length: file.upload_length,
Expand Down Expand Up @@ -131,9 +133,11 @@ class GCSDataStore extends DataStore {
return new Promise((resolve, reject) => {
const file = this.bucket.file(file_id);

const parsedMetadata = this._parseMetadataString(data.upload_metadata);
const options = {
offset,
metadata: {
contentType: parsedMetadata.type.decoded,
metadata: {
upload_length: data.upload_length,
tus_version: TUS_RESUMABLE,
Expand Down
21 changes: 0 additions & 21 deletions lib/stores/S3Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,27 +233,6 @@ class S3Store extends DataStore {
});
}

/**
* Parses the Base64 encoded metadata received from the client.
*
* @param {String} metadata_string tus' standard upload metadata
* @return {Object} metadata as key-value pair
*/
_parseMetadataString(metadata_string) {
const kv_pair_list = metadata_string.split(',');

return kv_pair_list.reduce((metadata, kv_pair) => {
const [key, base64_value] = kv_pair.split(' ');

metadata[key] = {
encoded: base64_value,
decoded: Buffer.from(base64_value, 'base64').toString('ascii'),
};

return metadata;
}, {});
}

/**
* Uploads a part/chunk to S3 from a temporary part file.
*
Expand Down
41 changes: 36 additions & 5 deletions test/Test-DataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,16 @@ describe('DataStore', () => {
done();
});

it('must have a create method', (done) => {
it('must have a create method', () => {
datastore.should.have.property('create');
datastore.create();
done();
const req = {
headers: {
'upload-length': 42,
'upload-defer-length': 0,
'upload-metadata': 'type YXBwbGljYXRpb24vcGRm,name bXktZmlsZS5wZGY=,filetype YXBwbGljYXRpb24vcGRm,filename bXktZmlsZS5wZGY='
}
};
return datastore.create(req);
});

it('must have a write method', (done) => {
Expand All @@ -53,9 +59,34 @@ describe('DataStore', () => {
done();
});

it('must have a getOffset method', (done) => {
it('must have a getOffset method', () => {
datastore.should.have.property('getOffset');
datastore.getOffset();
const id = 42;
return datastore.getOffset(id);
});

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);
parsed.should.deepEqual({
"filename": {
"decoded": "my-file.pdf",
"encoded": "bXktZmlsZS5wZGY="
},
"filetype": {
"decoded": "application/pdf",
"encoded": "YXBwbGljYXRpb24vcGRm"
},
"name": {
"decoded": "my-file.pdf",
"encoded": "bXktZmlsZS5wZGY="
},
"type": {
"decoded": "application/pdf",
"encoded": "YXBwbGljYXRpb24vcGRm"
}
})
done();
});
});

0 comments on commit 871bc2c

Please sign in to comment.