UploadHome.php 2.8 KB

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