Skip to content
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

feat: Use new downloadFile() method from cozy-client #2690

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions react/Viewer/NoViewer/DownloadButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ 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 '../../deprecated/Button'
import { FileDoctype } from '../../proptypes'
import { useI18n } from '../../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 react/Viewer/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 styles from './styles.styl'
import FileImageLoader from '../../FileImageLoader'
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 react/Viewer/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 PrintButton from './PrintButton'
import { ToolbarFilePath } from './ToolbarFilePath'
Expand All @@ -17,7 +19,6 @@ import withBreakpoints from '../../helpers/withBreakpoints'
import { useI18n } from '../../providers/I18n'
import { makeStyles } from '../../styles'
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 react/Viewer/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 react/Viewer/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