1
0

Plugin.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 function __construct(
  42. private IRequest $request,
  43. private IPreview $previewManager,
  44. ) {
  45. $this->request = $request;
  46. }
  47. public function initialize(Server $server) {
  48. $this->server = $server;
  49. $server->on('afterMethod:GET', [$this, 'afterGet']);
  50. $server->on('propFind', [$this, 'propFind']);
  51. $server->on('propPatch', [$this, 'propPatch']);
  52. }
  53. public function afterGet(RequestInterface $request, ResponseInterface $response) {
  54. $path = $request->getPath();
  55. if (!str_starts_with($path, 'versions')) {
  56. return;
  57. }
  58. try {
  59. $node = $this->server->tree->getNodeForPath($path);
  60. } catch (NotFound $e) {
  61. return;
  62. }
  63. if (!($node instanceof VersionFile)) {
  64. return;
  65. }
  66. $filename = $node->getVersion()->getSourceFileName();
  67. if ($this->request->isUserAgent(
  68. [
  69. Request::USER_AGENT_IE,
  70. Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  71. Request::USER_AGENT_FREEBOX,
  72. ])) {
  73. $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
  74. } else {
  75. $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
  76. . '; filename="' . rawurlencode($filename) . '"');
  77. }
  78. }
  79. public function propFind(PropFind $propFind, INode $node): void {
  80. if ($node instanceof VersionFile) {
  81. $propFind->handle(self::VERSION_LABEL, fn() => $node->getLabel());
  82. $propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, fn () => $this->previewManager->isMimeSupported($node->getContentType()));
  83. }
  84. }
  85. public function propPatch($path, PropPatch $propPatch): void {
  86. $node = $this->server->tree->getNodeForPath($path);
  87. if ($node instanceof VersionFile) {
  88. $propPatch->handle(self::VERSION_LABEL, fn ($label) => $node->setLabel($label));
  89. }
  90. }
  91. }