Skip to content

Commit

Permalink
Merge pull request #52 from cozy/upload-moddate
Browse files Browse the repository at this point in the history
Add a lastModifiedDate option to file upload
  • Loading branch information
aenario authored Feb 1, 2017
2 parents e437f1b + 29bf29c commit e5d4ff4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
6 changes: 5 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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()
})
```


Expand Down
22 changes: 16 additions & 6 deletions src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -24,21 +24,32 @@ 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') {
contentType = 'text/plain'
}
}

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) => {
Expand All @@ -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') {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions test/unit/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit e5d4ff4

Please sign in to comment.