trashbin.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 type { FileStat, ResponseDataDetailed } from 'webdav'
  23. import type { ContentsWithRoot } from '@nextcloud/files'
  24. import { File, Folder, davParsePermissions, getDavNameSpaces, getDavProperties } from '@nextcloud/files'
  25. import { generateRemoteUrl, generateUrl } from '@nextcloud/router'
  26. import { getCurrentUser } from '@nextcloud/auth'
  27. import client, { rootPath } from './client'
  28. const data = `<?xml version="1.0"?>
  29. <d:propfind ${getDavNameSpaces()}>
  30. <d:prop>
  31. <nc:trashbin-filename />
  32. <nc:trashbin-deletion-time />
  33. <nc:trashbin-original-location />
  34. <nc:trashbin-title />
  35. ${getDavProperties()}
  36. </d:prop>
  37. </d:propfind>`
  38. const resultToNode = function(node: FileStat): File | Folder {
  39. const permissions = davParsePermissions(node.props?.permissions)
  40. const owner = getCurrentUser()?.uid as string
  41. const previewUrl = generateUrl('/apps/files_trashbin/preview?fileId={fileid}&x=32&y=32', node.props)
  42. const nodeData = {
  43. id: node.props?.fileid as number || 0,
  44. source: generateRemoteUrl('dav' + rootPath + node.filename),
  45. // do not show the mtime column
  46. // mtime: new Date(node.lastmod),
  47. mime: node.mime as string,
  48. size: node.props?.size as number || 0,
  49. permissions,
  50. owner,
  51. root: rootPath,
  52. attributes: {
  53. ...node,
  54. ...node.props,
  55. // Override displayed name on the list
  56. displayName: node.props?.['trashbin-filename'],
  57. previewUrl,
  58. },
  59. }
  60. delete nodeData.attributes.props
  61. return node.type === 'file'
  62. ? new File(nodeData)
  63. : new Folder(nodeData)
  64. }
  65. export const getContents = async (path = '/'): Promise<ContentsWithRoot> => {
  66. // TODO: use only one request when webdav-client supports it
  67. // @see https://github.com/perry-mitchell/webdav-client/pull/334
  68. const rootResponse = await client.stat(path, {
  69. details: true,
  70. data,
  71. }) as ResponseDataDetailed<FileStat>
  72. const contentsResponse = await client.getDirectoryContents(path, {
  73. details: true,
  74. data,
  75. }) as ResponseDataDetailed<FileStat[]>
  76. return {
  77. folder: resultToNode(rootResponse.data) as Folder,
  78. contents: contentsResponse.data.map(resultToNode),
  79. }
  80. }