Skip to content

Commit

Permalink
feat(cozy-viewer): Use new downloadFile() method from cozy-client
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()`

BREAKING CHANGE: `downloadFile()` method has been removed from
`cozy-viewer/src/helpers.js`. Use the one from
`cozy-client/dist/models/file` instead
  • Loading branch information
Ldoppea committed Sep 24, 2024
1 parent 113b598 commit d654334
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 39 deletions.
7 changes: 4 additions & 3 deletions packages/cozy-viewer/src/NoViewer/DownloadButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import PropTypes from 'prop-types'
import React from 'react'

import { withClient } from 'cozy-client'
import { downloadFile } from 'cozy-client/dist/models/file'
import { useWebviewIntent } from 'cozy-intent'
import Button from 'cozy-ui/transpiled/react/deprecated/Button'
import { FileDoctype } from 'cozy-ui/transpiled/react/proptypes'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

import { downloadFile } from '../helpers'

const DownloadButton = ({ client, file, url }) => {
const { t } = useI18n()
const webviewIntent = useWebviewIntent()

return (
<Button
onClick={() => downloadFile({ client, file, url })}
onClick={() => downloadFile({ client, file, url, webviewIntent })}
label={t('Viewer.download')}
/>
)
Expand Down
7 changes: 5 additions & 2 deletions packages/cozy-viewer/src/ViewersByFile/PdfMobileViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import PropTypes from 'prop-types'
import React, { useState, useEffect, useRef, useCallback } from 'react'

import { useClient } from 'cozy-client'
import { downloadFile } from 'cozy-client/dist/models/file'
import { useWebviewIntent } from 'cozy-intent'
import FileImageLoader from 'cozy-ui/transpiled/react/FileImageLoader'
import Spinner from 'cozy-ui/transpiled/react/Spinner'
import { FileDoctype } from 'cozy-ui/transpiled/react/proptypes'
Expand All @@ -19,6 +21,7 @@ export const PdfMobileViewer = ({ file, url, t, gestures }) => {
const { showAlert } = useAlert()

const client = useClient()
const webviewIntent = useWebviewIntent()

const onImageError = () => {
setLoading(false)
Expand All @@ -32,7 +35,7 @@ export const PdfMobileViewer = ({ file, url, t, gestures }) => {
const handleOnClick = useCallback(
async file => {
try {
await client.collection('io.cozy.files').download(file)
await downloadFile({ client, file, webviewIntent })
} catch (error) {
showAlert({
message: t('Viewer.error.generic'),
Expand All @@ -42,7 +45,7 @@ export const PdfMobileViewer = ({ file, url, t, gestures }) => {
})
}
},
[client, showAlert, t]
[client, showAlert, t, webviewIntent]
)

useEffect(() => {
Expand Down
6 changes: 4 additions & 2 deletions packages/cozy-viewer/src/components/Toolbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import PropTypes from 'prop-types'
import React from 'react'

import { useClient } from 'cozy-client'
import { downloadFile } from 'cozy-client/dist/models/file'
import { useWebviewIntent } from 'cozy-intent'
import Icon from 'cozy-ui/transpiled/react/Icon'
import IconButton from 'cozy-ui/transpiled/react/IconButton'
import DownloadIcon from 'cozy-ui/transpiled/react/Icons/Download'
Expand All @@ -17,7 +19,6 @@ import PrintButton from './PrintButton'
import { ToolbarFilePath } from './ToolbarFilePath'
import styles from './styles.styl'
import { extractChildrenCompByName } from '../Footer/helpers'
import { downloadFile } from '../helpers'
import { useEncrypted } from '../providers/EncryptedProvider'

const useClasses = makeStyles(theme => ({
Expand All @@ -42,6 +43,7 @@ const Toolbar = ({
const client = useClient()
const classes = useClasses()
const { t } = useI18n()
const webviewIntent = useWebviewIntent()

const { url } = useEncrypted()

Expand Down Expand Up @@ -88,7 +90,7 @@ const Toolbar = ({
<IconButton
className="u-white"
aria-label={t('Viewer.download')}
onClick={() => downloadFile({ client, file, url })}
onClick={() => downloadFile({ client, file, url, webviewIntent })}
>
<Icon icon={DownloadIcon} />
</IconButton>
Expand Down
7 changes: 0 additions & 7 deletions packages/cozy-viewer/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ export const isValidForPanel = ({ file }) => {
)
}

export const downloadFile = async ({ client, file, url }) => {
if (isEncrypted(file)) {
return client.collection('io.cozy.files').forceFileDownload(url, file.name)
}
return client.collection('io.cozy.files').download(file)
}

export const isFileEncrypted = file => isEncrypted(file)

export const formatDate = ({ f, lang, date }) => {
Expand Down
25 changes: 0 additions & 25 deletions packages/cozy-viewer/src/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
import { createMockClient } from 'cozy-client'
import {
KNOWN_DATE_METADATA_NAMES,
KNOWN_INFORMATION_METADATA_NAMES
} from 'cozy-client/dist/models/paper'

import {
downloadFile,
getCurrentModel,
buildEditAttributePath,
isEditableAttribute,
removeFilenameFromPath
} from './helpers'

describe('helpers', () => {
describe('download', () => {
const client = new createMockClient({})
const mockDownload = jest.fn()
const mockForceFileDownload = jest.fn()
client.collection = jest.fn(() => ({
download: mockDownload,
forceFileDownload: mockForceFileDownload
}))

it('should call download when file is not encrypted', async () => {
const file = { name: 'toto.txt' }

await downloadFile({ client, file })
expect(mockDownload).toHaveBeenCalledWith(file)
})

it('should call forceFileDownload when file is encrypted', async () => {
const file = { name: 'encrypted-toto.txt', encrypted: true }
const url = 'blob:http://thedecryptedtoto'
await downloadFile({ client, file, url })
expect(mockForceFileDownload).toHaveBeenCalledWith(url, file.name)
})
})
describe('getCurrentModel', () => {
const expected = 'information'
it.each([
Expand Down

0 comments on commit d654334

Please sign in to comment.