TrashbinPlugin.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Files_Trashbin\Sabre;
  27. use OCA\DAV\Connector\Sabre\FilesPlugin;
  28. use OCP\IPreview;
  29. use Sabre\DAV\INode;
  30. use Sabre\DAV\PropFind;
  31. use Sabre\DAV\Server;
  32. use Sabre\DAV\ServerPlugin;
  33. use Sabre\HTTP\RequestInterface;
  34. use Sabre\HTTP\ResponseInterface;
  35. class TrashbinPlugin extends ServerPlugin {
  36. public const TRASHBIN_FILENAME = '{http://nextcloud.org/ns}trashbin-filename';
  37. public const TRASHBIN_ORIGINAL_LOCATION = '{http://nextcloud.org/ns}trashbin-original-location';
  38. public const TRASHBIN_DELETION_TIME = '{http://nextcloud.org/ns}trashbin-deletion-time';
  39. public const TRASHBIN_TITLE = '{http://nextcloud.org/ns}trashbin-title';
  40. public const TRASHBIN_DELETED_BY_ID = '{http://nextcloud.org/ns}trashbin-deleted-by-id';
  41. public const TRASHBIN_DELETED_BY_DISPLAY_NAME = '{http://nextcloud.org/ns}trashbin-deleted-by-display-name';
  42. /** @var Server */
  43. private $server;
  44. /** @var IPreview */
  45. private $previewManager;
  46. public function __construct(
  47. IPreview $previewManager
  48. ) {
  49. $this->previewManager = $previewManager;
  50. }
  51. public function initialize(Server $server) {
  52. $this->server = $server;
  53. $this->server->on('propFind', [$this, 'propFind']);
  54. $this->server->on('afterMethod:GET', [$this,'httpGet']);
  55. }
  56. public function propFind(PropFind $propFind, INode $node) {
  57. if (!($node instanceof ITrash)) {
  58. return;
  59. }
  60. $propFind->handle(self::TRASHBIN_FILENAME, function () use ($node) {
  61. return $node->getFilename();
  62. });
  63. $propFind->handle(self::TRASHBIN_ORIGINAL_LOCATION, function () use ($node) {
  64. return $node->getOriginalLocation();
  65. });
  66. $propFind->handle(self::TRASHBIN_TITLE, function () use ($node) {
  67. return $node->getTitle();
  68. });
  69. $propFind->handle(self::TRASHBIN_DELETION_TIME, function () use ($node) {
  70. return $node->getDeletionTime();
  71. });
  72. $propFind->handle(self::TRASHBIN_DELETED_BY_ID, function () use ($node) {
  73. return $node->getDeletedBy()?->getUID();
  74. });
  75. $propFind->handle(self::TRASHBIN_DELETED_BY_DISPLAY_NAME, function () use ($node) {
  76. return $node->getDeletedBy()?->getDisplayName();
  77. });
  78. $propFind->handle(FilesPlugin::SIZE_PROPERTYNAME, function () use ($node) {
  79. return $node->getSize();
  80. });
  81. $propFind->handle(FilesPlugin::FILEID_PROPERTYNAME, function () use ($node) {
  82. return $node->getFileId();
  83. });
  84. $propFind->handle(FilesPlugin::PERMISSIONS_PROPERTYNAME, function () {
  85. return 'GD'; // read + delete
  86. });
  87. $propFind->handle(FilesPlugin::GETETAG_PROPERTYNAME, function () use ($node) {
  88. // add fake etag, it is only needed to identify the preview image
  89. return $node->getLastModified();
  90. });
  91. $propFind->handle(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) {
  92. // add fake etag, it is only needed to identify the preview image
  93. return $node->getFileId();
  94. });
  95. $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
  96. return $this->previewManager->isAvailable($node->getFileInfo());
  97. });
  98. $propFind->handle(FilesPlugin::MOUNT_TYPE_PROPERTYNAME, function () {
  99. return '';
  100. });
  101. }
  102. /**
  103. * Set real filename on trashbin download
  104. *
  105. * @param RequestInterface $request
  106. * @param ResponseInterface $response
  107. */
  108. public function httpGet(RequestInterface $request, ResponseInterface $response): void {
  109. $path = $request->getPath();
  110. $node = $this->server->tree->getNodeForPath($path);
  111. if ($node instanceof ITrash) {
  112. $response->addHeader('Content-Disposition', 'attachment; filename="' . $node->getFilename() . '"');
  113. }
  114. }
  115. }