diff --git a/packages/cozy-client/src/models/file.js b/packages/cozy-client/src/models/file.js index 50e66f77f9..feaf972b41 100644 --- a/packages/cozy-client/src/models/file.js +++ b/packages/cozy-client/src/models/file.js @@ -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)) { diff --git a/packages/cozy-client/src/models/file.spec.js b/packages/cozy-client/src/models/file.spec.js index 5822047784..f96e72bc89 100644 --- a/packages/cozy-client/src/models/file.spec.js +++ b/packages/cozy-client/src/models/file.spec.js @@ -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 = { @@ -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 = {