Skip to content

Commit

Permalink
feat: Download files offline
Browse files Browse the repository at this point in the history
In cozy/cozy-client#1518 we implemented a new `downloadFile()` that
allow to download files as before in a browser, but will download files
through cozy-intent when hosted in the Flagship app

This commit will replace the old way to download files with the new one
using `downloadFile()`
  • Loading branch information
Ldoppea committed Sep 25, 2024
1 parent 665ba1a commit 2d4b5d1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/components/Actions/Items/download.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const download = ({
label,
icon,
disabled: docs => docs.length === 0,
action: async (docs, { client }) => {
action: async (docs, { client, webviewIntent }) => {
const fromMultiSelection = allMultiSelection.length > 0
const normalizedDocs =
allMultiSelection.length > 0
Expand All @@ -48,7 +48,8 @@ export const download = ({
showAlert,
t,
pushModal,
popModal
popModal,
webviewIntent
})
isMultiSelectionActive && navigate('..')
},
Expand Down
16 changes: 12 additions & 4 deletions src/components/Actions/handleFileDownloading.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { is2SidedFile } from 'src/helpers/is2SidedFile'
* @param {Function} params.showAlert - Function to show an alert
* @param {Function} params.t - Translation function
* @param {boolean} [params.fromMultiSelection] - Whether the action is from a multi-selection
* @param {WebviewService | undefined} [params.webviewIntent] - WebviewIntent used to call methods on FlagshipApp (when hosted in a WebView)
*/
export const handleFileDownloading = async ({
client,
Expand All @@ -21,7 +22,8 @@ export const handleFileDownloading = async ({
pushModal,
popModal,
t,
fromMultiSelection
fromMultiSelection,
webviewIntent
}) => {
if (
filesWithPage.length === 1 &&
Expand All @@ -37,18 +39,24 @@ export const handleFileDownloading = async ({
textAction={t('action.download')}
onClick={selectedChoice => {
const selected = onPickSelectedPage(selectedChoice, file)
downloadFiles({ client, t, filesWithPage: selected, showAlert })
downloadFiles({
client,
t,
filesWithPage: selected,
showAlert,
webviewIntent
})
}}
onClose={popModal}
/>
)
} else {
downloadFiles({ client, t, filesWithPage, showAlert })
downloadFiles({ client, t, filesWithPage, showAlert, webviewIntent })
}
return
}

if (filesWithPage.length > 0) {
downloadFiles({ client, t, filesWithPage, showAlert })
downloadFiles({ client, t, filesWithPage, showAlert, webviewIntent })
}
}
13 changes: 4 additions & 9 deletions src/components/Actions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { handleConflictFilename } from 'src/helpers/handleConflictFilename'

import { isReferencedBy } from 'cozy-client'
import { getDisplayName } from 'cozy-client/dist/models/contact'
import { splitFilename } from 'cozy-client/dist/models/file'
import { downloadFile, splitFilename } from 'cozy-client/dist/models/file'
import { getSharingLink } from 'cozy-client/dist/models/sharing'

export const isAnyFileReferencedBy = (files, doctype) => {
Expand Down Expand Up @@ -200,20 +200,15 @@ export const downloadFiles = async ({
client,
filesWithPage,
showAlert,
t
t,
webviewIntent
}) => {
try {
const fileCollection = client.collection(FILES_DOCTYPE)
if (filesWithPage.length === 1) {
const { file, page } = filesWithPage[0]
if (filesWithPage[0].page === null) {
const filename = file.name
const downloadURL = await fileCollection.getDownloadLinkById(
file.id,
filename
)

return fileCollection.forceFileDownload(`${downloadURL}?Dl=1`, filename)
return await downloadFile({ client, file, webviewIntent })
} else {
const filename = makeFilenameWithPage({ file, page, t })
const bin = await getPdfPage({
Expand Down

0 comments on commit 2d4b5d1

Please sign in to comment.