Plugin.php 2.2 KB

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