From df1dc7ff22ffed517e45958e6beb882864e2e83a Mon Sep 17 00:00:00 2001
From: cballevre
Rules determine the behavior of the sharing when changes are made to the shared document See https://docs.cozy.io/en/cozy-stack/sharing-design/#description-of-a-sharing
Force a download from the given href
+Sharing
](#Sharing) | The document to share. Should have and _id and a name |
| sharingType | [SharingType
](#SharingType) | The type of the sharing |
+
+
+## forceDownload
+Force a download from the given href
+
+**Kind**: global constant
+
+| Param | Type | Description |
+| --- | --- | --- |
+| href | string
| The link to download |
+| filename | string
| The file name to download |
+
## getAccessToken() ⇒ string
diff --git a/packages/cozy-stack-client/src/FileCollection.js b/packages/cozy-stack-client/src/FileCollection.js
index 91a507b618..e7f84beb3d 100644
--- a/packages/cozy-stack-client/src/FileCollection.js
+++ b/packages/cozy-stack-client/src/FileCollection.js
@@ -6,7 +6,7 @@ import pick from 'lodash/pick'
import { MangoQueryOptions } from './mangoIndex'
import DocumentCollection, { normalizeDoc } from './DocumentCollection'
-import { uri, slugify, formatBytes } from './utils'
+import { uri, slugify, formatBytes, forceDownload } from './utils'
import { FetchError } from './errors'
import { dontThrowNotFoundError } from './Collection'
import { getIllegalCharacters } from './getIllegalCharacter'
@@ -659,13 +659,7 @@ class FileCollection extends DocumentCollection {
* @param {string} filename - The file name to download
*/
forceFileDownload = (href, filename) => {
- const element = document.createElement('a')
- element.setAttribute('href', href)
- element.setAttribute('download', filename)
- element.style.display = 'none'
- document.body.appendChild(element)
- element.click()
- document.body.removeChild(element)
+ forceDownload(href, filename)
}
/**
diff --git a/packages/cozy-stack-client/src/NextcloudFilesCollection.js b/packages/cozy-stack-client/src/NextcloudFilesCollection.js
index 34278823e4..503c6a5ef9 100644
--- a/packages/cozy-stack-client/src/NextcloudFilesCollection.js
+++ b/packages/cozy-stack-client/src/NextcloudFilesCollection.js
@@ -1,4 +1,5 @@
import DocumentCollection from './DocumentCollection'
+import { forceDownload } from './utils'
const NEXTCLOUD_FILES_DOCTYPE = 'io.cozy.remote.nextcloud.files'
@@ -41,6 +42,21 @@ class NextcloudFilesCollection extends DocumentCollection {
}
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 }
diff --git a/packages/cozy-stack-client/src/utils.js b/packages/cozy-stack-client/src/utils.js
index 954dc58b00..336c4aa5db 100644
--- a/packages/cozy-stack-client/src/utils.js
+++ b/packages/cozy-stack-client/src/utils.js
@@ -69,3 +69,19 @@ export const formatBytes = (bytes, decimals = 2) => {
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
}
+
+/**
+ * Force a download from the given href
+ *
+ * @param {string} href - The link to download
+ * @param {string} filename - The file name to download
+ */
+export const forceDownload = (href, filename) => {
+ const element = document.createElement('a')
+ element.setAttribute('href', href)
+ element.setAttribute('download', filename)
+ element.style.display = 'none'
+ document.body.appendChild(element)
+ element.click()
+ document.body.removeChild(element)
+}