From 00b86bcba6cebd5e355a5bcd737bfcf8b341b43a Mon Sep 17 00:00:00 2001 From: Pierre Guilleminot Date: Wed, 1 Feb 2017 11:30:06 +0100 Subject: [PATCH 1/2] Add a lastModifiedDate option to file upload --- docs/README.md | 6 +++++- src/files.js | 22 ++++++++++++++++------ test/unit/files.js | 7 +++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/docs/README.md b/docs/README.md index 58005447..50e75046 100644 --- a/docs/README.md +++ b/docs/README.md @@ -295,6 +295,7 @@ It returns a promise for the document of the file created. * `name`: specify the name of the file. optional for a data of type `File`, type, mandatory otherwise. * `dirID`: specify identifier of the file's directory. if empty, it is the root directory. * `contentType`: specify the content type of the uploaded data. For a `File` type, it is be handled automatically. default: `application/octet-stream`. + * `lastModifiedDate`: a date to specify the last modification time to use for the uploaded file. If the given `data` is a `File` instance, the `lastModifiedDate` is automatically used (not overridden). **Warning**: this API is not v2 compatible. @@ -303,7 +304,10 @@ const created = await cozy.files.create(blob, { name: "filename", dirID: "123456", }) -const fileCreated = await cozy.files.create(fileInput.files[0], { dirID: "" }) +const fileCreated = await cozy.files.create(fileInput.files[0], { + dirID: "", + lastModifiedDate: new Date() +}) ``` diff --git a/src/files.js b/src/files.js index 6554123f..8585f1ad 100644 --- a/src/files.js +++ b/src/files.js @@ -4,7 +4,7 @@ import jsonapi from './jsonapi' const contentTypeOctetStream = 'application/octet-stream' -function doUpload (cozy, data, contentType, method, path) { +function doUpload (cozy, data, method, path, options) { if (!data) { throw new Error('missing data argument') } @@ -23,11 +23,15 @@ function doUpload (cozy, data, contentType, method, path) { throw new Error('invalid data type') } + let {contentType, lastModifiedDate} = options || {} if (!contentType) { if (isBuffer) { contentType = contentTypeOctetStream } else if (isFile) { contentType = data.type || contentTypeOctetStream + if (!lastModifiedDate) { + lastModifiedDate = data.lastModifiedDate + } } else if (isBlob) { contentType = contentTypeOctetStream } else if (typeof data === 'string') { @@ -35,9 +39,16 @@ function doUpload (cozy, data, contentType, method, path) { } } + if (lastModifiedDate && typeof lastModifiedDate === 'string') { + lastModifiedDate = new Date(lastModifiedDate) + } + return cozyFetch(cozy, path, { method: method, - headers: { 'Content-Type': contentType }, + headers: { + 'Content-Type': contentType, + 'Date': lastModifiedDate ? lastModifiedDate.toISOString() : '' + }, body: data }) .then((res) => { @@ -51,7 +62,7 @@ function doUpload (cozy, data, contentType, method, path) { } export function create (cozy, data, options) { - let {name, dirID, contentType} = options || {} + let {name, dirID} = options || {} // handle case where data is a file and contains the name if (!name && typeof data.name === 'string') { @@ -64,7 +75,7 @@ export function create (cozy, data, options) { const path = `/files/${encodeURIComponent(dirID || '')}` const query = `?Name=${encodeURIComponent(name)}&Type=file` - return doUpload(cozy, data, contentType, 'POST', `${path}${query}`) + return doUpload(cozy, data, 'POST', `${path}${query}`, options) } export function createDirectory (cozy, options) { @@ -80,8 +91,7 @@ export function createDirectory (cozy, options) { } export function updateById (cozy, id, data, options) { - const {contentType} = options || {} - return doUpload(cozy, data, contentType, 'PUT', `/files/${encodeURIComponent(id)}`) + return doUpload(cozy, data, 'PUT', `/files/${encodeURIComponent(id)}`, options) } export function updateAttributesById (cozy, id, attrs) { diff --git a/test/unit/files.js b/test/unit/files.js index 5bd1b826..a0630464 100644 --- a/test/unit/files.js +++ b/test/unit/files.js @@ -19,11 +19,14 @@ describe('Files', function () { before(mock.mockAPI('UploadFile')) it('should work for supported data types', async function () { + const date = new Date('2017-02-01T10:24:42.116Z') const res1 = await cozy.files.create('somestringdata', { name: 'foo', dirID: '12345' }) - const res2 = await cozy.files.create(new Uint8Array(10), { name: 'foo', dirID: '12345' }) + const res2 = await cozy.files.create(new Uint8Array(10), { name: 'foo', dirID: '12345', lastModifiedDate: date }) const res3 = await cozy.files.create(new ArrayBuffer(10), { name: 'foo', dirID: '12345' }) - mock.calls('UploadFile').should.have.length(3) + const calls = mock.calls('UploadFile') + calls.should.have.length(3) + calls[1][1].headers['Date'].should.equal(date.toISOString()) mock.lastUrl('UploadFile').should.equal('http://my.cozy.io/files/12345?Name=foo&Type=file') res1.should.have.property('attributes') From 29bf29c2c830413b76ecaf6406945f2e48cfa705 Mon Sep 17 00:00:00 2001 From: Pierre Guilleminot Date: Wed, 1 Feb 2017 11:32:35 +0100 Subject: [PATCH 2/2] Add a test for content-type header --- test/unit/files.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/files.js b/test/unit/files.js index a0630464..1ddbf404 100644 --- a/test/unit/files.js +++ b/test/unit/files.js @@ -20,12 +20,13 @@ describe('Files', function () { it('should work for supported data types', async function () { const date = new Date('2017-02-01T10:24:42.116Z') - const res1 = await cozy.files.create('somestringdata', { name: 'foo', dirID: '12345' }) + const res1 = await cozy.files.create('somestringdata', { name: 'foo', dirID: '12345', contentType: 'text/html' }) const res2 = await cozy.files.create(new Uint8Array(10), { name: 'foo', dirID: '12345', lastModifiedDate: date }) const res3 = await cozy.files.create(new ArrayBuffer(10), { name: 'foo', dirID: '12345' }) const calls = mock.calls('UploadFile') calls.should.have.length(3) + calls[0][1].headers['Content-Type'].should.equal('text/html') calls[1][1].headers['Date'].should.equal(date.toISOString()) mock.lastUrl('UploadFile').should.equal('http://my.cozy.io/files/12345?Name=foo&Type=file')