2015-10-28 09:52:45 -04:00
|
|
|
<?php
|
2024-05-27 11:39:07 -04:00
|
|
|
|
2016-05-26 13:56:05 -04:00
|
|
|
/**
|
2024-05-27 11:39:07 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-05-26 13:56:05 -04:00
|
|
|
*/
|
2015-10-28 09:52:45 -04:00
|
|
|
namespace OCA\DAV\Upload;
|
|
|
|
|
|
|
|
|
|
use OC\Files\View;
|
|
|
|
|
use OCA\DAV\Connector\Sabre\Directory;
|
2025-02-24 13:22:52 -05:00
|
|
|
use OCP\Files\Folder;
|
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
|
use OCP\Files\NotFoundException;
|
2025-02-03 09:34:01 -05:00
|
|
|
use OCP\IUserSession;
|
2015-10-28 09:52:45 -04:00
|
|
|
use Sabre\DAV\Exception\Forbidden;
|
|
|
|
|
use Sabre\DAV\ICollection;
|
|
|
|
|
|
|
|
|
|
class UploadHome implements ICollection {
|
2025-04-14 09:44:59 -04:00
|
|
|
private string $uid;
|
2025-02-24 13:22:52 -05:00
|
|
|
private ?Folder $uploadFolder = null;
|
|
|
|
|
|
2024-10-18 06:04:22 -04:00
|
|
|
public function __construct(
|
2025-02-24 13:22:52 -05:00
|
|
|
private readonly array $principalInfo,
|
|
|
|
|
private readonly CleanupService $cleanupService,
|
|
|
|
|
private readonly IRootFolder $rootFolder,
|
|
|
|
|
private readonly IUserSession $userSession,
|
2025-04-14 09:45:50 -04:00
|
|
|
private readonly \OCP\Share\IManager $shareManager,
|
2024-10-18 06:04:22 -04:00
|
|
|
) {
|
2025-04-14 09:45:50 -04:00
|
|
|
[$prefix, $name] = \Sabre\Uri\split($principalInfo['uri']);
|
|
|
|
|
if ($prefix === 'principals/shares') {
|
|
|
|
|
$this->uid = $this->shareManager->getShareByToken($name)->getShareOwner();
|
|
|
|
|
} else {
|
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
|
if (!$user) {
|
|
|
|
|
throw new Forbidden('Not logged in');
|
|
|
|
|
}
|
2025-04-14 09:44:59 -04:00
|
|
|
|
2025-04-14 09:45:50 -04:00
|
|
|
$this->uid = $user->getUID();
|
|
|
|
|
}
|
2015-10-28 09:52:45 -04:00
|
|
|
}
|
|
|
|
|
|
2018-11-02 16:19:27 -04:00
|
|
|
public function createFile($name, $data = null) {
|
2015-10-28 09:52:45 -04:00
|
|
|
throw new Forbidden('Permission denied to create file (filename ' . $name . ')');
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 16:19:27 -04:00
|
|
|
public function createDirectory($name) {
|
2015-10-28 09:52:45 -04:00
|
|
|
$this->impl()->createDirectory($name);
|
2018-11-02 16:19:27 -04:00
|
|
|
|
|
|
|
|
// Add a cleanup job
|
2025-04-14 09:44:59 -04:00
|
|
|
$this->cleanupService->addJob($this->uid, $name);
|
2015-10-28 09:52:45 -04:00
|
|
|
}
|
|
|
|
|
|
2018-11-02 16:19:27 -04:00
|
|
|
public function getChild($name): UploadFolder {
|
2025-04-14 09:44:59 -04:00
|
|
|
return new UploadFolder(
|
|
|
|
|
$this->impl()->getChild($name),
|
|
|
|
|
$this->cleanupService,
|
|
|
|
|
$this->getStorage(),
|
|
|
|
|
$this->uid,
|
|
|
|
|
);
|
2015-10-28 09:52:45 -04:00
|
|
|
}
|
|
|
|
|
|
2018-11-02 16:19:27 -04:00
|
|
|
public function getChildren(): array {
|
2020-04-09 07:53:40 -04:00
|
|
|
return array_map(function ($node) {
|
2025-04-14 09:44:59 -04:00
|
|
|
return new UploadFolder(
|
|
|
|
|
$node,
|
|
|
|
|
$this->cleanupService,
|
|
|
|
|
$this->getStorage(),
|
|
|
|
|
$this->uid,
|
|
|
|
|
);
|
2015-10-28 09:52:45 -04:00
|
|
|
}, $this->impl()->getChildren());
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 16:19:27 -04:00
|
|
|
public function childExists($name): bool {
|
2015-10-28 09:52:45 -04:00
|
|
|
return !is_null($this->getChild($name));
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 16:19:27 -04:00
|
|
|
public function delete() {
|
2015-10-28 09:52:45 -04:00
|
|
|
$this->impl()->delete();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 16:19:27 -04:00
|
|
|
public function getName() {
|
2021-01-12 04:15:48 -05:00
|
|
|
[,$name] = \Sabre\Uri\split($this->principalInfo['uri']);
|
2018-01-10 02:35:20 -05:00
|
|
|
return $name;
|
2015-10-28 09:52:45 -04:00
|
|
|
}
|
|
|
|
|
|
2018-11-02 16:19:27 -04:00
|
|
|
public function setName($name) {
|
2015-10-28 09:52:45 -04:00
|
|
|
throw new Forbidden('Permission denied to rename this folder');
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 16:19:27 -04:00
|
|
|
public function getLastModified() {
|
2015-10-28 09:52:45 -04:00
|
|
|
return $this->impl()->getLastModified();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 13:22:52 -05:00
|
|
|
private function getUploadFolder(): Folder {
|
|
|
|
|
if ($this->uploadFolder === null) {
|
2025-04-14 09:44:59 -04:00
|
|
|
$path = '/' . $this->uid . '/uploads';
|
2025-02-24 13:22:52 -05:00
|
|
|
try {
|
|
|
|
|
$folder = $this->rootFolder->get($path);
|
|
|
|
|
if (!$folder instanceof Folder) {
|
|
|
|
|
throw new \Exception('Upload folder is a file');
|
|
|
|
|
}
|
|
|
|
|
$this->uploadFolder = $folder;
|
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
|
$this->uploadFolder = $this->rootFolder->newFolder($path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->uploadFolder;
|
2021-05-06 12:26:42 -04:00
|
|
|
}
|
|
|
|
|
|
2025-02-24 13:22:52 -05:00
|
|
|
private function impl(): Directory {
|
|
|
|
|
$folder = $this->getUploadFolder();
|
|
|
|
|
$view = new View($folder->getPath());
|
|
|
|
|
return new Directory($view, $folder);
|
2021-05-06 12:26:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getStorage() {
|
2025-02-24 13:22:52 -05:00
|
|
|
return $this->getUploadFolder()->getStorage();
|
2015-10-28 09:52:45 -04:00
|
|
|
}
|
|
|
|
|
}
|