您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Plugin.php 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 (strpos($path, 'versions') !== 0) {
  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. }