1
0

TrashbinPlugin.php 3.9 KB

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