Plugin.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Server;
  30. use Sabre\DAV\ServerPlugin;
  31. use Sabre\HTTP\RequestInterface;
  32. use Sabre\HTTP\ResponseInterface;
  33. class Plugin extends ServerPlugin {
  34. /** @var Server */
  35. private $server;
  36. /** @var IRequest */
  37. private $request;
  38. public function __construct(IRequest $request) {
  39. $this->request = $request;
  40. }
  41. public function initialize(Server $server) {
  42. $this->server = $server;
  43. $server->on('afterMethod:GET', [$this, 'afterGet']);
  44. }
  45. public function afterGet(RequestInterface $request, ResponseInterface $response) {
  46. $path = $request->getPath();
  47. if (!str_starts_with($path, 'versions')) {
  48. return;
  49. }
  50. try {
  51. $node = $this->server->tree->getNodeForPath($path);
  52. } catch (NotFound $e) {
  53. return;
  54. }
  55. if (!($node instanceof VersionFile)) {
  56. return;
  57. }
  58. $filename = $node->getVersion()->getSourceFileName();
  59. if ($this->request->isUserAgent(
  60. [
  61. Request::USER_AGENT_IE,
  62. Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  63. Request::USER_AGENT_FREEBOX,
  64. ])) {
  65. $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
  66. } else {
  67. $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
  68. . '; filename="' . rawurlencode($filename) . '"');
  69. }
  70. }
  71. }