You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

trashbin.ts 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /* eslint-disable */
  23. import { getCurrentUser } from '@nextcloud/auth'
  24. import { File, Folder, parseWebdavPermissions } from '@nextcloud/files'
  25. import { generateRemoteUrl } from '@nextcloud/router'
  26. import type { FileStat, ResponseDataDetailed } from 'webdav'
  27. import type { ContentsWithRoot } from '../../../files/src/services/Navigation'
  28. import client, { rootPath } from './client'
  29. const data = `<?xml version="1.0"?>
  30. <d:propfind xmlns:d="DAV:"
  31. xmlns:oc="http://owncloud.org/ns"
  32. xmlns:nc="http://nextcloud.org/ns">
  33. <d:prop>
  34. <nc:trashbin-filename />
  35. <nc:trashbin-deletion-time />
  36. <nc:trashbin-original-location />
  37. <nc:trashbin-title />
  38. <d:getlastmodified />
  39. <d:getetag />
  40. <d:getcontenttype />
  41. <d:resourcetype />
  42. <oc:fileid />
  43. <oc:permissions />
  44. <oc:size />
  45. <d:getcontentlength />
  46. </d:prop>
  47. </d:propfind>`
  48. const resultToNode = function(node: FileStat): File | Folder {
  49. const permissions = parseWebdavPermissions(node.props?.permissions)
  50. const owner = getCurrentUser()?.uid as string
  51. const nodeData = {
  52. id: node.props?.fileid as number || 0,
  53. source: generateRemoteUrl('dav' + rootPath + node.filename),
  54. mtime: new Date(node.lastmod),
  55. mime: node.mime as string,
  56. size: node.props?.size as number || 0,
  57. permissions,
  58. owner,
  59. root: rootPath,
  60. attributes: {
  61. ...node,
  62. ...node.props,
  63. // Override displayed name on the list
  64. displayName: node.props?.['trashbin-filename'],
  65. },
  66. }
  67. return node.type === 'file'
  68. ? new File(nodeData)
  69. : new Folder(nodeData)
  70. }
  71. export default async (path: string = '/'): Promise<ContentsWithRoot> => {
  72. // TODO: use only one request when webdav-client supports it
  73. // @see https://github.com/perry-mitchell/webdav-client/pull/334
  74. const rootResponse = await client.stat(path, {
  75. details: true,
  76. data,
  77. }) as ResponseDataDetailed<FileStat>
  78. const contentsResponse = await client.getDirectoryContents(path, {
  79. details: true,
  80. data,
  81. }) as ResponseDataDetailed<FileStat[]>
  82. return {
  83. folder: resultToNode(rootResponse.data) as Folder,
  84. contents: contentsResponse.data.map(resultToNode),
  85. }
  86. }