2018-04-25 14:24:23 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-04-25 14:24:23 -04:00
|
|
|
declare(strict_types=1);
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-04-25 14:24:23 -04:00
|
|
|
/**
|
2024-06-02 09:26:54 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-04-25 14:24:23 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Files_Trashbin\Sabre;
|
|
|
|
|
|
2025-01-07 11:02:43 -05:00
|
|
|
use OCA\Files_Trashbin\Service\ConfigService;
|
2018-09-10 08:40:35 -04:00
|
|
|
use OCA\Files_Trashbin\Trash\ITrashItem;
|
|
|
|
|
use OCA\Files_Trashbin\Trash\ITrashManager;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCA\Files_Trashbin\Trashbin;
|
2018-04-25 14:24:23 -04:00
|
|
|
use OCP\Files\FileInfo;
|
2018-09-10 08:40:35 -04:00
|
|
|
use OCP\IUser;
|
2018-04-25 14:24:23 -04:00
|
|
|
use Sabre\DAV\Exception\Forbidden;
|
|
|
|
|
use Sabre\DAV\Exception\NotFound;
|
|
|
|
|
use Sabre\DAV\ICollection;
|
|
|
|
|
|
|
|
|
|
class TrashRoot implements ICollection {
|
|
|
|
|
|
2024-10-18 06:04:22 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private IUser $user,
|
|
|
|
|
private ITrashManager $trashManager,
|
|
|
|
|
) {
|
2018-04-25 14:24:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete() {
|
2025-01-07 11:02:43 -05:00
|
|
|
if (!ConfigService::getDeleteFromTrashEnabled()) {
|
|
|
|
|
throw new Forbidden('Not allowed to delete items from the trash bin');
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
Trashbin::deleteAll();
|
2021-03-03 08:26:35 -05:00
|
|
|
foreach ($this->trashManager->listTrashRoot($this->user) as $trashItem) {
|
|
|
|
|
$this->trashManager->removeItem($trashItem);
|
|
|
|
|
}
|
2018-04-25 14:24:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getName(): string {
|
|
|
|
|
return 'trash';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setName($name) {
|
|
|
|
|
throw new Forbidden('Permission denied to rename this trashbin');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createFile($name, $data = null) {
|
|
|
|
|
throw new Forbidden('Not allowed to create files in the trashbin');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createDirectory($name) {
|
|
|
|
|
throw new Forbidden('Not allowed to create folders in the trashbin');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getChildren(): array {
|
2018-09-10 08:40:35 -04:00
|
|
|
$entries = $this->trashManager->listTrashRoot($this->user);
|
2018-04-25 14:24:23 -04:00
|
|
|
|
2018-09-10 08:40:35 -04:00
|
|
|
$children = array_map(function (ITrashItem $entry) {
|
2018-04-25 14:24:23 -04:00
|
|
|
if ($entry->getType() === FileInfo::TYPE_FOLDER) {
|
2018-10-17 09:21:09 -04:00
|
|
|
return new TrashFolder($this->trashManager, $entry);
|
2018-04-25 14:24:23 -04:00
|
|
|
}
|
2018-10-17 09:21:09 -04:00
|
|
|
return new TrashFile($this->trashManager, $entry);
|
2018-04-25 14:24:23 -04:00
|
|
|
}, $entries);
|
|
|
|
|
|
|
|
|
|
return $children;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-10 08:40:35 -04:00
|
|
|
public function getChild($name): ITrash {
|
|
|
|
|
$entries = $this->getChildren();
|
2018-04-25 14:24:23 -04:00
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
2018-09-10 08:40:35 -04:00
|
|
|
if ($entry->getName() === $name) {
|
|
|
|
|
return $entry;
|
2018-04-25 14:24:23 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-10 08:40:35 -04:00
|
|
|
throw new NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function childExists($name): bool {
|
|
|
|
|
try {
|
|
|
|
|
$this->getChild($name);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (NotFound $e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-04-25 14:24:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getLastModified(): int {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|