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 135f90be..cd679d6c 100644 --- a/src/files.js +++ b/src/files.js @@ -5,7 +5,7 @@ import { DOCTYPE_FILES } from './doctypes' 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') } @@ -24,11 +24,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') { @@ -36,9 +40,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) => { @@ -52,7 +63,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') { @@ -65,7 +76,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) { @@ -81,8 +92,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..1ddbf404 100644 --- a/test/unit/files.js +++ b/test/unit/files.js @@ -19,11 +19,15 @@ describe('Files', function () { before(mock.mockAPI('UploadFile')) it('should work for supported data types', async function () { - 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 date = new Date('2017-02-01T10:24:42.116Z') + 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' }) - mock.calls('UploadFile').should.have.length(3) + 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') res1.should.have.property('attributes')