From 59290513e3456ab30f77bd312c8d457d605c5c72 Mon Sep 17 00:00:00 2001 From: Taylor McKinnon Date: Fri, 22 Mar 2019 11:50:18 -0700 Subject: [PATCH] ft(ZENKO-1616): Update ObjectMD model for azure blob api --- lib/constants.js | 4 ++- lib/models/ObjectMD.js | 56 ++++++++++++++++++++++++++++++++++- tests/unit/models/ObjectMD.js | 10 ++++++- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/lib/constants.js b/lib/constants.js index ecce25a72..a987f0a3b 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -34,7 +34,9 @@ module.exports = { emptyFileMd5: 'd41d8cd98f00b204e9800998ecf8427e', // Version 2 changes the format of the data location property // Version 3 adds the dataStoreName attribute - mdModelVersion: 3, + // Version 4 add the Creation-Time and Content-Language attributes, + // and add support for x-ms-meta-* headers in UserMetadata + mdModelVersion: 4, /* * Splitter is used to build the object name for the overview of a * multipart upload and to build the object names for each part of a diff --git a/lib/models/ObjectMD.js b/lib/models/ObjectMD.js index 6300adbe2..bd341c305 100644 --- a/lib/models/ObjectMD.js +++ b/lib/models/ObjectMD.js @@ -28,9 +28,14 @@ class ObjectMD { } else { this._updateFromParsedJSON(objMd); } + if (!this._data['creation-time']) { + this.setCreationTime(this.getLastModified()); + } } else { // set newly-created object md modified time to current time - this._data['last-modified'] = new Date().toJSON(); + const dt = new Date().toJSON(); + this.setLastModified(dt); + this.setCreationTime(dt); } // set latest md model version now that we ensured // backward-compat conversion @@ -85,6 +90,8 @@ class ObjectMD { 'content-length': 0, 'content-type': '', 'content-md5': '', + 'content-language': '', + 'creation-time': undefined, // simple/no version. will expand once object versioning is // introduced 'x-amz-version-id': 'null', @@ -352,6 +359,50 @@ class ObjectMD { return this._data['content-md5']; } + /** + * Set content-language + * + * @param {string} contentLanguage - content-language + * @return {ObjectMD} itself + */ + setContentLanguage(contentLanguage) { + this._data['content-language'] = contentLanguage; + return this; + } + + /** + * Returns content-language + * + * @return {string} content-language + */ + getContentLanguage() { + return this._data['content-language']; + } + + /** + * Set Creation Date + * + * @param {string} creationTime - Creation Date + * @return {ObjectMD} itself + */ + setCreationTime(creationTime) { + this._data['creation-time'] = creationTime; + return this; + } + + /** + * Returns Creation Date + * + * @return {string} Creation Date + */ + getCreationTime() { + // If creation-time is not set fallback to LastModified + if (!this._data['creation-time']) { + return this.getLastModified(); + } + return this._data['creation-time']; + } + /** * Set version id * @@ -916,6 +967,9 @@ class ObjectMD { Object.keys(metaHeaders).forEach(key => { if (key.startsWith('x-amz-meta-')) { this._data[key] = metaHeaders[key]; + } else if (key.startsWith('x-ms-meta-')) { + const _key = key.replace('x-ms-meta-', 'x-amz-meta-'); + this._data[_key] = metaHeaders[key]; } }); // If a multipart object and the acl is already parsed, we update it diff --git a/tests/unit/models/ObjectMD.js b/tests/unit/models/ObjectMD.js index 1b0d14f7c..8774cc2da 100644 --- a/tests/unit/models/ObjectMD.js +++ b/tests/unit/models/ObjectMD.js @@ -30,6 +30,9 @@ describe('ObjectMD class setters/getters', () => { ['LastModified', new Date().toJSON()], ['ContentMd5', null, ''], ['ContentMd5', 'content-md5'], + ['ContentLanguage', null, ''], + ['ContentLanguage', 'content-language', ''], + ['CreationTime', new Date().toJSON()], ['AmzVersionId', null, 'null'], ['AmzVersionId', 'version-id'], ['AmzServerVersionId', null, ''], @@ -215,8 +218,10 @@ describe('ObjectMD class setters/getters', () => { md.setUserMetadata({ 'x-amz-meta-foo': 'bar', 'x-amz-meta-baz': 'qux', - // this one should be filtered out + // This one should be filtered out 'x-amz-storage-class': 'STANDARD_IA', + // This one should be changed to 'x-amz-meta-foobar' + 'x-ms-meta-foobar': 'bar', // ACLs are updated 'acl': { FULL_CONTROL: ['john'], @@ -225,6 +230,7 @@ describe('ObjectMD class setters/getters', () => { assert.deepStrictEqual(JSON.parse(md.getUserMetadata()), { 'x-amz-meta-foo': 'bar', 'x-amz-meta-baz': 'qux', + 'x-amz-meta-foobar': 'bar', }); assert.deepStrictEqual(md.getAcl(), { FULL_CONTROL: ['john'], @@ -333,6 +339,8 @@ describe('getAttributes static method', () => { 'content-length': true, 'content-type': true, 'content-md5': true, + 'content-language': true, + 'creation-time': true, 'x-amz-version-id': true, 'x-amz-server-version-id': true, 'x-amz-storage-class': true,