restoreAction.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 { generateRemoteUrl } from '@nextcloud/router'
  24. import { getCurrentUser } from '@nextcloud/auth'
  25. import { Permission, Node, View, registerFileAction, FileAction } from '@nextcloud/files'
  26. import { translate as t } from '@nextcloud/l10n'
  27. import axios from '@nextcloud/axios'
  28. import History from '@mdi/svg/svg/history.svg?raw'
  29. import logger from '../../../files/src/logger.js'
  30. import { encodePath } from '@nextcloud/paths'
  31. registerFileAction(new FileAction({
  32. id: 'restore',
  33. displayName() {
  34. return t('files_trashbin', 'Restore')
  35. },
  36. iconSvgInline: () => History,
  37. enabled(nodes: Node[], view) {
  38. // Only available in the trashbin view
  39. if (view.id !== 'trashbin') {
  40. return false
  41. }
  42. // Only available if all nodes have read permission
  43. return nodes.length > 0 && nodes
  44. .map(node => node.permissions)
  45. .every(permission => (permission & Permission.READ) !== 0)
  46. },
  47. async exec(node: Node) {
  48. try {
  49. const destination = generateRemoteUrl(encodePath(`dav/trashbin/${getCurrentUser()?.uid}/restore/${node.basename}`))
  50. await axios({
  51. method: 'MOVE',
  52. url: node.encodedSource,
  53. headers: {
  54. destination,
  55. },
  56. })
  57. // Let's pretend the file is deleted since
  58. // we don't know the restored location
  59. emit('files:node:deleted', node)
  60. return true
  61. } catch (error) {
  62. logger.error(error)
  63. return false
  64. }
  65. },
  66. async execBatch(nodes: Node[], view: View, dir: string) {
  67. return Promise.all(nodes.map(node => this.exec(node, view, dir)))
  68. },
  69. order: 1,
  70. inline: () => true,
  71. }))