-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[VO-711] feat: Add a collection to access files into a Nextcloud #1471
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import DocumentCollection from './DocumentCollection' | ||
import { forceDownload } from './utils' | ||
|
||
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}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The stack does not return the path like the other files? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nop, but maybe she could do it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like it should for consistency, @nono what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it makes the response bigger, and they are quite easy to compute client-side, so I decided to not include them. |
||
cozyMetadata: { | ||
...file.attributes.cozyMetadata, | ||
sourceAccount | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have to manually set the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is for convenience, because in order to use all the routes in the stack we need to have the Nextcloud path and sourceAccount pair. This is only present in the shortcut describing the Nextcloud. To reuse the same action as the io.cozy.files, I've introduced it into the file so that you only have to pass a file as a parameter as expected |
||
} | ||
} | ||
|
||
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') | ||
} | ||
|
||
/** | ||
* Download a file from a Nextcloud server | ||
* | ||
*/ | ||
async download(file) { | ||
const res = await this.stackClient.fetch( | ||
'GET', | ||
`/remote/nextcloud/${file.cozyMetadata.sourceAccount}/${file.path}?Dl=1` | ||
) | ||
const blob = await res.blob() | ||
const href = URL.createObjectURL(blob) | ||
const filename = file.path.split('/').pop() | ||
forceDownload(href, filename) | ||
} | ||
} | ||
|
||
export { NextcloudFilesCollection, NEXTCLOUD_FILES_DOCTYPE } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks to typing, yet another example, even with JSDoc