deleteAction.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import { emit } from '@nextcloud/event-bus'
  23. import { Permission, Node, View, FileAction } from '@nextcloud/files'
  24. import { translate as t } from '@nextcloud/l10n'
  25. import axios from '@nextcloud/axios'
  26. import TrashCanSvg from '@mdi/svg/svg/trash-can.svg?raw'
  27. import logger from '../logger.js'
  28. export const action = new FileAction({
  29. id: 'delete',
  30. displayName(nodes: Node[], view: View) {
  31. return view.id === 'trashbin'
  32. ? t('files', 'Delete permanently')
  33. : t('files', 'Delete')
  34. },
  35. iconSvgInline: () => TrashCanSvg,
  36. enabled(nodes: Node[]) {
  37. return nodes.length > 0 && nodes
  38. .map(node => node.permissions)
  39. .every(permission => (permission & Permission.DELETE) !== 0)
  40. },
  41. async exec(node: Node) {
  42. try {
  43. await axios.delete(node.encodedSource)
  44. // Let's delete even if it's moved to the trashbin
  45. // since it has been removed from the current view
  46. // and changing the view will trigger a reload anyway.
  47. emit('files:node:deleted', node)
  48. return true
  49. } catch (error) {
  50. logger.error('Error while deleting a file', { error, source: node.source, node })
  51. return false
  52. }
  53. },
  54. async execBatch(nodes: Node[], view: View, dir: string) {
  55. return Promise.all(nodes.map(node => this.exec(node, view, dir)))
  56. },
  57. order: 100,
  58. })