1
0

TrashbinPlugin.php 3.7 KB

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