TrashbinPlugin.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Trashbin\Sabre;
  8. use OCA\DAV\Connector\Sabre\FilesPlugin;
  9. use OCA\Files_Trashbin\Trash\ITrashItem;
  10. use OCP\IPreview;
  11. use Sabre\DAV\INode;
  12. use Sabre\DAV\PropFind;
  13. use Sabre\DAV\Server;
  14. use Sabre\DAV\ServerPlugin;
  15. use Sabre\HTTP\RequestInterface;
  16. use Sabre\HTTP\ResponseInterface;
  17. class TrashbinPlugin extends ServerPlugin {
  18. public const TRASHBIN_FILENAME = '{http://nextcloud.org/ns}trashbin-filename';
  19. public const TRASHBIN_ORIGINAL_LOCATION = '{http://nextcloud.org/ns}trashbin-original-location';
  20. public const TRASHBIN_DELETION_TIME = '{http://nextcloud.org/ns}trashbin-deletion-time';
  21. public const TRASHBIN_TITLE = '{http://nextcloud.org/ns}trashbin-title';
  22. public const TRASHBIN_DELETED_BY_ID = '{http://nextcloud.org/ns}trashbin-deleted-by-id';
  23. public const TRASHBIN_DELETED_BY_DISPLAY_NAME = '{http://nextcloud.org/ns}trashbin-deleted-by-display-name';
  24. public const TRASHBIN_BACKEND = '{http://nextcloud.org/ns}trashbin-backend';
  25. /** @var Server */
  26. private $server;
  27. public function __construct(
  28. private IPreview $previewManager,
  29. ) {
  30. }
  31. public function initialize(Server $server) {
  32. $this->server = $server;
  33. $this->server->on('propFind', [$this, 'propFind']);
  34. $this->server->on('afterMethod:GET', [$this,'httpGet']);
  35. }
  36. public function propFind(PropFind $propFind, INode $node) {
  37. if (!($node instanceof ITrash)) {
  38. return;
  39. }
  40. $propFind->handle(self::TRASHBIN_FILENAME, function () use ($node) {
  41. return $node->getFilename();
  42. });
  43. $propFind->handle(self::TRASHBIN_ORIGINAL_LOCATION, function () use ($node) {
  44. return $node->getOriginalLocation();
  45. });
  46. $propFind->handle(self::TRASHBIN_TITLE, function () use ($node) {
  47. return $node->getTitle();
  48. });
  49. $propFind->handle(self::TRASHBIN_DELETION_TIME, function () use ($node) {
  50. return $node->getDeletionTime();
  51. });
  52. $propFind->handle(self::TRASHBIN_DELETED_BY_ID, function () use ($node) {
  53. return $node->getDeletedBy()?->getUID();
  54. });
  55. $propFind->handle(self::TRASHBIN_DELETED_BY_DISPLAY_NAME, function () use ($node) {
  56. return $node->getDeletedBy()?->getDisplayName();
  57. });
  58. // Pass the real filename as the DAV display name
  59. $propFind->handle(FilesPlugin::DISPLAYNAME_PROPERTYNAME, function () use ($node) {
  60. return $node->getFilename();
  61. });
  62. $propFind->handle(FilesPlugin::SIZE_PROPERTYNAME, function () use ($node) {
  63. return $node->getSize();
  64. });
  65. $propFind->handle(FilesPlugin::FILEID_PROPERTYNAME, function () use ($node) {
  66. return $node->getFileId();
  67. });
  68. $propFind->handle(FilesPlugin::PERMISSIONS_PROPERTYNAME, function () {
  69. return 'GD'; // read + delete
  70. });
  71. $propFind->handle(FilesPlugin::GETETAG_PROPERTYNAME, function () use ($node) {
  72. // add fake etag, it is only needed to identify the preview image
  73. return $node->getLastModified();
  74. });
  75. $propFind->handle(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) {
  76. // add fake etag, it is only needed to identify the preview image
  77. return $node->getFileId();
  78. });
  79. $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
  80. return $this->previewManager->isAvailable($node->getFileInfo());
  81. });
  82. $propFind->handle(FilesPlugin::MOUNT_TYPE_PROPERTYNAME, function () {
  83. return '';
  84. });
  85. $propFind->handle(self::TRASHBIN_BACKEND, function () use ($node) {
  86. $fileInfo = $node->getFileInfo();
  87. if (!($fileInfo instanceof ITrashItem)) {
  88. return '';
  89. }
  90. return $fileInfo->getTrashBackend()::class;
  91. });
  92. }
  93. /**
  94. * Set real filename on trashbin download
  95. *
  96. * @param RequestInterface $request
  97. * @param ResponseInterface $response
  98. */
  99. public function httpGet(RequestInterface $request, ResponseInterface $response): void {
  100. $path = $request->getPath();
  101. $node = $this->server->tree->getNodeForPath($path);
  102. if ($node instanceof ITrash) {
  103. $response->addHeader('Content-Disposition', 'attachment; filename="' . $node->getFilename() . '"');
  104. }
  105. }
  106. }