1
0

Plugin.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_Versions\Sabre;
  26. use OC\AppFramework\Http\Request;
  27. use OCA\DAV\Connector\Sabre\FilesPlugin;
  28. use OCP\IPreview;
  29. use OCP\IRequest;
  30. use Sabre\DAV\Exception\NotFound;
  31. use Sabre\DAV\INode;
  32. use Sabre\DAV\PropFind;
  33. use Sabre\DAV\PropPatch;
  34. use Sabre\DAV\Server;
  35. use Sabre\DAV\ServerPlugin;
  36. use Sabre\HTTP\RequestInterface;
  37. use Sabre\HTTP\ResponseInterface;
  38. class Plugin extends ServerPlugin {
  39. private Server $server;
  40. public const VERSION_LABEL = '{http://nextcloud.org/ns}version-label';
  41. public const VERSION_AUTHOR = '{http://nextcloud.org/ns}version-author'; // dav property for author
  42. public function __construct(
  43. private IRequest $request,
  44. private IPreview $previewManager,
  45. ) {
  46. $this->request = $request;
  47. }
  48. public function initialize(Server $server) {
  49. $this->server = $server;
  50. $server->on('afterMethod:GET', [$this, 'afterGet']);
  51. $server->on('propFind', [$this, 'propFind']);
  52. $server->on('propPatch', [$this, 'propPatch']);
  53. }
  54. public function afterGet(RequestInterface $request, ResponseInterface $response) {
  55. $path = $request->getPath();
  56. if (!str_starts_with($path, 'versions')) {
  57. return;
  58. }
  59. try {
  60. $node = $this->server->tree->getNodeForPath($path);
  61. } catch (NotFound $e) {
  62. return;
  63. }
  64. if (!($node instanceof VersionFile)) {
  65. return;
  66. }
  67. $filename = $node->getVersion()->getSourceFileName();
  68. if ($this->request->isUserAgent(
  69. [
  70. Request::USER_AGENT_IE,
  71. Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  72. Request::USER_AGENT_FREEBOX,
  73. ])) {
  74. $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
  75. } else {
  76. $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
  77. . '; filename="' . rawurlencode($filename) . '"');
  78. }
  79. }
  80. public function propFind(PropFind $propFind, INode $node): void {
  81. if ($node instanceof VersionFile) {
  82. $propFind->handle(self::VERSION_LABEL, fn () => $node->getMetadataValue('label'));
  83. $propFind->handle(self::VERSION_AUTHOR, fn () => $node->getMetadataValue("author"));
  84. $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, fn () => $this->previewManager->isMimeSupported($node->getContentType()));
  85. }
  86. }
  87. public function propPatch($path, PropPatch $propPatch): void {
  88. $node = $this->server->tree->getNodeForPath($path);
  89. if ($node instanceof VersionFile) {
  90. $propPatch->handle(self::VERSION_LABEL, fn (string $label) => $node->setMetadataValue('label', $label));
  91. }
  92. }
  93. }