From fb7087d38a09d13eb2706fe39efc24e75501d075 Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 15 Oct 2024 10:26:07 +0200 Subject: [PATCH 1/4] fix: refs #7229 download file --- src/composables/downloadFile.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/composables/downloadFile.js b/src/composables/downloadFile.js index 12639dcd6..f46697fb1 100644 --- a/src/composables/downloadFile.js +++ b/src/composables/downloadFile.js @@ -1,11 +1,28 @@ import { useSession } from 'src/composables/useSession'; import { getUrl } from './getUrl'; +import axios from 'axios'; +import { exportFile } from 'quasar'; const { getTokenMultimedia } = useSession(); const token = getTokenMultimedia(); export async function downloadFile(id, model = 'dms', urlPath = '/downloadFile', url) { - let appUrl = await getUrl('', 'lilium'); - appUrl = appUrl.replace('/#/', ''); - window.open(url ?? `${appUrl}/api/${model}/${id}${urlPath}?access_token=${token}`); + try { + const appUrl = (await getUrl('', 'lilium')).replace('/#/', ''); + const response = await axios.get( + url ?? `${appUrl}/${model}/${id}${urlPath}?access_token=${token}`, + { responseType: 'blob' } + ); + + const contentDisposition = response.headers['content-disposition']; + const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition); + const filename = + matches != null && matches[1] + ? matches[1].replace(/['"]/g, '') + : 'downloaded-file'; + + exportFile(filename, response.data); + } catch (error) { + console.error('Error downloading the file', error); + } } From 9665a3407f4d218105c21a531382fc3ffaa7e491 Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 19 Nov 2024 15:54:38 +0100 Subject: [PATCH 2/4] fix: refs #7229 remove catch --- src/composables/downloadFile.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/composables/downloadFile.js b/src/composables/downloadFile.js index f46697fb1..ef1a1093e 100644 --- a/src/composables/downloadFile.js +++ b/src/composables/downloadFile.js @@ -7,22 +7,18 @@ const { getTokenMultimedia } = useSession(); const token = getTokenMultimedia(); export async function downloadFile(id, model = 'dms', urlPath = '/downloadFile', url) { - try { - const appUrl = (await getUrl('', 'lilium')).replace('/#/', ''); - const response = await axios.get( - url ?? `${appUrl}/${model}/${id}${urlPath}?access_token=${token}`, - { responseType: 'blob' } - ); + const appUrl = (await getUrl('', 'lilium')).replace('/#/', ''); + const response = await axios.get( + url ?? `${appUrl}/${model}/${id}${urlPath}?access_token=${token}`, + { responseType: 'blob' } + ); - const contentDisposition = response.headers['content-disposition']; - const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition); - const filename = - matches != null && matches[1] - ? matches[1].replace(/['"]/g, '') - : 'downloaded-file'; + const contentDisposition = response.headers['content-disposition']; + const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition); + const filename = + matches != null && matches[1] + ? matches[1].replace(/['"]/g, '') + : 'downloaded-file'; - exportFile(filename, response.data); - } catch (error) { - console.error('Error downloading the file', error); - } + exportFile(filename, response.data); } From ad93e16896018423e72454f1ee8b8bd6feb01f9c Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 20 Nov 2024 13:50:35 +0100 Subject: [PATCH 3/4] fix: refs #7229 url --- src/composables/downloadFile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/composables/downloadFile.js b/src/composables/downloadFile.js index ef1a1093e..4588265a2 100644 --- a/src/composables/downloadFile.js +++ b/src/composables/downloadFile.js @@ -9,7 +9,7 @@ const token = getTokenMultimedia(); export async function downloadFile(id, model = 'dms', urlPath = '/downloadFile', url) { const appUrl = (await getUrl('', 'lilium')).replace('/#/', ''); const response = await axios.get( - url ?? `${appUrl}/${model}/${id}${urlPath}?access_token=${token}`, + url ?? `${appUrl}/api/${model}/${id}${urlPath}?access_token=${token}`, { responseType: 'blob' } ); From 43f94ede6410b4e8ce4de1a1ec0ffa1cf79a8cbb Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 20 Nov 2024 16:43:59 +0100 Subject: [PATCH 4/4] fix: refs #7229 url + test --- src/composables/downloadFile.js | 2 +- .../composables/downloadFile.spec.js | 33 ++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/composables/downloadFile.js b/src/composables/downloadFile.js index 4588265a2..ef1a1093e 100644 --- a/src/composables/downloadFile.js +++ b/src/composables/downloadFile.js @@ -9,7 +9,7 @@ const token = getTokenMultimedia(); export async function downloadFile(id, model = 'dms', urlPath = '/downloadFile', url) { const appUrl = (await getUrl('', 'lilium')).replace('/#/', ''); const response = await axios.get( - url ?? `${appUrl}/api/${model}/${id}${urlPath}?access_token=${token}`, + url ?? `${appUrl}/${model}/${id}${urlPath}?access_token=${token}`, { responseType: 'blob' } ); diff --git a/test/vitest/__tests__/composables/downloadFile.spec.js b/test/vitest/__tests__/composables/downloadFile.spec.js index f611479bf..8d480fcef 100644 --- a/test/vitest/__tests__/composables/downloadFile.spec.js +++ b/test/vitest/__tests__/composables/downloadFile.spec.js @@ -1,25 +1,36 @@ -import { vi, describe, expect, it } from 'vitest'; +import { vi, describe, expect, it, beforeAll, afterAll } from 'vitest'; import { axios } from 'app/test/vitest/helper'; import { downloadFile } from 'src/composables/downloadFile'; import { useSession } from 'src/composables/useSession'; - const session = useSession(); const token = session.getToken(); describe('downloadFile', () => { + const baseUrl = 'http://localhost:9000'; + let defaulCreateObjectURL; + + beforeAll(() => { + defaulCreateObjectURL = window.URL.createObjectURL; + window.URL.createObjectURL = vi.fn(() => 'blob:http://localhost:9000/blob-id'); + }); + + afterAll(() => (window.URL.createObjectURL = defaulCreateObjectURL)); + it('should open a new window to download the file', async () => { - const url = 'http://localhost:9000'; - - vi.spyOn(axios, 'get').mockResolvedValueOnce({ data: url }); - - const mockWindowOpen = vi.spyOn(window, 'open'); + const res = { + data: new Blob(['file content'], { type: 'application/octet-stream' }), + headers: { 'content-disposition': 'attachment; filename="test-file.txt"' }, + }; + vi.spyOn(axios, 'get').mockImplementation((url) => { + if (url == 'Urls/getUrl') return Promise.resolve({ data: baseUrl }); + else if (url.includes('downloadFile')) return Promise.resolve(res); + }); await downloadFile(1); - expect(mockWindowOpen).toHaveBeenCalledWith( - `${url}/api/dms/1/downloadFile?access_token=${token}` + expect(axios.get).toHaveBeenCalledWith( + `${baseUrl}/dms/1/downloadFile?access_token=${token}`, + { responseType: 'blob' } ); - - mockWindowOpen.mockRestore(); }); });