Skip to content

Commit

Permalink
feat(nextcloud): Download a file
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed May 23, 2024
1 parent 332ce42 commit df1dc7f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
15 changes: 15 additions & 0 deletions docs/api/cozy-stack-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ query to work</p>
<dd><p>Rules determine the behavior of the sharing when changes are made to the shared document
See <a href="https://docs.cozy.io/en/cozy-stack/sharing-design/#description-of-a-sharing">https://docs.cozy.io/en/cozy-stack/sharing-design/#description-of-a-sharing</a></p>
</dd>
<dt><a href="#forceDownload">forceDownload</a></dt>
<dd><p>Force a download from the given href</p>
</dd>
</dl>

## Functions
Expand Down Expand Up @@ -2200,6 +2203,18 @@ See https://docs.cozy.io/en/cozy-stack/sharing-design/#description-of-a-sharing
| document | [<code>Sharing</code>](#Sharing) | The document to share. Should have and _id and a name |
| sharingType | [<code>SharingType</code>](#SharingType) | The type of the sharing |

<a name="forceDownload"></a>

## forceDownload
Force a download from the given href

**Kind**: global constant

| Param | Type | Description |
| --- | --- | --- |
| href | <code>string</code> | The link to download |
| filename | <code>string</code> | The file name to download |

<a name="getAccessToken"></a>

## getAccessToken() ⇒ <code>string</code>
Expand Down
10 changes: 2 additions & 8 deletions packages/cozy-stack-client/src/FileCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
}

/**
Expand Down
16 changes: 16 additions & 0 deletions packages/cozy-stack-client/src/NextcloudFilesCollection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import DocumentCollection from './DocumentCollection'
import { forceDownload } from './utils'

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

Expand Down Expand Up @@ -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 }
16 changes: 16 additions & 0 deletions packages/cozy-stack-client/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit df1dc7f

Please sign in to comment.