diff options
author | Robin Appelman <robin@icewind.nl> | 2018-09-10 14:40:35 +0200 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2018-10-17 14:56:45 +0200 |
commit | 4adac445dc57d1ccc7f26e21018e1e731e5b1654 (patch) | |
tree | b3da061a9332a1ebca2809a61ea3cf186d9fb259 /apps/files_trashbin/lib/Trash | |
parent | 2634ceb35b72eac94e6bf4c61640036392c5f97f (diff) | |
download | nextcloud-server-4adac445dc57d1ccc7f26e21018e1e731e5b1654.tar.gz nextcloud-server-4adac445dc57d1ccc7f26e21018e1e731e5b1654.zip |
fix select statement
fix select statement
Make trashbin api modules
Apps can register trashbin backends for specific storages,
allowing them to modify trashbin behavior for storages
The old trashbin implementation has been wrapped in a "legacy" backend,
for future work this can be replaced with a new backend that better handles
shares while still keeping the legacy backend around to keep existing trash
from being accessible
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'apps/files_trashbin/lib/Trash')
-rw-r--r-- | apps/files_trashbin/lib/Trash/BackendNotFoundException.php | 26 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Trash/ITrashBackend.php | 78 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Trash/ITrashItem.php | 71 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Trash/ITrashManager.php | 56 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Trash/LegacyTrashBackend.php | 102 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Trash/TrashItem.php | 187 | ||||
-rw-r--r-- | apps/files_trashbin/lib/Trash/TrashManager.php | 115 |
7 files changed, 635 insertions, 0 deletions
diff --git a/apps/files_trashbin/lib/Trash/BackendNotFoundException.php b/apps/files_trashbin/lib/Trash/BackendNotFoundException.php new file mode 100644 index 00000000000..13289db7ab0 --- /dev/null +++ b/apps/files_trashbin/lib/Trash/BackendNotFoundException.php @@ -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 { + +} diff --git a/apps/files_trashbin/lib/Trash/ITrashBackend.php b/apps/files_trashbin/lib/Trash/ITrashBackend.php new file mode 100644 index 00000000000..35b16a00c22 --- /dev/null +++ b/apps/files_trashbin/lib/Trash/ITrashBackend.php @@ -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\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 IUser $user + * @param ITrashItem $folder + * @return ITrashItem[] + * @since 15.0.0 + */ + public function listTrashFolder(IUser $user, 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 IUser $user + * @param ITrashItem $item + * @return + * @since 15.0.0 + */ + public function removeItem(IUser $user, 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); +} diff --git a/apps/files_trashbin/lib/Trash/ITrashItem.php b/apps/files_trashbin/lib/Trash/ITrashItem.php new file mode 100644 index 00000000000..ed01894beef --- /dev/null +++ b/apps/files_trashbin/lib/Trash/ITrashItem.php @@ -0,0 +1,71 @@ +<?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 for the user + * + * @param IUser $user + * @return string + * @since 15.0.0 + */ + public function getOriginalLocation(IUser $user): 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; +} diff --git a/apps/files_trashbin/lib/Trash/ITrashManager.php b/apps/files_trashbin/lib/Trash/ITrashManager.php new file mode 100644 index 00000000000..fcd51f2d371 --- /dev/null +++ b/apps/files_trashbin/lib/Trash/ITrashManager.php @@ -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(); +} diff --git a/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php b/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php new file mode 100644 index 00000000000..210c84228f7 --- /dev/null +++ b/apps/files_trashbin/lib/Trash/LegacyTrashBackend.php @@ -0,0 +1,102 @@ +<?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\Storage\IStorage; +use OCP\IUser; + +class LegacyTrashBackend implements ITrashBackend { + /** @var array */ + private $deletedFiles = []; + + /** + * @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($user) . '/' . $file->getName(), + $file->getMTime(), + $parentTrashPath . '/' . $file->getName() . ($isRoot ? '.d' . $file->getMtime() : ''), + $file + ); + }, $items); + } + + public function listTrashRoot(IUser $user): array { + $entries = Helper::getTrashFiles('/', $user->getUID()); + return $this->mapTrashItems($entries, $user, null); + } + + public function listTrashFolder(IUser $user, ITrashItem $folder): array { + $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(IUser $user, ITrashItem $item) { + 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) { + 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; + } +} diff --git a/apps/files_trashbin/lib/Trash/TrashItem.php b/apps/files_trashbin/lib/Trash/TrashItem.php new file mode 100644 index 00000000000..6d1c6dc328d --- /dev/null +++ b/apps/files_trashbin/lib/Trash/TrashItem.php @@ -0,0 +1,187 @@ +<?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; + + /** + * TrashItem constructor. + * + * @param ITrashBackend $backend + * @param string $originalLocation + * @param int $deletedTime + * @param string $trashPath + * @param FileInfo $fileInfo + */ + public function __construct(ITrashBackend $backend, string $originalLocation, int $deletedTime, string $trashPath, FileInfo $fileInfo) { + $this->backend = $backend; + $this->orignalLocation = $originalLocation; + $this->deletedTime = $deletedTime; + $this->trashPath = $trashPath; + $this->fileInfo = $fileInfo; + } + + public function getTrashBackend(): ITrashBackend { + return $this->backend; + } + + public function getOriginalLocation(IUser $user): 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 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(); + } + + +} diff --git a/apps/files_trashbin/lib/Trash/TrashManager.php b/apps/files_trashbin/lib/Trash/TrashManager.php new file mode 100644 index 00000000000..3870271cc7e --- /dev/null +++ b/apps/files_trashbin/lib/Trash/TrashManager.php @@ -0,0 +1,115 @@ +<?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(IUser $user, ITrashItem $folder): array { + return $this->getBackendForItem($folder)->listTrashFolder($user, $folder); + } + + public function restoreItem(ITrashItem $item) { + return $this->getBackendForItem($item)->restoreItem($item); + } + + public function removeItem(IUser $user, ITrashItem $item) { + $this->getBackendForItem($item)->removeItem($user, $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 ($fullType) { + if ( + is_subclass_of($fullType, $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) { + if ($this->trashPaused) { + return false; + } + try { + $backend = $this->getBackendForStorage($storage); + return $backend->moveToTrash($storage, $internalPath); + } catch (BackendNotFoundException $e) { + return false; + } + } + + public function pauseTrash() { + $this->trashPaused = true; + } + + public function resumeTrash() { + $this->trashPaused = false; + } +} |