22 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
33 * SPDX-License-Identifier: AGPL-3.0-or-later
44 */
5- import type { ShareAttribute } from '../../../files_sharing/src/sharing'
6-
7- import { FileAction , Permission , Node , FileType , View , DefaultType } from '@nextcloud/files'
5+ import { FileAction , Node , FileType , View , DefaultType } from '@nextcloud/files'
86import { t } from '@nextcloud/l10n'
97import { generateUrl } from '@nextcloud/router'
8+ import { getSharingToken , isPublicShare } from '@nextcloud/sharing/public'
9+ import { basename } from 'path'
10+ import { isDownloadable } from '../utils/permissions'
1011
1112import ArrowDownSvg from '@mdi/svg/svg/arrow-down.svg?raw'
1213
@@ -19,29 +20,23 @@ const triggerDownload = function(url: string) {
1920
2021const downloadNodes = function ( dir : string , nodes : Node [ ] ) {
2122 const secret = Math . random ( ) . toString ( 36 ) . substring ( 2 )
22- const url = generateUrl ( '/apps/files/ajax/download.php?dir={dir}&files={files}&downloadStartSecret={secret}' , {
23- dir,
24- secret,
25- files : JSON . stringify ( nodes . map ( node => node . basename ) ) ,
26- } )
27- triggerDownload ( url )
28- }
29-
30- const isDownloadable = function ( node : Node ) {
31- if ( ( node . permissions & Permission . READ ) === 0 ) {
32- return false
33- }
34-
35- // If the mount type is a share, ensure it got download permissions.
36- if ( node . attributes [ 'mount-type' ] === 'shared' ) {
37- const shareAttributes = JSON . parse ( node . attributes [ 'share-attributes' ] ?? '[]' ) as Array < ShareAttribute >
38- const downloadAttribute = shareAttributes ?. find ?.( ( attribute : { scope : string ; key : string } ) => attribute . scope === 'permissions' && attribute . key === 'download' )
39- if ( downloadAttribute !== undefined && downloadAttribute . value === false ) {
40- return false
41- }
23+ let url : string
24+ if ( isPublicShare ( ) ) {
25+ url = generateUrl ( '/s/{token}/download/{filename}?path={dir}&files={files}&downloadStartSecret={secret}' , {
26+ dir,
27+ secret,
28+ files : JSON . stringify ( nodes . map ( node => node . basename ) ) ,
29+ token : getSharingToken ( ) ,
30+ filename : `${ basename ( dir ) } .zip}` ,
31+ } )
32+ } else {
33+ url = generateUrl ( '/apps/files/ajax/download.php?dir={dir}&files={files}&downloadStartSecret={secret}' , {
34+ dir,
35+ secret,
36+ files : JSON . stringify ( nodes . map ( node => node . basename ) ) ,
37+ } )
4238 }
43-
44- return true
39+ triggerDownload ( url )
4540}
4641
4742export const action = new FileAction ( {
0 commit comments