#7229 download file #833

Merged
jorgep merged 10 commits from 7229-fixDownloadFile into dev 2024-11-22 09:34:32 +00:00
2 changed files with 38 additions and 14 deletions

View File

@ -1,11 +1,24 @@
import { useSession } from 'src/composables/useSession'; import { useSession } from 'src/composables/useSession';
import { getUrl } from './getUrl'; import { getUrl } from './getUrl';
import axios from 'axios';
Review

Así en lugar de abrirlo en una ventana de al lado, lo descarga

Así en lugar de abrirlo en una ventana de al lado, lo descarga
import { exportFile } from 'quasar';
const { getTokenMultimedia } = useSession(); const { getTokenMultimedia } = useSession();
const token = getTokenMultimedia(); const token = getTokenMultimedia();
export async function downloadFile(id, model = 'dms', urlPath = '/downloadFile', url) { export async function downloadFile(id, model = 'dms', urlPath = '/downloadFile', url) {
let appUrl = await getUrl('', 'lilium'); const appUrl = (await getUrl('', 'lilium')).replace('/#/', '');
appUrl = appUrl.replace('/#/', ''); const response = await axios.get(
window.open(url ?? `${appUrl}/api/${model}/${id}${urlPath}?access_token=${token}`); 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);
} }

View File

@ -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 { axios } from 'app/test/vitest/helper';
import { downloadFile } from 'src/composables/downloadFile'; import { downloadFile } from 'src/composables/downloadFile';
import { useSession } from 'src/composables/useSession'; import { useSession } from 'src/composables/useSession';
const session = useSession(); const session = useSession();
const token = session.getToken(); const token = session.getToken();
describe('downloadFile', () => { describe('downloadFile', () => {
const baseUrl = 'http://localhost:9000';
let defaulCreateObjectURL;
beforeAll(() => {
Review

Hay que mockearlo, si no da fallo

Hay que mockearlo, si no da fallo
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 () => { it('should open a new window to download the file', async () => {
const url = 'http://localhost:9000'; const res = {
data: new Blob(['file content'], { type: 'application/octet-stream' }),
vi.spyOn(axios, 'get').mockResolvedValueOnce({ data: url }); headers: { 'content-disposition': 'attachment; filename="test-file.txt"' },
};
const mockWindowOpen = vi.spyOn(window, 'open'); 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); await downloadFile(1);
expect(mockWindowOpen).toHaveBeenCalledWith( expect(axios.get).toHaveBeenCalledWith(
`${url}/api/dms/1/downloadFile?access_token=${token}` `${baseUrl}/dms/1/downloadFile?access_token=${token}`,
{ responseType: 'blob' }
); );
mockWindowOpen.mockRestore();
}); });
}); });