Skip to content

Commit

Permalink
fix: Check for Flagship's downloadFile method support before calling it
Browse files Browse the repository at this point in the history
Previous implementation would not be backward compatible with older
Flagship app versions

By checking for method availability, we ensure the code is called only
on the correct Flagship app versions, otherwise the old process is
called

Related PR: #1518
  • Loading branch information
Ldoppea committed Sep 13, 2024
1 parent e800bbf commit ff16b46
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/cozy-client/src/models/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,12 @@ export const downloadFile = async ({ client, file, url, webviewIntent }) => {
const filesCollection = client.collection(DOCTYPE_FILES)

if (isFlagshipApp() && webviewIntent && !isEncrypted(file)) {
return await webviewIntent.call('downloadFile', file)
const isFlagshipDownloadAvailable =
(await webviewIntent?.call('isAvailable', 'downloadFile')) ?? false

if (isFlagshipDownloadAvailable) {
return await webviewIntent.call('downloadFile', file)
}
}

if (isEncrypted(file)) {
Expand Down
35 changes: 34 additions & 1 deletion packages/cozy-client/src/models/file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ describe('downloadFile', () => {
it('should handle download in Flagship app', async () => {
isFlagshipApp.mockReturnValue(true)
const webviewIntent = {
call: jest.fn()
call: jest.fn().mockResolvedValue(true)
}

const file = {
Expand All @@ -911,9 +911,42 @@ describe('downloadFile', () => {
})

expect(downloadFromCozySpy).not.toHaveBeenCalled()
expect(webviewIntent.call).toHaveBeenCalledWith(
'isAvailable',
'downloadFile'
)
expect(webviewIntent.call).toHaveBeenCalledWith('downloadFile', file)
})

it('should download files from web page in old Flagship app versions', async () => {
isFlagshipApp.mockReturnValue(true)
const webviewIntent = {
call: jest.fn().mockResolvedValue(false) // `isAvailable` returns `false` when not implemented
}

const file = {
_id: 'SOME_FILE_ID',
_type: 'io.cozy.file',
name: 'SOME_FILE_NAME'
}

await fileModel.downloadFile({
// @ts-ignore
client: cozyClient,
// @ts-ignore
file,
// @ts-ignore
webviewIntent
})

expect(downloadFromCozySpy).toHaveBeenCalled()
expect(webviewIntent.call).toHaveBeenCalledWith(
'isAvailable',
'downloadFile'
)
expect(webviewIntent.call).not.toHaveBeenCalledWith('downloadFile', file)
})

it('should download encrypted files from web page as this is not supported yet by Flagship app', async () => {
isFlagshipApp.mockReturnValue(true)
const webviewIntent = {
Expand Down

0 comments on commit ff16b46

Please sign in to comment.