|  | 
| 7 | 7 |  */ | 
| 8 | 8 | namespace OCA\DAV\Upload; | 
| 9 | 9 | 
 | 
| 10 |  | -use OC\Files\Filesystem; | 
| 11 | 10 | use OC\Files\View; | 
| 12 | 11 | use OCA\DAV\Connector\Sabre\Directory; | 
|  | 12 | +use OCP\Files\Folder; | 
|  | 13 | +use OCP\Files\IRootFolder; | 
|  | 14 | +use OCP\Files\NotFoundException; | 
| 13 | 15 | use OCP\IUserSession; | 
| 14 |  | -use OCP\Server; | 
| 15 | 16 | use Sabre\DAV\Exception\Forbidden; | 
| 16 | 17 | use Sabre\DAV\ICollection; | 
| 17 | 18 | 
 | 
| 18 | 19 | class UploadHome implements ICollection { | 
|  | 20 | +	private ?Folder $uploadFolder = null; | 
|  | 21 | + | 
| 19 | 22 | 	public function __construct( | 
| 20 |  | -		private array $principalInfo, | 
| 21 |  | -		private CleanupService $cleanupService, | 
|  | 23 | +		private readonly array $principalInfo, | 
|  | 24 | +		private readonly CleanupService $cleanupService, | 
|  | 25 | +		private readonly IRootFolder $rootFolder, | 
|  | 26 | +		private readonly IUserSession $userSession, | 
| 22 | 27 | 	) { | 
| 23 | 28 | 	} | 
| 24 | 29 | 
 | 
| @@ -64,28 +69,33 @@ public function getLastModified() { | 
| 64 | 69 | 		return $this->impl()->getLastModified(); | 
| 65 | 70 | 	} | 
| 66 | 71 | 
 | 
| 67 |  | -	/** | 
| 68 |  | -	 * @return Directory | 
| 69 |  | -	 */ | 
| 70 |  | -	private function impl() { | 
| 71 |  | -		$view = $this->getView(); | 
| 72 |  | -		$rootInfo = $view->getFileInfo(''); | 
| 73 |  | -		return new Directory($view, $rootInfo); | 
|  | 72 | +	private function getUploadFolder(): Folder { | 
|  | 73 | +		if ($this->uploadFolder === null) { | 
|  | 74 | +			$user = $this->userSession->getUser(); | 
|  | 75 | +			if (!$user) { | 
|  | 76 | +				throw new Forbidden('Not logged in'); | 
|  | 77 | +			} | 
|  | 78 | +			$path = '/' . $user->getUID() . '/uploads'; | 
|  | 79 | +			try { | 
|  | 80 | +				$folder = $this->rootFolder->get($path); | 
|  | 81 | +				if (!$folder instanceof Folder) { | 
|  | 82 | +					throw new \Exception("Upload folder is a file"); | 
|  | 83 | +				} | 
|  | 84 | +				$this->uploadFolder = $folder; | 
|  | 85 | +			} catch (NotFoundException $e) { | 
|  | 86 | +				$this->uploadFolder = $this->rootFolder->newFolder($path); | 
|  | 87 | +			} | 
|  | 88 | +		} | 
|  | 89 | +		return $this->uploadFolder; | 
| 74 | 90 | 	} | 
| 75 | 91 | 
 | 
| 76 |  | -	private function getView() { | 
| 77 |  | -		$rootView = new View(); | 
| 78 |  | -		$user = Server::get(IUserSession::class)->getUser(); | 
| 79 |  | -		Filesystem::initMountPoints($user->getUID()); | 
| 80 |  | -		if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) { | 
| 81 |  | -			$rootView->mkdir('/' . $user->getUID() . '/uploads'); | 
| 82 |  | -		} | 
| 83 |  | -		return new View('/' . $user->getUID() . '/uploads'); | 
|  | 92 | +	private function impl(): Directory { | 
|  | 93 | +		$folder = $this->getUploadFolder(); | 
|  | 94 | +		$view = new View($folder->getPath()); | 
|  | 95 | +		return new Directory($view, $folder); | 
| 84 | 96 | 	} | 
| 85 | 97 | 
 | 
| 86 | 98 | 	private function getStorage() { | 
| 87 |  | -		$view = $this->getView(); | 
| 88 |  | -		$storage = $view->getFileInfo('')->getStorage(); | 
| 89 |  | -		return $storage; | 
|  | 99 | +		return $this->getUploadFolder()->getStorage(); | 
| 90 | 100 | 	} | 
| 91 | 101 | } | 
0 commit comments