FilesDropPlugin.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  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\DAV\Files\Sharing;
  26. use OC\Files\View;
  27. use Sabre\DAV\Exception\MethodNotAllowed;
  28. use Sabre\DAV\ServerPlugin;
  29. use Sabre\HTTP\RequestInterface;
  30. use Sabre\HTTP\ResponseInterface;
  31. /**
  32. * Make sure that the destination is writable
  33. */
  34. class FilesDropPlugin extends ServerPlugin {
  35. /** @var View */
  36. private $view;
  37. /** @var bool */
  38. private $enabled = false;
  39. /**
  40. * @param View $view
  41. */
  42. public function setView($view) {
  43. $this->view = $view;
  44. }
  45. public function enable() {
  46. $this->enabled = true;
  47. }
  48. /**
  49. * This initializes the plugin.
  50. *
  51. * @param \Sabre\DAV\Server $server Sabre server
  52. *
  53. * @return void
  54. * @throws MethodNotAllowed
  55. */
  56. public function initialize(\Sabre\DAV\Server $server) {
  57. $server->on('beforeMethod:*', [$this, 'beforeMethod'], 999);
  58. $this->enabled = false;
  59. }
  60. public function beforeMethod(RequestInterface $request, ResponseInterface $response) {
  61. if (!$this->enabled) {
  62. return;
  63. }
  64. if ($request->getMethod() !== 'PUT') {
  65. throw new MethodNotAllowed('Only PUT is allowed on files drop');
  66. }
  67. $path = explode('/', $request->getPath());
  68. $path = array_pop($path);
  69. $newName = \OC_Helper::buildNotExistingFileNameForView('/', $path, $this->view);
  70. $url = $request->getBaseUrl() . $newName;
  71. $request->setUrl($url);
  72. }
  73. }