From a84de3c755b25bdff0a4131dd552eb78715dd131 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 2 Aug 2024 12:32:59 +0200 Subject: [PATCH] feat(files): Allow to download files on public shares Signed-off-by: Ferdinand Thiessen --- apps/files/src/actions/downloadAction.ts | 23 ++++++++++++---- apps/files/src/components/FileEntryMixin.ts | 30 ++++++++++++--------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/apps/files/src/actions/downloadAction.ts b/apps/files/src/actions/downloadAction.ts index cce1e48579a..f1bcb97e279 100644 --- a/apps/files/src/actions/downloadAction.ts +++ b/apps/files/src/actions/downloadAction.ts @@ -7,6 +7,8 @@ import type { ShareAttribute } from '../../../files_sharing/src/sharing' import { FileAction, Permission, Node, FileType, View, DefaultType } from '@nextcloud/files' import { t } from '@nextcloud/l10n' import { generateUrl } from '@nextcloud/router' +import { getSharingToken, isPublicShare } from '@nextcloud/sharing/public' +import { basename } from 'path' import ArrowDownSvg from '@mdi/svg/svg/arrow-down.svg?raw' @@ -19,11 +21,22 @@ const triggerDownload = function(url: string) { const downloadNodes = function(dir: string, nodes: Node[]) { const secret = Math.random().toString(36).substring(2) - const url = generateUrl('/apps/files/ajax/download.php?dir={dir}&files={files}&downloadStartSecret={secret}', { - dir, - secret, - files: JSON.stringify(nodes.map(node => node.basename)), - }) + let url: string + if (isPublicShare()) { + url = generateUrl('/s/{token}/download/{filename}?path={dir}&files={files}&downloadStartSecret={secret}', { + dir, + secret, + files: JSON.stringify(nodes.map(node => node.basename)), + token: getSharingToken(), + filename: `${basename(dir)}.zip}`, + }) + } else { + url = generateUrl('/apps/files/ajax/download.php?dir={dir}&files={files}&downloadStartSecret={secret}', { + dir, + secret, + files: JSON.stringify(nodes.map(node => node.basename)), + }) + } triggerDownload(url) } diff --git a/apps/files/src/components/FileEntryMixin.ts b/apps/files/src/components/FileEntryMixin.ts index f2a6f89cf22..73c6ce19387 100644 --- a/apps/files/src/components/FileEntryMixin.ts +++ b/apps/files/src/components/FileEntryMixin.ts @@ -10,6 +10,7 @@ import { showError } from '@nextcloud/dialogs' import { FileType, Permission, Folder, File as NcFile, NodeStatus, Node, getFileActions } from '@nextcloud/files' import { translate as t } from '@nextcloud/l10n' import { generateUrl } from '@nextcloud/router' +import { isPublicShare } from '@nextcloud/sharing/public' import { vOnClickOutside } from '@vueuse/components' import { extname } from 'path' import Vue, { defineComponent } from 'vue' @@ -281,21 +282,26 @@ export default defineComponent({ } // if ctrl+click or middle mouse button, open in new tab - if (event.ctrlKey || event.metaKey || event.button === 1) { - event.preventDefault() - window.open(generateUrl('/f/{fileId}', { fileId: this.fileid })) - return false - } - - if (this.defaultFileAction) { + // also if there is no default action use this as a fallback + const metaKeyPressed = event.ctrlKey || event.metaKey || event.button === 1 + if (metaKeyPressed || !this.defaultFileAction) { event.preventDefault() event.stopPropagation() - // Execute the first default action if any - this.defaultFileAction.exec(this.source, this.currentView, this.currentDir) - } else { - // fallback to open in current tab - window.open(generateUrl('/f/{fileId}', { fileId: this.fileid }), '_self') + let url: string + if (isPublicShare()) { + url = this.source.encodedSource + } else { + url = generateUrl('/f/{fileId}', { fileId: this.fileid }) + } + window.open(url, metaKeyPressed ? '_self' : undefined) + return } + + // every special case handled so just execute the default action + event.preventDefault() + event.stopPropagation() + // Execute the first default action if any + this.defaultFileAction.exec(this.source, this.currentView, this.currentDir) }, openDetailsIfAvailable(event) { -- 2.39.5