From 4eb2c45c33d9ea03d994c283abcf8dc3f74b083d Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 28 Feb 2025 21:50:47 +0100 Subject: fix(files_sharing): ensure downloaded file has the correct filename Single file shares use the share token as source name, so we need to use the displayname. To do so we need to set the download attribute to the displayname of the file to download. Signed-off-by: Ferdinand Thiessen --- apps/files/src/actions/downloadAction.spec.ts | 23 +++++++++++++++++++++-- apps/files/src/actions/downloadAction.ts | 12 +++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) (limited to 'apps') diff --git a/apps/files/src/actions/downloadAction.spec.ts b/apps/files/src/actions/downloadAction.spec.ts index 2d42de5b8b1..8d5612d982b 100644 --- a/apps/files/src/actions/downloadAction.spec.ts +++ b/apps/files/src/actions/downloadAction.spec.ts @@ -105,7 +105,7 @@ describe('Download action execute tests', () => { // Silent action expect(exec).toBe(null) - expect(link.download).toEqual('') + expect(link.download).toBe('foobar.txt') expect(link.href).toEqual('https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt') expect(link.click).toHaveBeenCalledTimes(1) }) @@ -123,7 +123,26 @@ describe('Download action execute tests', () => { // Silent action expect(exec).toStrictEqual([null]) - expect(link.download).toEqual('') + expect(link.download).toEqual('foobar.txt') + expect(link.href).toEqual('https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt') + expect(link.click).toHaveBeenCalledTimes(1) + }) + + test('Download single file with displayname set', async () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + displayname: 'baz.txt', + permissions: Permission.READ, + }) + + const exec = await action.execBatch!([file], view, '/') + + // Silent action + expect(exec).toStrictEqual([null]) + expect(link.download).toEqual('baz.txt') expect(link.href).toEqual('https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt') expect(link.click).toHaveBeenCalledTimes(1) }) diff --git a/apps/files/src/actions/downloadAction.ts b/apps/files/src/actions/downloadAction.ts index b4c04d2970c..32f029d777c 100644 --- a/apps/files/src/actions/downloadAction.ts +++ b/apps/files/src/actions/downloadAction.ts @@ -9,9 +9,15 @@ import { isDownloadable } from '../utils/permissions' import ArrowDownSvg from '@mdi/svg/svg/arrow-down.svg?raw' -const triggerDownload = function(url: string) { +/** + * Trigger downloading a file. + * + * @param url The url of the asset to download + * @param name Optionally the recommended name of the download (browsers might ignore it) + */ +function triggerDownload(url: string, name?: string) { const hiddenElement = document.createElement('a') - hiddenElement.download = '' + hiddenElement.download = name ?? '' hiddenElement.href = url hiddenElement.click() } @@ -43,7 +49,7 @@ const downloadNodes = function(nodes: Node[]) { if (nodes.length === 1) { if (nodes[0].type === FileType.File) { - return triggerDownload(nodes[0].encodedSource) + return triggerDownload(nodes[0].encodedSource, nodes[0].displayname) } else { url = new URL(nodes[0].encodedSource) url.searchParams.append('accept', 'zip') -- cgit v1.2.3