Skip to content

Commit

Permalink
feat: Add forceReplaceFile entry attribute to saveFiles
Browse files Browse the repository at this point in the history
This option will force the replacement of the given file
  • Loading branch information
doubleface authored and doubleface committed Dec 14, 2023
1 parent 7f60245 commit 830906a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/cozy-clisk/src/launcher/saveFiles.js
Expand Up @@ -15,7 +15,7 @@ import { dataUriToArrayBuffer } from '../libs/utils'
* @property {string} [subPath] - subPath of the destination folder path where to put the downloaded file
* @property {import('cozy-client/types/types').IOCozyFile} [existingFile] - already existing file corresponding to the entry
* @property {boolean} [shouldReplace] - Internal result of the shouldReplaceFile function on the entry
* @property {Function} [shouldReplaceFile] - Function which will define if the file should be replaced or not
* @property {boolean} [forceReplaceFile] - should the konnector force the replace of the current file
*/

/**
Expand Down Expand Up @@ -591,11 +591,12 @@ const defaultShouldReplaceFile = (file, entry, options) => {
return result
}
/**
* Determine if we should replace the current file if any
*
* @param {import('cozy-client/types/types').IOCozyFile} file - io.cozy.files document
* @param {saveFilesEntry} entry - saveFiles entry
* @param {saveFileOptions} options - saveFiles options
* @returns boolean
* @returns {boolean} - should we replace the current file
*/
const shouldReplaceFile = function (file, entry, options) {
const isValid = !options.validateFile || options.validateFile(file)
Expand All @@ -607,12 +608,11 @@ const shouldReplaceFile = function (file, entry, options) {
)
throw new Error('BAD_DOWNLOADED_FILE')
}
const shouldReplaceFileFn =
entry.shouldReplaceFile ||
options.shouldReplaceFile ||
defaultShouldReplaceFile

return shouldReplaceFileFn(file, entry, options)
const result =
entry.forceReplaceFile ?? defaultShouldReplaceFile(file, entry, options)

return result
}

/**
Expand Down Expand Up @@ -757,6 +757,7 @@ function sanitizeEntry(entry) {
delete entry.shouldReplaceFile
delete entry.existingFile
delete entry.shouldReplace
delete entry.forceReplaceFile
delete entry.fileAttributes
return entry
}
Expand Down
81 changes: 81 additions & 0 deletions packages/cozy-clisk/src/launcher/saveFiles.spec.js
Expand Up @@ -57,6 +57,87 @@ describe('saveFiles', function () {
}
])
})
it('should download a file if the file is already present and has option forceReplaceFile', async () => {
const fileDocument = {
_id: 'existingid',
_rev: 'existingrev',
_type: 'io.cozy.files',
type: 'file',
data: 'already present file content',
dirId: '/test/folder/path',
name: 'file name.txt',
cozyMetadata: {
sourceAccount: 'testsourceaccount',
sourceAccountIdentifier: 'testsourceaccountidentifier'
},
metadata: {
fileIdAttributes: 'file name.txt'
}
}
const existingFilesIndex = new Map([['file name.txt', fileDocument]])
const client = {
save: jest.fn().mockImplementation(doc => ({
data: { ...doc, _rev: 'newrev' }
})),
collection: () => ({
statByPath: jest.fn().mockImplementation(path => {
return { data: { _id: path } }
})
})
}

const downloadAndFormatFile = jest.fn().mockResolvedValue({
dataUri: 'downloaded file content'
})
dataUriToArrayBuffer.mockImplementation(dataUri => ({
arrayBuffer: dataUri + ' arrayBuffer'
}))

const document = {
forceReplaceFile: true,
fileurl: 'https://myfile.txt',
filename: 'file name.txt'
}
const result = await saveFiles(client, [document], '/test/folder/path', {
manifest: {
slug: 'testslug'
},
sourceAccount: 'testsourceaccount',
sourceAccountIdentifier: 'testsourceaccountidentifier',
fileIdAttributes: ['filename'],
existingFilesIndex,
downloadAndFormatFile,
log: jest.fn()
})

expect(downloadAndFormatFile).toHaveBeenCalledWith(
expect.objectContaining({
fileurl: 'https://myfile.txt',
filename: 'file name.txt'
})
)

const newFileDocument = {
_id: 'existingid',
_rev: 'newrev',
_type: 'io.cozy.files',
type: 'file',
data: 'downloaded file content arrayBuffer',
dirId: '/test/folder/path',
name: 'file name.txt',
sourceAccount: 'testsourceaccount',
sourceAccountIdentifier: 'testsourceaccountidentifier',
metadata: {
fileIdAttributes: 'file name.txt'
}
}
expect(result).toStrictEqual([
{
filename: 'file name.txt',
fileDocument: newFileDocument
}
])
})
it('should download a file with fileurl and without filestream', async () => {
const client = {
save: jest.fn().mockImplementation(doc => ({
Expand Down

0 comments on commit 830906a

Please sign in to comment.