TrashbinPlugin.php 3.8 KB

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