1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/**
* @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { emit } from '@nextcloud/event-bus'
import { Permission, Node, View, FileAction, FileType } from '@nextcloud/files'
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
import axios from '@nextcloud/axios'
import CloseSvg from '@mdi/svg/svg/close.svg?raw'
import NetworkOffSvg from '@mdi/svg/svg/network-off.svg?raw'
import TrashCanSvg from '@mdi/svg/svg/trash-can.svg?raw'
import logger from '../logger.js'
const canUnshareOnly = (nodes: Node[]) => {
return nodes.every(node => node.attributes['is-mount-root'] === true
&& node.attributes['mount-type'] === 'shared')
}
const canDisconnectOnly = (nodes: Node[]) => {
return nodes.every(node => node.attributes['is-mount-root'] === true
&& node.attributes['mount-type'] === 'external')
}
const isMixedUnshareAndDelete = (nodes: Node[]) => {
if (nodes.length === 1) {
return false
}
const hasSharedItems = nodes.some(node => canUnshareOnly([node]))
const hasDeleteItems = nodes.some(node => !canUnshareOnly([node]))
return hasSharedItems && hasDeleteItems
}
const isAllFiles = (nodes: Node[]) => {
return !nodes.some(node => node.type !== FileType.File)
}
const isAllFolders = (nodes: Node[]) => {
return !nodes.some(node => node.type !== FileType.Folder)
}
export const action = new FileAction({
id: 'delete',
displayName(nodes: Node[], view: View) {
/**
* If we're in the trashbin, we can only delete permanently
*/
if (view.id === 'trashbin') {
return t('files', 'Delete permanently')
}
/**
* If we're in the sharing view, we can only unshare
*/
if (isMixedUnshareAndDelete(nodes)) {
return t('files', 'Delete and unshare')
}
/**
* If those nodes are all the root node of a
* share, we can only unshare them.
*/
if (canUnshareOnly(nodes)) {
return n('files', 'Leave this share', 'Leave these shares', nodes.length)
}
/**
* If those nodes are all the root node of an
* external storage, we can only disconnect it.
*/
if (canDisconnectOnly(nodes)) {
return n('files', 'Disconnect storage', 'Disconnect storages', nodes.length)
}
/**
* If we're only selecting files, use proper wording
*/
if (isAllFiles(nodes)) {
return n('files', 'Delete file', 'Delete files', nodes.length)
}
/**
* If we're only selecting folders, use proper wording
*/
if (isAllFolders(nodes)) {
return n('files', 'Delete folder', 'Delete folders', nodes.length)
}
return t('files', 'Delete')
},
iconSvgInline: (nodes: Node[]) => {
if (canUnshareOnly(nodes)) {
return CloseSvg
}
if (canDisconnectOnly(nodes)) {
return NetworkOffSvg
}
return TrashCanSvg
},
enabled(nodes: Node[]) {
return nodes.length > 0 && nodes
.map(node => node.permissions)
.every(permission => (permission & Permission.DELETE) !== 0)
},
async exec(node: Node) {
try {
await axios.delete(node.encodedSource)
// Let's delete even if it's moved to the trashbin
// since it has been removed from the current view
// and changing the view will trigger a reload anyway.
emit('files:node:deleted', node)
return true
} catch (error) {
logger.error('Error while deleting a file', { error, source: node.source, node })
return false
}
},
async execBatch(nodes: Node[], view: View, dir: string) {
return Promise.all(nodes.map(node => this.exec(node, view, dir)))
},
order: 100,
})
|