feat(files): Allow to download files on public shares

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
Ferdinand Thiessen 2024-08-02 12:32:59 +02:00
parent ada6a61688
commit a84de3c755
No known key found for this signature in database
GPG key ID: 45FAE7268762B400
2 changed files with 36 additions and 17 deletions

View file

@ -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)
}

View file

@ -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) {