Compare commits

..

11 Commits

Author SHA1 Message Date
Jorge Penadés 55f9023201 Merge pull request '#7229 download file' (!833) from 7229-fixDownloadFile into dev
gitea/salix-front/pipeline/head This commit looks good Details
Reviewed-on: #833
Reviewed-by: Alex Moreno <alexm@verdnatura.es>
2024-11-22 09:34:31 +00:00
Jorge Penadés d1055ad572 Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix-front into 7229-fixDownloadFile
gitea/salix-front/pipeline/pr-dev This commit looks good Details
2024-11-22 10:24:52 +01:00
Jorge Penadés 9632fb7c75 Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix-front into 7229-fixDownloadFile
gitea/salix-front/pipeline/pr-dev This commit looks good Details
2024-11-20 16:46:58 +01:00
Jorge Penadés 43f94ede64 fix: refs #7229 url + test
gitea/salix-front/pipeline/pr-dev This commit looks good Details
2024-11-20 16:43:59 +01:00
Jorge Penadés ad93e16896 fix: refs #7229 url
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details
2024-11-20 13:50:35 +01:00
Jorge Penadés 0274dfcef2 Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix-front into 7229-fixDownloadFile
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details
2024-11-20 13:14:56 +01:00
Jorge Penadés 9665a3407f fix: refs #7229 remove catch
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details
2024-11-19 15:54:38 +01:00
Jorge Penadés 85aeda337f Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix-front into 7229-fixDownloadFile
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details
2024-11-19 15:49:45 +01:00
Jorge Penadés 53399c8f74 Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix-front into 7229-fixDownloadFile
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details
2024-10-28 16:25:18 +01:00
Jorge Penadés 4e5a4ae7a3 Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix-front into 7229-fixDownloadFile
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details
2024-10-16 11:49:42 +02:00
Jorge Penadés fb7087d38a fix: refs #7229 download file
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details
2024-10-15 10:26:07 +02:00
2 changed files with 38 additions and 14 deletions

View File

@ -1,11 +1,24 @@
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}`);
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);
}

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 { 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();
});
});