FilesDropPlugin.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Files\Sharing;
  7. use OC\Files\View;
  8. use Sabre\DAV\Exception\MethodNotAllowed;
  9. use Sabre\DAV\ServerPlugin;
  10. use Sabre\HTTP\RequestInterface;
  11. use Sabre\HTTP\ResponseInterface;
  12. /**
  13. * Make sure that the destination is writable
  14. */
  15. class FilesDropPlugin extends ServerPlugin {
  16. /** @var View */
  17. private $view;
  18. /** @var bool */
  19. private $enabled = false;
  20. /**
  21. * @param View $view
  22. */
  23. public function setView($view) {
  24. $this->view = $view;
  25. }
  26. public function enable() {
  27. $this->enabled = true;
  28. }
  29. /**
  30. * This initializes the plugin.
  31. *
  32. * @param \Sabre\DAV\Server $server Sabre server
  33. *
  34. * @return void
  35. * @throws MethodNotAllowed
  36. */
  37. public function initialize(\Sabre\DAV\Server $server) {
  38. $server->on('beforeMethod:*', [$this, 'beforeMethod'], 999);
  39. $this->enabled = false;
  40. }
  41. public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
  42. if (!$this->enabled) {
  43. return;
  44. }
  45. if ($request->getMethod() !== 'PUT') {
  46. throw new MethodNotAllowed('Only PUT is allowed on files drop');
  47. }
  48. $path = explode('/', $request->getPath());
  49. $path = array_pop($path);
  50. $newName = \OC_Helper::buildNotExistingFileNameForView('/', $path, $this->view);
  51. $url = $request->getBaseUrl() . $newName;
  52. $request->setUrl($url);
  53. }
  54. }