UploadHome.php 2.8 KB

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