commit
4ad27260a9
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_Trashbin\Sabre;
|
||||
|
||||
use Sabre\DAV\Exception\Forbidden;
|
||||
use Sabre\DAV\IFile;
|
||||
|
||||
abstract class AbstractTrashFile extends AbstractTrash implements IFile , ITrash{
|
||||
public function put($data) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
public function setName($name) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_Trashbin\Sabre;
|
||||
|
||||
use OCA\Files_Trashbin\Trash\ITrashItem;
|
||||
use OCP\Files\FileInfo;
|
||||
use Sabre\DAV\Exception\Forbidden;
|
||||
use Sabre\DAV\Exception\NotFound;
|
||||
use Sabre\DAV\ICollection;
|
||||
|
||||
abstract class AbstractTrashFolder extends AbstractTrash implements ICollection, ITrash {
|
||||
public function getChildren(): array {
|
||||
$entries = $this->trashManager->listTrashFolder($this->data);
|
||||
|
||||
$children = array_map(function (ITrashItem $entry) {
|
||||
if ($entry->getType() === FileInfo::TYPE_FOLDER) {
|
||||
return new TrashFolderFolder($this->trashManager, $entry);
|
||||
}
|
||||
return new TrashFolderFile($this->trashManager, $entry);
|
||||
}, $entries);
|
||||
|
||||
return $children;
|
||||
}
|
||||
|
||||
public function getChild($name): ITrash {
|
||||
$entries = $this->getChildren();
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
if ($entry->getName() === $name) {
|
||||
return $entry;
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
public function childExists($name): bool {
|
||||
try {
|
||||
$this->getChild($name);
|
||||
return true;
|
||||
} catch (NotFound $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function setName($name) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
public function createFile($name, $data = null) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
|
||||
public function createDirectory($name) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_Trashbin\Trash;
|
||||
|
||||
class BackendNotFoundException extends \Exception {
|
||||
|
||||
}
|
||||
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_Trashbin\Trash;
|
||||
|
||||
use OCP\Files\FileInfo;
|
||||
use OCP\Files\Node;
|
||||
use OCP\Files\SimpleFS\ISimpleFile;
|
||||
use OCP\Files\Storage\IStorage;
|
||||
use OCP\IUser;
|
||||
|
||||
/**
|
||||
* @since 15.0.0
|
||||
*/
|
||||
interface ITrashBackend {
|
||||
/**
|
||||
* List all trash items in the root of the trashbin
|
||||
*
|
||||
* @param IUser $user
|
||||
* @return ITrashItem[]
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function listTrashRoot(IUser $user): array;
|
||||
|
||||
/**
|
||||
* List all trash items in a subfolder in the trashbin
|
||||
*
|
||||
* @param ITrashItem $folder
|
||||
* @return ITrashItem[]
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function listTrashFolder(ITrashItem $folder): array;
|
||||
|
||||
/**
|
||||
* Restore a trashbin item
|
||||
*
|
||||
* @param ITrashItem $item
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function restoreItem(ITrashItem $item);
|
||||
|
||||
/**
|
||||
* Permanently remove an item from trash
|
||||
*
|
||||
* @param ITrashItem $item
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function removeItem(ITrashItem $item);
|
||||
|
||||
/**
|
||||
* Move a file or folder to trash
|
||||
*
|
||||
* @param IStorage $storage
|
||||
* @param string $internalPath
|
||||
* @return boolean whether or not the file was moved to trash, if false then the file should be deleted normally
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function moveToTrash(IStorage $storage, string $internalPath): bool;
|
||||
|
||||
/**
|
||||
* @param IUser $user
|
||||
* @param int $fileId
|
||||
* @return Node|null
|
||||
*/
|
||||
public function getTrashNodeById(IUser $user, int $fileId);
|
||||
}
|
||||
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_Trashbin\Trash;
|
||||
|
||||
use OCP\Files\FileInfo;
|
||||
use OCP\IUser;
|
||||
|
||||
/**
|
||||
* @since 15.0.0
|
||||
*/
|
||||
interface ITrashItem extends FileInfo {
|
||||
/**
|
||||
* Get the trash backend for this item
|
||||
*
|
||||
* @return ITrashBackend
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function getTrashBackend(): ITrashBackend;
|
||||
|
||||
/**
|
||||
* Get the original location for the trash item
|
||||
*
|
||||
* @return string
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function getOriginalLocation(): string;
|
||||
|
||||
/**
|
||||
* Get the timestamp that the file was moved to trash
|
||||
*
|
||||
* @return int
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function getDeletedTime(): int;
|
||||
|
||||
/**
|
||||
* Get the path of the item relative to the users trashbin
|
||||
*
|
||||
* @return string
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function getTrashPath(): string;
|
||||
|
||||
/**
|
||||
* Whether the item is a deleted item in the root of the trash, or a file in a subfolder
|
||||
*
|
||||
* @return bool
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function isRootItem(): bool;
|
||||
|
||||
/**
|
||||
* Get the user for which this trash item applies
|
||||
*
|
||||
* @return IUser
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function getUser(): IUser;
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_Trashbin\Trash;
|
||||
|
||||
use OCP\IUser;
|
||||
|
||||
interface ITrashManager extends ITrashBackend {
|
||||
/**
|
||||
* Add a backend for the trashbin
|
||||
*
|
||||
* @param string $storageType
|
||||
* @param ITrashBackend $backend
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function registerBackend(string $storageType, ITrashBackend $backend);
|
||||
|
||||
/**
|
||||
* List all trash items in the root of the trashbin
|
||||
*
|
||||
* @param IUser $user
|
||||
* @return ITrashItem[]
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function listTrashRoot(IUser $user): array;
|
||||
|
||||
/**
|
||||
* Temporally prevent files from being moved to the trash
|
||||
*
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function pauseTrash();
|
||||
|
||||
/**
|
||||
* @since 15.0.0
|
||||
*/
|
||||
public function resumeTrash();
|
||||
}
|
||||
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_Trashbin\Trash;
|
||||
|
||||
use OC\Files\Filesystem;
|
||||
use OC\Files\View;
|
||||
use OCA\Files_Trashbin\Helper;
|
||||
use OCA\Files_Trashbin\Storage;
|
||||
use OCA\Files_Trashbin\Trashbin;
|
||||
use OCP\Files\FileInfo;
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCP\Files\NotFoundException;
|
||||
use OCP\Files\Storage\IStorage;
|
||||
use OCP\IUser;
|
||||
|
||||
class LegacyTrashBackend implements ITrashBackend {
|
||||
/** @var array */
|
||||
private $deletedFiles = [];
|
||||
|
||||
/** @var IRootFolder */
|
||||
private $rootFolder;
|
||||
|
||||
public function __construct(IRootFolder $rootFolder) {
|
||||
$this->rootFolder = $rootFolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $items
|
||||
* @param IUser $user
|
||||
* @param ITrashItem $parent
|
||||
* @return ITrashItem[]
|
||||
*/
|
||||
private function mapTrashItems(array $items, IUser $user, ITrashItem $parent = null): array {
|
||||
$parentTrashPath = ($parent instanceof ITrashItem) ? $parent->getTrashPath() : '';
|
||||
$isRoot = $parent === null;
|
||||
return array_map(function (FileInfo $file) use ($parent, $parentTrashPath, $isRoot, $user) {
|
||||
return new TrashItem(
|
||||
$this,
|
||||
$isRoot ? $file['extraData'] : $parent->getOriginalLocation() . '/' . $file->getName(),
|
||||
$file->getMTime(),
|
||||
$parentTrashPath . '/' . $file->getName() . ($isRoot ? '.d' . $file->getMtime() : ''),
|
||||
$file,
|
||||
$user
|
||||
);
|
||||
}, $items);
|
||||
}
|
||||
|
||||
public function listTrashRoot(IUser $user): array {
|
||||
$entries = Helper::getTrashFiles('/', $user->getUID());
|
||||
return $this->mapTrashItems($entries, $user);
|
||||
}
|
||||
|
||||
public function listTrashFolder(ITrashItem $folder): array {
|
||||
$user = $folder->getUser();
|
||||
$entries = Helper::getTrashFiles($folder->getTrashPath(), $user->getUID());
|
||||
return $this->mapTrashItems($entries, $user ,$folder);
|
||||
}
|
||||
|
||||
public function restoreItem(ITrashItem $item) {
|
||||
Trashbin::restore($item->getTrashPath(), $item->getName(), $item->isRootItem() ? $item->getDeletedTime() : null);
|
||||
}
|
||||
|
||||
public function removeItem(ITrashItem $item) {
|
||||
$user = $item->getUser();
|
||||
if ($item->isRootItem()) {
|
||||
$path = substr($item->getTrashPath(), 0, -strlen('.d' . $item->getDeletedTime()));
|
||||
Trashbin::delete($path, $user->getUID(), $item->getDeletedTime());
|
||||
} else {
|
||||
Trashbin::delete($item->getTrashPath(), $user->getUID(), null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function moveToTrash(IStorage $storage, string $internalPath): bool {
|
||||
if (!$storage instanceof Storage) {
|
||||
return false;
|
||||
}
|
||||
$normalized = Filesystem::normalizePath($storage->getMountPoint() . '/' . $internalPath, true, false, true);
|
||||
$view = Filesystem::getView();
|
||||
if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
|
||||
$this->deletedFiles[$normalized] = $normalized;
|
||||
if ($filesPath = $view->getRelativePath($normalized)) {
|
||||
$filesPath = trim($filesPath, '/');
|
||||
$result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath);
|
||||
} else {
|
||||
$result = false;
|
||||
}
|
||||
unset($this->deletedFiles[$normalized]);
|
||||
} else {
|
||||
$result = false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getTrashNodeById(IUser $user, int $fileId) {
|
||||
try {
|
||||
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
|
||||
$trash = $userFolder->getParent()->get('files_trashbin/files');
|
||||
$trashFiles = $trash->getById($fileId);
|
||||
if (!$trashFiles) {
|
||||
return null;
|
||||
}
|
||||
return $trashFiles ? array_pop($trashFiles) : null;
|
||||
} catch (NotFoundException $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,172 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_Trashbin\Trash;
|
||||
|
||||
use OCP\Files\FileInfo;
|
||||
use OCP\IUser;
|
||||
|
||||
class TrashItem implements ITrashItem {
|
||||
/** @var ITrashBackend */
|
||||
private $backend;
|
||||
/** @var string */
|
||||
private $orignalLocation;
|
||||
/** @var int */
|
||||
private $deletedTime;
|
||||
/** @var string */
|
||||
private $trashPath;
|
||||
/** @var FileInfo */
|
||||
private $fileInfo;
|
||||
/** @var IUser */
|
||||
private $user;
|
||||
|
||||
public function __construct(
|
||||
ITrashBackend $backend,
|
||||
string $originalLocation,
|
||||
int $deletedTime,
|
||||
string $trashPath,
|
||||
FileInfo $fileInfo,
|
||||
IUser $user
|
||||
) {
|
||||
$this->backend = $backend;
|
||||
$this->orignalLocation = $originalLocation;
|
||||
$this->deletedTime = $deletedTime;
|
||||
$this->trashPath = $trashPath;
|
||||
$this->fileInfo = $fileInfo;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function getTrashBackend(): ITrashBackend {
|
||||
return $this->backend;
|
||||
}
|
||||
|
||||
public function getOriginalLocation(): string {
|
||||
return $this->orignalLocation;
|
||||
}
|
||||
|
||||
public function getDeletedTime(): int {
|
||||
return $this->deletedTime;
|
||||
}
|
||||
|
||||
public function getTrashPath(): string {
|
||||
return $this->trashPath;
|
||||
}
|
||||
|
||||
public function isRootItem(): bool {
|
||||
return substr_count($this->getTrashPath(), '/') === 1;
|
||||
}
|
||||
|
||||
public function getUser(): IUser {
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function getEtag() {
|
||||
return $this->fileInfo->getEtag();
|
||||
}
|
||||
|
||||
public function getSize() {
|
||||
return $this->fileInfo->getSize();
|
||||
}
|
||||
|
||||
public function getMtime() {
|
||||
return $this->fileInfo->getMtime();
|
||||
}
|
||||
|
||||
public function getName() {
|
||||
return $this->fileInfo->getName();
|
||||
}
|
||||
|
||||
public function getInternalPath() {
|
||||
return $this->fileInfo->getInternalPath();
|
||||
}
|
||||
|
||||
public function getPath() {
|
||||
return $this->fileInfo->getPath();
|
||||
}
|
||||
|
||||
public function getMimetype() {
|
||||
return $this->fileInfo->getMimetype();
|
||||
}
|
||||
|
||||
public function getMimePart() {
|
||||
return $this->fileInfo->getMimePart();
|
||||
}
|
||||
|
||||
public function getStorage() {
|
||||
return $this->fileInfo->getStorage();
|
||||
}
|
||||
|
||||
public function getId() {
|
||||
return $this->fileInfo->getId();
|
||||
}
|
||||
|
||||
public function isEncrypted() {
|
||||
return $this->fileInfo->isEncrypted();
|
||||
}
|
||||
|
||||
public function getPermissions() {
|
||||
return $this->fileInfo->getPermissions();
|
||||
}
|
||||
|
||||
public function getType() {
|
||||
return $this->fileInfo->getType();
|
||||
}
|
||||
|
||||
public function isReadable() {
|
||||
return $this->fileInfo->isReadable();
|
||||
}
|
||||
|
||||
public function isUpdateable() {
|
||||
return $this->fileInfo->isUpdateable();
|
||||
}
|
||||
|
||||
public function isCreatable() {
|
||||
return $this->fileInfo->isCreatable();
|
||||
}
|
||||
|
||||
public function isDeletable() {
|
||||
return $this->fileInfo->isDeletable();
|
||||
}
|
||||
|
||||
public function isShareable() {
|
||||
return $this->fileInfo->isShareable();
|
||||
}
|
||||
|
||||
public function isShared() {
|
||||
return $this->fileInfo->isShared();
|
||||
}
|
||||
|
||||
public function isMounted() {
|
||||
return $this->fileInfo->isMounted();
|
||||
}
|
||||
|
||||
public function getMountPoint() {
|
||||
return $this->fileInfo->getMountPoint();
|
||||
}
|
||||
|
||||
public function getOwner() {
|
||||
return $this->fileInfo->getOwner();
|
||||
}
|
||||
|
||||
public function getChecksum() {
|
||||
return $this->fileInfo->getChecksum();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_Trashbin\Trash;
|
||||
|
||||
use OCP\Files\FileInfo;
|
||||
use OCP\Files\Mount\IMountPoint;
|
||||
use OCP\Files\Storage\IStorage;
|
||||
use OCP\IUser;
|
||||
|
||||
class TrashManager implements ITrashManager {
|
||||
/** @var ITrashBackend[] */
|
||||
private $backends = [];
|
||||
|
||||
private $trashPaused = false;
|
||||
|
||||
public function registerBackend(string $storageType, ITrashBackend $backend) {
|
||||
$this->backends[$storageType] = $backend;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ITrashBackend[]
|
||||
*/
|
||||
private function getBackends(): array {
|
||||
return $this->backends;
|
||||
}
|
||||
|
||||
public function listTrashRoot(IUser $user): array {
|
||||
$items = array_reduce($this->getBackends(), function (array $items, ITrashBackend $backend) use ($user) {
|
||||
return array_merge($items, $backend->listTrashRoot($user));
|
||||
}, []);
|
||||
usort($items, function (ITrashItem $a, ITrashItem $b) {
|
||||
return $a->getDeletedTime() - $b->getDeletedTime();
|
||||
});
|
||||
return $items;
|
||||
}
|
||||
|
||||
private function getBackendForItem(ITrashItem $item) {
|
||||
return $item->getTrashBackend();
|
||||
}
|
||||
|
||||
public function listTrashFolder(ITrashItem $folder): array {
|
||||
return $this->getBackendForItem($folder)->listTrashFolder($folder);
|
||||
}
|
||||
|
||||
public function restoreItem(ITrashItem $item) {
|
||||
return $this->getBackendForItem($item)->restoreItem($item);
|
||||
}
|
||||
|
||||
public function removeItem(ITrashItem $item) {
|
||||
$this->getBackendForItem($item)->removeItem($item);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IStorage $storage
|
||||
* @return ITrashBackend
|
||||
* @throws BackendNotFoundException
|
||||
*/
|
||||
public function getBackendForStorage(IStorage $storage): ITrashBackend {
|
||||
$fullType = get_class($storage);
|
||||
$foundType = array_reduce(array_keys($this->backends), function ($type, $registeredType) use ($storage) {
|
||||
if (
|
||||
$storage->instanceOfStorage($registeredType) &&
|
||||
($type === '' || is_subclass_of($registeredType, $type))
|
||||
) {
|
||||
return $registeredType;
|
||||
} else {
|
||||
return $type;
|
||||
}
|
||||
}, '');
|
||||
if ($foundType === '') {
|
||||
throw new BackendNotFoundException("Trash backend for $fullType not found");
|
||||
} else {
|
||||
return $this->backends[$foundType];
|
||||
}
|
||||
}
|
||||
|
||||
public function moveToTrash(IStorage $storage, string $internalPath): bool {
|
||||
if ($this->trashPaused) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$backend = $this->getBackendForStorage($storage);
|
||||
return $backend->moveToTrash($storage, $internalPath);
|
||||
} catch (BackendNotFoundException $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getTrashNodeById(IUser $user, int $fileId) {
|
||||
foreach ($this->backends as $backend) {
|
||||
$item = $backend->getTrashNodeById($user, $fileId);
|
||||
if ($item !== null) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function pauseTrash() {
|
||||
$this->trashPaused = true;
|
||||
}
|
||||
|
||||
public function resumeTrash() {
|
||||
$this->trashPaused = false;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue