Skip to content

Commit

Permalink
feat: Add a collection to access files into a Nextcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed May 23, 2024
1 parent 0fc6568 commit 332ce42
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/cozy-stack-client/src/CozyStackClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import MicroEE from 'microee'
import errors, { FetchError } from './errors'
import logger from './logger'
import PromiseCache from './promise-cache'
import {
NEXTCLOUD_FILES_DOCTYPE,
NextcloudFilesCollection
} from './NextcloudFilesCollection'

const normalizeUri = uriArg => {
let uri = uriArg
Expand Down Expand Up @@ -100,6 +104,8 @@ class CozyStackClient {
return new ShortcutsCollection(this)
case APPS_REGISTRY_DOCTYPE:
return new AppsRegistryCollection(this)
case NEXTCLOUD_FILES_DOCTYPE:
return new NextcloudFilesCollection(this)
default:
return new DocumentCollection(doctype, this)
}
Expand Down
46 changes: 46 additions & 0 deletions packages/cozy-stack-client/src/NextcloudFilesCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import DocumentCollection from './DocumentCollection'

const NEXTCLOUD_FILES_DOCTYPE = 'io.cozy.remote.nextcloud.files'

const normalizeDoc = DocumentCollection.normalizeDoctypeJsonApi(
NEXTCLOUD_FILES_DOCTYPE
)
const normalizeNextcloudFile = (sourceAccount, path) => file => {
const extendedAttributes = {
...file.attributes,
path: `${path}${path.endsWith('/') ? '' : '/'}${file.attributes.name}`,
cozyMetadata: {
...file.attributes.cozyMetadata,
sourceAccount
}
}

return {
...normalizeDoc(file, NEXTCLOUD_FILES_DOCTYPE),
...extendedAttributes
}
}

class NextcloudFilesCollection extends DocumentCollection {
constructor(stackClient) {
super(NEXTCLOUD_FILES_DOCTYPE, stackClient)
}

async find(selector) {
if (selector.sourceAccount && selector.path) {
const resp = await this.stackClient.fetchJSON(
'GET',
`/remote/nextcloud/${selector.sourceAccount}/${selector.path}`
)

return {
data: resp.data.map(
normalizeNextcloudFile(selector.sourceAccount, selector.path)
)
}
}
throw new Error('Not implemented')
}
}

export { NextcloudFilesCollection, NEXTCLOUD_FILES_DOCTYPE }

0 comments on commit 332ce42

Please sign in to comment.