diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2025-02-28 21:50:47 +0100 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2025-03-05 08:31:25 +0100 |
commit | 4eb2c45c33d9ea03d994c283abcf8dc3f74b083d (patch) | |
tree | e9f9a90ee4d919edcece11138f93ba1a3a96ac41 /apps | |
parent | a4760ef906ba897f19669898466bdb5c48703ec0 (diff) | |
download | nextcloud-server-4eb2c45c33d9ea03d994c283abcf8dc3f74b083d.tar.gz nextcloud-server-4eb2c45c33d9ea03d994c283abcf8dc3f74b083d.zip |
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 <opensource@fthiessen.de>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files/src/actions/downloadAction.spec.ts | 23 | ||||
-rw-r--r-- | apps/files/src/actions/downloadAction.ts | 12 |
2 files changed, 30 insertions, 5 deletions
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') |