UploadHome.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Upload;
  27. use OC\Files\Filesystem;
  28. use OC\Files\View;
  29. use OCA\DAV\Connector\Sabre\Directory;
  30. use Sabre\DAV\Exception\Forbidden;
  31. use Sabre\DAV\ICollection;
  32. class UploadHome implements ICollection {
  33. /** @var array */
  34. private $principalInfo;
  35. /** @var CleanupService */
  36. private $cleanupService;
  37. public function __construct(array $principalInfo, CleanupService $cleanupService) {
  38. $this->principalInfo = $principalInfo;
  39. $this->cleanupService = $cleanupService;
  40. }
  41. public function createFile($name, $data = null) {
  42. throw new Forbidden('Permission denied to create file (filename ' . $name . ')');
  43. }
  44. public function createDirectory($name) {
  45. $this->impl()->createDirectory($name);
  46. // Add a cleanup job
  47. $this->cleanupService->addJob($name);
  48. }
  49. public function getChild($name): UploadFolder {
  50. return new UploadFolder($this->impl()->getChild($name), $this->cleanupService);
  51. }
  52. public function getChildren(): array {
  53. return array_map(function ($node) {
  54. return new UploadFolder($node, $this->cleanupService);
  55. }, $this->impl()->getChildren());
  56. }
  57. public function childExists($name): bool {
  58. return !is_null($this->getChild($name));
  59. }
  60. public function delete() {
  61. $this->impl()->delete();
  62. }
  63. public function getName() {
  64. list(,$name) = \Sabre\Uri\split($this->principalInfo['uri']);
  65. return $name;
  66. }
  67. public function setName($name) {
  68. throw new Forbidden('Permission denied to rename this folder');
  69. }
  70. public function getLastModified() {
  71. return $this->impl()->getLastModified();
  72. }
  73. /**
  74. * @return Directory
  75. */
  76. private function impl() {
  77. $rootView = new View();
  78. $user = \OC::$server->getUserSession()->getUser();
  79. Filesystem::initMountPoints($user->getUID());
  80. if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) {
  81. $rootView->mkdir('/' . $user->getUID() . '/uploads');
  82. }
  83. $view = new View('/' . $user->getUID() . '/uploads');
  84. $rootInfo = $view->getFileInfo('');
  85. return new Directory($view, $rootInfo);
  86. }
  87. }