Plugin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 OCP\IRequest;
  28. use Sabre\DAV\Exception\NotFound;
  29. use Sabre\DAV\INode;
  30. use Sabre\DAV\PropFind;
  31. use Sabre\DAV\PropPatch;
  32. use Sabre\DAV\Server;
  33. use Sabre\DAV\ServerPlugin;
  34. use Sabre\HTTP\RequestInterface;
  35. use Sabre\HTTP\ResponseInterface;
  36. class Plugin extends ServerPlugin {
  37. private Server $server;
  38. private IRequest $request;
  39. public const VERSION_LABEL = '{http://nextcloud.org/ns}version-label';
  40. public function __construct(
  41. IRequest $request
  42. ) {
  43. $this->request = $request;
  44. }
  45. public function initialize(Server $server) {
  46. $this->server = $server;
  47. $server->on('afterMethod:GET', [$this, 'afterGet']);
  48. $server->on('propFind', [$this, 'propFind']);
  49. $server->on('propPatch', [$this, 'propPatch']);
  50. }
  51. public function afterGet(RequestInterface $request, ResponseInterface $response) {
  52. $path = $request->getPath();
  53. if (!str_starts_with($path, 'versions')) {
  54. return;
  55. }
  56. try {
  57. $node = $this->server->tree->getNodeForPath($path);
  58. } catch (NotFound $e) {
  59. return;
  60. }
  61. if (!($node instanceof VersionFile)) {
  62. return;
  63. }
  64. $filename = $node->getVersion()->getSourceFileName();
  65. if ($this->request->isUserAgent(
  66. [
  67. Request::USER_AGENT_IE,
  68. Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  69. Request::USER_AGENT_FREEBOX,
  70. ])) {
  71. $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
  72. } else {
  73. $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
  74. . '; filename="' . rawurlencode($filename) . '"');
  75. }
  76. }
  77. public function propFind(PropFind $propFind, INode $node): void {
  78. if ($node instanceof VersionFile) {
  79. $propFind->handle(self::VERSION_LABEL, fn() => $node->getLabel());
  80. }
  81. }
  82. public function propPatch($path, PropPatch $propPatch): void {
  83. $node = $this->server->tree->getNodeForPath($path);
  84. if ($node instanceof VersionFile) {
  85. $propPatch->handle(self::VERSION_LABEL, fn ($label) => $node->setLabel($label));
  86. }
  87. }
  88. }