Skip to content

Commit

Permalink
feat: Improve uploadFileWithConflictStrategy
Browse files Browse the repository at this point in the history
Following the development of @zatteo on the
improvement of the "generateNewFileNameOnConflict" function (#1425),
we can take advantage of it to also offer the option in the
"uploadFileWithConflictStrategy" function which uses the latter.
  • Loading branch information
Merkur39 committed Nov 22, 2023
1 parent 8525395 commit 296d899
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
12 changes: 12 additions & 0 deletions docs/api/cozy-client/interfaces/models.file.FileUploadOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

## Properties

### conflictOptions

**conflictOptions**: `ConflictOptions`

Conflict options

*Defined in*

[packages/cozy-client/src/models/file.js:449](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L449)

***

### conflictStrategy

**conflictStrategy**: `string`
Expand Down
16 changes: 8 additions & 8 deletions docs/api/cozy-client/modules/models.file.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Upload a file on a mobile

*Defined in*

[packages/cozy-client/src/models/file.js:552](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L552)
[packages/cozy-client/src/models/file.js:554](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L554)

***

Expand Down Expand Up @@ -86,7 +86,7 @@ file object with path attribute

*Defined in*

[packages/cozy-client/src/models/file.js:598](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L598)
[packages/cozy-client/src/models/file.js:600](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L600)

***

Expand Down Expand Up @@ -302,7 +302,7 @@ The mime-type of the target file, or an empty string is the target is not a file

*Defined in*

[packages/cozy-client/src/models/file.js:578](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L578)
[packages/cozy-client/src/models/file.js:580](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L580)

***

Expand Down Expand Up @@ -346,7 +346,7 @@ Whether the file's metadata attribute exists

*Defined in*

[packages/cozy-client/src/models/file.js:570](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L570)
[packages/cozy-client/src/models/file.js:572](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L572)

***

Expand Down Expand Up @@ -428,7 +428,7 @@ Whether the file is client-side encrypted

*Defined in*

[packages/cozy-client/src/models/file.js:589](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L589)
[packages/cozy-client/src/models/file.js:591](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L591)

***

Expand Down Expand Up @@ -493,7 +493,7 @@ Whether the file is supported by Only Office

*Defined in*

[packages/cozy-client/src/models/file.js:562](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L562)
[packages/cozy-client/src/models/file.js:564](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L564)

***

Expand Down Expand Up @@ -711,7 +711,7 @@ Read a file on a mobile

*Defined in*

[packages/cozy-client/src/models/file.js:505](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L505)
[packages/cozy-client/src/models/file.js:507](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L507)

***

Expand Down Expand Up @@ -818,4 +818,4 @@ If there is a conflict, then we apply the conflict strategy : `erase` or `rename

*Defined in*

[packages/cozy-client/src/models/file.js:466](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L466)
[packages/cozy-client/src/models/file.js:467](https://github.com/cozy/cozy-client/blob/master/packages/cozy-client/src/models/file.js#L467)
6 changes: 4 additions & 2 deletions packages/cozy-client/src/models/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ export const generateFileNameForRevision = (file, revision, f) => {
* @property {object} [metadata] - An object containing the metadata to attach
* @property {string} [contentType] - The file Content-Type
* @property {string} [conflictStrategy] - Erase / rename
* @property {import('../types').ConflictOptions} [conflictOptions] - Conflict options
*/

/**
Expand All @@ -464,7 +465,7 @@ export const generateFileNameForRevision = (file, revision, f) => {
* @param {FileUploadOptions} options - The upload options
*/
export const uploadFileWithConflictStrategy = async (client, file, options) => {
const { name, dirId, conflictStrategy } = options
const { name, dirId, conflictStrategy, conflictOptions } = options

try {
const path = await getFullpath(client, dirId, name)
Expand All @@ -482,7 +483,8 @@ export const uploadFileWithConflictStrategy = async (client, file, options) => {
name,
type: 'file'
})
const newFileName = generateNewFileNameOnConflict(filename) + extension
const newFileName =
generateNewFileNameOnConflict(filename, conflictOptions) + extension
//recall itself with the newFilename.
return uploadFileWithConflictStrategy(client, file, {
...options,
Expand Down
27 changes: 27 additions & 0 deletions packages/cozy-client/src/models/file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,33 @@ describe('File Model', () => {
})
})

it('should rename the file with "_cozyX" if there is a conflict', async () => {
const dirId = 'toto'
//first call we return an existing file => conflict
//second call, we reject as not found
statByPathSpy
.mockReturnValueOnce({
data: {
_id: 'file_id',
_type: 'io.cozy.files'
}
})
.mockRejectedValueOnce(new Error('Not Found'))

const opts = {
name: 'filename',
dirId,
conflictStrategy: 'rename',
conflictOptions: { delimiter: '_cozy' },
contentType: 'image/jpeg'
}
await fileModel.uploadFileWithConflictStrategy(cozyClient, '', opts)
expect(createFileSpy).toHaveBeenCalledWith('', {
...opts,
name: 'filename_cozy1'
})
})

it('should erase the file if there is a conflict', async () => {
const dirId = 'toto'
//first call we return an existing file => conflict
Expand Down
4 changes: 4 additions & 0 deletions packages/cozy-client/types/models/file.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,9 @@ export type FileUploadOptions = {
* - Erase / rename
*/
conflictStrategy?: string;
/**
* - Conflict options
*/
conflictOptions?: import('../types').ConflictOptions;
};
import CozyClient from "../CozyClient";

0 comments on commit 296d899

Please sign in to comment.