Skip to content

Commit

Permalink
feat: Add sourceAccount & sourceAccountIdentifier to createFile
Browse files Browse the repository at this point in the history
Konnectors need to pass the sourceAccount & sourceAccountIdentifier
to POST /files/ in order to let the stack set the right
cozyMetadata.
  • Loading branch information
Crash-- committed Jan 31, 2023
1 parent 89faf50 commit 1f8e450
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
16 changes: 16 additions & 0 deletions docs/api/cozy-stack-client.md
Expand Up @@ -164,6 +164,9 @@ Deleted and design docs are filtered by default, thus documents are retrieved in
<dt><a href="#IOCozyFolder">IOCozyFolder</a> : <code>object</code></dt>
<dd><p>Folder</p>
</dd>
<dt><a href="#SpecificFileAttributesForKonnector">SpecificFileAttributesForKonnector</a> : <code>object</code></dt>
<dd><p>Specific file attributes for creation for konnector</p>
</dd>
<dt><a href="#CouchDBViewCursor">CouchDBViewCursor</a> : <code>Array.&lt;string&gt;</code> | <code>string</code></dt>
<dd><p>Cursor used for Mango queries pagination</p>
</dd>
Expand Down Expand Up @@ -2246,6 +2249,19 @@ You should use fetchChangesRaw to have low level control on _changes parameters.
Folder

**Kind**: global typedef
<a name="SpecificFileAttributesForKonnector"></a>

## SpecificFileAttributesForKonnector : <code>object</code>
Specific file attributes for creation for konnector

**Kind**: global typedef
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| sourceAccount | <code>string</code> | the id of the source account used by a konnector |
| sourceAccountIdentifier | <code>string</code> | the unique identifier of the account targeted by the connector |

<a name="CouchDBViewCursor"></a>

## CouchDBViewCursor : <code>Array.&lt;string&gt;</code> \| <code>string</code>
Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-client/src/CozyClient.spec.js
Expand Up @@ -1905,7 +1905,7 @@ describe('file creation', () => {
expect(doc._id).toEqual('1337')
expect(client.stackClient.fetchJSON).toHaveBeenCalledWith(
'POST',
'/files/folder-id?Name=toto.pdf&Type=file&Executable=false&Encrypted=false&MetadataID=&Size=12',
'/files/folder-id?Name=toto.pdf&Type=file&Executable=false&Encrypted=false&MetadataID=&Size=12&SourceAccount=&SourceAccountIdentifier=',
'file-content',
{
headers: { 'Content-Type': 'text/plain', 'Content-Length': '12' },
Expand Down
12 changes: 10 additions & 2 deletions packages/cozy-stack-client/src/FileCollection.js
Expand Up @@ -15,6 +15,11 @@ import logger from './logger'
/**
* @typedef {object} IOCozyFolder Folder
*/
/**
* @typedef {object} SpecificFileAttributesForKonnector Specific file attributes for creation for konnector
* @property {string} sourceAccount the id of the source account used by a konnector
* @property {string} sourceAccountIdentifier the unique identifier of the account targeted by the connector
*/
/**
* Cursor used for Mango queries pagination
*
Expand Down Expand Up @@ -520,9 +525,10 @@ class FileCollection extends DocumentCollection {
/**
* Creates a file
*
*
* @private
* @param {File|Blob|Stream|string|ArrayBuffer} data file to be uploaded
* @param {FileAttributes} params Additional parameters
* @param {FileAttributes & SpecificFileAttributesForKonnector} params Additional parameters
* @param {object} params.options Options to pass to doUpload method (additional headers)
* @throws {Error} - explaining reason why creation failed
*/
Expand All @@ -534,6 +540,8 @@ class FileCollection extends DocumentCollection {
executable = false,
encrypted = false,
metadata,
sourceAccount = '',
sourceAccountIdentifier = '',
...options
} = {}
) {
Expand All @@ -554,7 +562,7 @@ class FileCollection extends DocumentCollection {
if (options.contentLength) {
size = String(options.contentLength)
}
const path = uri`/files/${dirId}?Name=${name}&Type=file&Executable=${executable}&Encrypted=${encrypted}&MetadataID=${metadataId}&Size=${size}`
const path = uri`/files/${dirId}?Name=${name}&Type=file&Executable=${executable}&Encrypted=${encrypted}&MetadataID=${metadataId}&Size=${size}&SourceAccount=${sourceAccount}&SourceAccountIdentifier=${sourceAccountIdentifier}`
return this.doUpload(data, path, options)
}

Expand Down
41 changes: 38 additions & 3 deletions packages/cozy-stack-client/src/FileCollection.spec.js
Expand Up @@ -1137,7 +1137,7 @@ describe('FileCollection', () => {
it('should create a file without metadata', async () => {
const params = { dirId }
const result = await collection.createFile(data, params)
const expectedPath = `/files/${dirId}?Name=mydoc.epub&Type=file&Executable=false&Encrypted=false&MetadataID=&Size=&UpdatedAt=${new Date(
const expectedPath = `/files/${dirId}?Name=mydoc.epub&Type=file&Executable=false&Encrypted=false&MetadataID=&Size=&SourceAccount=&SourceAccountIdentifier=&UpdatedAt=${new Date(
data.lastModified
).toISOString()}&CreatedAt=${new Date(data.lastModified).toISOString()}`
const expectedOptions = {
Expand Down Expand Up @@ -1173,7 +1173,7 @@ describe('FileCollection', () => {
metadata: { type: 'bill' }
}
const result = await collection.createFile(data, params)
const expectedPath = `/files/${dirId}?Name=mydoc.epub&Type=file&Executable=false&Encrypted=false&MetadataID=${metadataId}&Size=&UpdatedAt=${new Date(
const expectedPath = `/files/${dirId}?Name=mydoc.epub&Type=file&Executable=false&Encrypted=false&MetadataID=${metadataId}&Size=&SourceAccount=&SourceAccountIdentifier=&UpdatedAt=${new Date(
data.lastModified
).toISOString()}&CreatedAt=${new Date(data.lastModified).toISOString()}`
const expectedOptions = {
Expand Down Expand Up @@ -1202,7 +1202,42 @@ describe('FileCollection', () => {
const result = await collection.createFile(data, params)
const expectedPath = `/files/${dirId}?Name=mydoc.epub&Type=file&Executable=false&Encrypted=false&MetadataID=&Size=${String(
contentLength
)}&UpdatedAt=${new Date(
)}&SourceAccount=&SourceAccountIdentifier=&UpdatedAt=${new Date(
data.lastModified
).toISOString()}&CreatedAt=${new Date(data.lastModified).toISOString()}`
const expectedOptions = {
headers: {
'Content-Type': 'application/epub+zip',
'Content-Length': String(contentLength)
}
}
expect(client.fetchJSON).toHaveBeenCalledWith(
'POST',
expectedPath,
data,
expectedOptions
)
expect(result).toEqual({
data: {
id: id,
_id: id,
_type: 'io.cozy.files',
dir_id: dirId
}
})
})

it('should send the file konnectors params via querystring', async () => {
const params = {
dirId,
contentLength,
sourceAccount: 'source-account',
sourceAccountIdentifier: 'bob@cozy.localhost'
}
const result = await collection.createFile(data, params)
const expectedPath = `/files/${dirId}?Name=mydoc.epub&Type=file&Executable=false&Encrypted=false&MetadataID=&Size=${String(
contentLength
)}&SourceAccount=source-account&SourceAccountIdentifier=bob%40cozy.localhost&UpdatedAt=${new Date(
data.lastModified
).toISOString()}&CreatedAt=${new Date(data.lastModified).toISOString()}`
const expectedOptions = {
Expand Down

0 comments on commit 1f8e450

Please sign in to comment.