Skip to content

Commit

Permalink
feat(nextcloud): Delete files
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed Jun 17, 2024
1 parent d56d11a commit 896ca16
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/cozy-client/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,8 @@ import { QueryDefinition } from './queries/dsl'

/**
* @typedef {object} NextcloudFile - An io.cozy.remote.nextcloud document after normalization
* @property {string} [_id] - Id of the file
* @property {string} [id] - Id of the file
* @property {string} _id - Id of the file
* @property {string} id - Id of the file
* @property {NextcloudFilesDoctype} _type - Doctype of the folder
* @property {string} name - Name of the file
* @property {string} path - Path to the file
Expand Down
4 changes: 2 additions & 2 deletions packages/cozy-client/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,11 +813,11 @@ export type NextcloudFile = {
/**
* - Id of the file
*/
_id?: string;
_id: string;
/**
* - Id of the file
*/
id?: string;
id: string;
/**
* - Doctype of the folder
*/
Expand Down
46 changes: 40 additions & 6 deletions packages/cozy-stack-client/src/NextcloudFilesCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,43 @@ class NextcloudFilesCollection extends DocumentCollection {
throw new Error('Not implemented')
}

/**
* Move a file to the trash
*
* @param {object} file - The `io.cozy.remote.nextcloud.files` file to move to the trash
* @returns {Promise<object>}
* @throws {FetchError}
*/
async destroy(file) {
const resp = await this.stackClient.fetch(
'DELETE',
`/remote/nextcloud/${file.cozyMetadata.sourceAccount}${encodePath(
file.path
)}`
)
if (resp.status === 204) {
return resp
}
throw new FetchError(resp, resp.json())
}

/**
* Move to the trash multiple files
*
* @param {object[]} files - `io.cozy.remote.nextcloud.files` files to move to the trash
* @returns {Promise<any>}
* @throws {FetchError}
*/
async destroyAll(files) {
for (const file of files) {
await this.destroy(file)
}
}

/**
* Download a file from a Nextcloud server
*
* @param {object} file - The `io.cozy.remote.nextcloud.files` file to download
*/
async download(file) {
const res = await this.stackClient.fetch(
Expand All @@ -70,8 +104,8 @@ class NextcloudFilesCollection extends DocumentCollection {
/**
* Move a file inside a Nextcloud server
*
* @param {object} file - The file to move
* @param {object} to - The destination path
* @param {object} file - The `io.cozy.remote.nextcloud.files` file to move
* @param {object} to - The `io.cozy.remote.nextcloud.files` folder to move the file to
*/
async move(file, to) {
const newPath = joinPath(to.path, file.name)
Expand All @@ -90,8 +124,8 @@ class NextcloudFilesCollection extends DocumentCollection {
/**
* Move a file from a Nextcloud server to a Cozy
*
* @param {object} file - The file to move
* @param {object} to - The destination folder
* @param {object} file - The `io.cozy.remote.nextcloud.files` file to move
* @param {object} to - The `io.cozy.files` folder to move the file to
*
*/
async moveToCozy(file, to) {
Expand All @@ -110,8 +144,8 @@ class NextcloudFilesCollection extends DocumentCollection {
/**
* Move a file from a Cozy to a Nextcloud server
*
* @param {object} file - The destination folder
* @param {object} from - The file to move
* @param {object} file - The `io.cozy.remote.nextcloud.files` folder to move the file to
* @param {object} from - The `io.cozy.files` file to move
* @throws {FetchError}
*/
async moveFromCozy(file, from) {
Expand Down

0 comments on commit 896ca16

Please sign in to comment.