Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/dav/lib/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@
$uploadCollection = new Upload\RootCollection(
$userPrincipalBackend,
'principals/users',
\OC::$server->query(CleanupService::class));
\OC::$server->query(CleanupService::class),

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OC\ServerContainer::query has been marked as deprecated
$rootFolder,
$userSession,
$logger,
);
$uploadCollection->disableListing = $disableListing;

$avatarCollection = new Avatars\RootCollection($userPrincipalBackend, 'principals/users');
Expand Down
16 changes: 9 additions & 7 deletions apps/dav/lib/Upload/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,28 @@
*/
namespace OCA\DAV\Upload;

use OCP\Files\IRootFolder;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
use Sabre\DAVACL\AbstractPrincipalCollection;
use Sabre\DAVACL\PrincipalBackend;

class RootCollection extends AbstractPrincipalCollection {

/** @var CleanupService */
private $cleanupService;

public function __construct(PrincipalBackend\BackendInterface $principalBackend,
string $principalPrefix,
CleanupService $cleanupService) {
private CleanupService $cleanupService,
private IRootFolder $rootFolder,
private IUserSession $userSession,
private LoggerInterface $logger,
) {
parent::__construct($principalBackend, $principalPrefix);
$this->cleanupService = $cleanupService;
}

/**
* @inheritdoc
*/
public function getChildForPrincipal(array $principalInfo): UploadHome {
return new UploadHome($principalInfo, $this->cleanupService);
return new UploadHome($principalInfo, $this->cleanupService, $this->rootFolder, $this->userSession, $this->logger);
}

/**
Expand Down
69 changes: 43 additions & 26 deletions apps/dav/lib/Upload/UploadHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@
*/
namespace OCA\DAV\Upload;

use OC\Files\Filesystem;
use OC\Files\View;
use OCA\DAV\Connector\Sabre\Directory;
use OCP\Constants;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\ICollection;

class UploadHome implements ICollection {
/** @var array */
private $principalInfo;
/** @var CleanupService */
private $cleanupService;

public function __construct(array $principalInfo, CleanupService $cleanupService) {
$this->principalInfo = $principalInfo;
$this->cleanupService = $cleanupService;
private ?Folder $uploadFolder = null;

public function __construct(
private array $principalInfo,
private CleanupService $cleanupService,
private IRootFolder $rootFolder,
private IUserSession $userSession,
private LoggerInterface $logger,
) {
}

public function createFile($name, $data = null) {
Expand Down Expand Up @@ -84,28 +90,39 @@
return $this->impl()->getLastModified();
}

/**
* @return Directory
*/
private function impl() {
$view = $this->getView();
$rootInfo = $view->getFileInfo('');
return new Directory($view, $rootInfo);
private function getUploadFolder(): Folder {
if ($this->uploadFolder === null) {
$user = $this->userSession->getUser();
if (!$user) {
throw new Forbidden('Not logged in');
}
$path = '/' . $user->getUID() . '/uploads';
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;
}

private function getView() {
$rootView = new View();
$user = \OC::$server->getUserSession()->getUser();
Filesystem::initMountPoints($user->getUID());
if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) {
$rootView->mkdir('/' . $user->getUID() . '/uploads');
private function impl(): Directory {
$folder = $this->getUploadFolder();
if (!$folder->isCreatable()) {
$user = $this->userSession->getUser();
$this->logger->warning('Upload home not writable for ' . $user->getUID() . ', attempting to fix', ['permissions' => $folder->getPermissions()]);

Check notice

Code scanning / Psalm

PossiblyNullReference Note

Cannot call method getUID on possibly null value
$cache = $folder->getStorage()->getCache();
$cache->update($folder->getId(), ['permissions', Constants::PERMISSION_ALL]);
}
return new View('/' . $user->getUID() . '/uploads');
$view = new View($folder->getPath());
return new Directory($view, $folder);
}

private function getStorage() {
$view = $this->getView();
$storage = $view->getFileInfo('')->getStorage();
return $storage;
return $this->getUploadFolder()->getStorage();
}
}
Loading