1
0

TrashbinPlugin.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. $propFind->handle(FilesPlugin::SIZE_PROPERTYNAME, function () use ($node) {
  60. return $node->getSize();
  61. });
  62. $propFind->handle(FilesPlugin::FILEID_PROPERTYNAME, function () use ($node) {
  63. return $node->getFileId();
  64. });
  65. $propFind->handle(FilesPlugin::PERMISSIONS_PROPERTYNAME, function () {
  66. return 'GD'; // read + delete
  67. });
  68. $propFind->handle(FilesPlugin::GETETAG_PROPERTYNAME, function () use ($node) {
  69. // add fake etag, it is only needed to identify the preview image
  70. return $node->getLastModified();
  71. });
  72. $propFind->handle(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) {
  73. // add fake etag, it is only needed to identify the preview image
  74. return $node->getFileId();
  75. });
  76. $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
  77. return $this->previewManager->isAvailable($node->getFileInfo());
  78. });
  79. $propFind->handle(FilesPlugin::MOUNT_TYPE_PROPERTYNAME, function () {
  80. return '';
  81. });
  82. }
  83. /**
  84. * Set real filename on trashbin download
  85. *
  86. * @param RequestInterface $request
  87. * @param ResponseInterface $response
  88. */
  89. public function httpGet(RequestInterface $request, ResponseInterface $response): void {
  90. $path = $request->getPath();
  91. $node = $this->server->tree->getNodeForPath($path);
  92. if ($node instanceof ITrash) {
  93. $response->addHeader('Content-Disposition', 'attachment; filename="' . $node->getFilename() . '"');
  94. }
  95. }
  96. }