|  | 
| 25 | 25 |  */ | 
| 26 | 26 | namespace OCA\DAV\Upload; | 
| 27 | 27 | 
 | 
| 28 |  | -use OC\Files\Filesystem; | 
| 29 | 28 | use OC\Files\View; | 
| 30 | 29 | use OCA\DAV\Connector\Sabre\Directory; | 
|  | 30 | +use OCP\Files\Folder; | 
|  | 31 | +use OCP\Files\IRootFolder; | 
|  | 32 | +use OCP\Files\NotFoundException; | 
|  | 33 | +use OCP\IUserSession; | 
| 31 | 34 | use Sabre\DAV\Exception\Forbidden; | 
| 32 | 35 | use Sabre\DAV\ICollection; | 
| 33 | 36 | 
 | 
| 34 | 37 | class UploadHome implements ICollection { | 
| 35 |  | -	/** @var array */ | 
| 36 |  | -	private $principalInfo; | 
| 37 |  | -	/** @var CleanupService */ | 
| 38 |  | -	private $cleanupService; | 
| 39 |  | - | 
| 40 |  | -	public function __construct(array $principalInfo, CleanupService $cleanupService) { | 
| 41 |  | -		$this->principalInfo = $principalInfo; | 
| 42 |  | -		$this->cleanupService = $cleanupService; | 
|  | 38 | +	private ?Folder $uploadFolder = null; | 
|  | 39 | + | 
|  | 40 | +	public function __construct( | 
|  | 41 | +		private array $principalInfo, | 
|  | 42 | +		private CleanupService $cleanupService, | 
|  | 43 | +		private IRootFolder $rootFolder, | 
|  | 44 | +		private IUserSession $userSession, | 
|  | 45 | +	) { | 
| 43 | 46 | 	} | 
| 44 | 47 | 
 | 
| 45 | 48 | 	public function createFile($name, $data = null) { | 
| @@ -84,28 +87,33 @@ public function getLastModified() { | 
| 84 | 87 | 		return $this->impl()->getLastModified(); | 
| 85 | 88 | 	} | 
| 86 | 89 | 
 | 
| 87 |  | -	/** | 
| 88 |  | -	 * @return Directory | 
| 89 |  | -	 */ | 
| 90 |  | -	private function impl() { | 
| 91 |  | -		$view = $this->getView(); | 
| 92 |  | -		$rootInfo = $view->getFileInfo(''); | 
| 93 |  | -		return new Directory($view, $rootInfo); | 
|  | 90 | +	private function getUploadFolder(): Folder { | 
|  | 91 | +		if ($this->uploadFolder === null) { | 
|  | 92 | +			$user = $this->userSession->getUser(); | 
|  | 93 | +			if (!$user) { | 
|  | 94 | +				throw new Forbidden('Not logged in'); | 
|  | 95 | +			} | 
|  | 96 | +			$path = '/' . $user->getUID() . '/uploads'; | 
|  | 97 | +			try { | 
|  | 98 | +				$folder = $this->rootFolder->get($path); | 
|  | 99 | +				if (!$folder instanceof Folder) { | 
|  | 100 | +					throw new \Exception('Upload folder is a file'); | 
|  | 101 | +				} | 
|  | 102 | +				$this->uploadFolder = $folder; | 
|  | 103 | +			} catch (NotFoundException $e) { | 
|  | 104 | +				$this->uploadFolder = $this->rootFolder->newFolder($path); | 
|  | 105 | +			} | 
|  | 106 | +		} | 
|  | 107 | +		return $this->uploadFolder; | 
| 94 | 108 | 	} | 
| 95 | 109 | 
 | 
| 96 |  | -	private function getView() { | 
| 97 |  | -		$rootView = new View(); | 
| 98 |  | -		$user = \OC::$server->getUserSession()->getUser(); | 
| 99 |  | -		Filesystem::initMountPoints($user->getUID()); | 
| 100 |  | -		if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) { | 
| 101 |  | -			$rootView->mkdir('/' . $user->getUID() . '/uploads'); | 
| 102 |  | -		} | 
| 103 |  | -		return new View('/' . $user->getUID() . '/uploads'); | 
|  | 110 | +	private function impl(): Directory { | 
|  | 111 | +		$folder = $this->getUploadFolder(); | 
|  | 112 | +		$view = new View($folder->getPath()); | 
|  | 113 | +		return new Directory($view, $folder); | 
| 104 | 114 | 	} | 
| 105 | 115 | 
 | 
| 106 | 116 | 	private function getStorage() { | 
| 107 |  | -		$view = $this->getView(); | 
| 108 |  | -		$storage = $view->getFileInfo('')->getStorage(); | 
| 109 |  | -		return $storage; | 
|  | 117 | +		return $this->getUploadFolder()->getStorage(); | 
| 110 | 118 | 	} | 
| 111 | 119 | } | 
0 commit comments