@@ -44,7 +44,7 @@ |
||
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | - * @param array $items |
|
| 47 | + * @param FileInfo[] $items |
|
| 48 | 48 | * @param IUser $user |
| 49 | 49 | * @param ITrashItem $parent |
| 50 | 50 | * @return ITrashItem[] |
@@ -33,96 +33,96 @@ |
||
| 33 | 33 | use OCP\IUser; |
| 34 | 34 | |
| 35 | 35 | class LegacyTrashBackend implements ITrashBackend { |
| 36 | - /** @var array */ |
|
| 37 | - private $deletedFiles = []; |
|
| 36 | + /** @var array */ |
|
| 37 | + private $deletedFiles = []; |
|
| 38 | 38 | |
| 39 | - /** @var IRootFolder */ |
|
| 40 | - private $rootFolder; |
|
| 39 | + /** @var IRootFolder */ |
|
| 40 | + private $rootFolder; |
|
| 41 | 41 | |
| 42 | - public function __construct(IRootFolder $rootFolder) { |
|
| 43 | - $this->rootFolder = $rootFolder; |
|
| 44 | - } |
|
| 42 | + public function __construct(IRootFolder $rootFolder) { |
|
| 43 | + $this->rootFolder = $rootFolder; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @param array $items |
|
| 48 | - * @param IUser $user |
|
| 49 | - * @param ITrashItem $parent |
|
| 50 | - * @return ITrashItem[] |
|
| 51 | - */ |
|
| 52 | - private function mapTrashItems(array $items, IUser $user, ITrashItem $parent = null): array { |
|
| 53 | - $parentTrashPath = ($parent instanceof ITrashItem) ? $parent->getTrashPath() : ''; |
|
| 54 | - $isRoot = $parent === null; |
|
| 55 | - return array_map(function (FileInfo $file) use ($parent, $parentTrashPath, $isRoot, $user) { |
|
| 56 | - return new TrashItem( |
|
| 57 | - $this, |
|
| 58 | - $isRoot ? $file['extraData'] : $parent->getOriginalLocation() . '/' . $file->getName(), |
|
| 59 | - $file->getMTime(), |
|
| 60 | - $parentTrashPath . '/' . $file->getName() . ($isRoot ? '.d' . $file->getMtime() : ''), |
|
| 61 | - $file, |
|
| 62 | - $user |
|
| 63 | - ); |
|
| 64 | - }, $items); |
|
| 65 | - } |
|
| 46 | + /** |
|
| 47 | + * @param array $items |
|
| 48 | + * @param IUser $user |
|
| 49 | + * @param ITrashItem $parent |
|
| 50 | + * @return ITrashItem[] |
|
| 51 | + */ |
|
| 52 | + private function mapTrashItems(array $items, IUser $user, ITrashItem $parent = null): array { |
|
| 53 | + $parentTrashPath = ($parent instanceof ITrashItem) ? $parent->getTrashPath() : ''; |
|
| 54 | + $isRoot = $parent === null; |
|
| 55 | + return array_map(function (FileInfo $file) use ($parent, $parentTrashPath, $isRoot, $user) { |
|
| 56 | + return new TrashItem( |
|
| 57 | + $this, |
|
| 58 | + $isRoot ? $file['extraData'] : $parent->getOriginalLocation() . '/' . $file->getName(), |
|
| 59 | + $file->getMTime(), |
|
| 60 | + $parentTrashPath . '/' . $file->getName() . ($isRoot ? '.d' . $file->getMtime() : ''), |
|
| 61 | + $file, |
|
| 62 | + $user |
|
| 63 | + ); |
|
| 64 | + }, $items); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - public function listTrashRoot(IUser $user): array { |
|
| 68 | - $entries = Helper::getTrashFiles('/', $user->getUID()); |
|
| 69 | - return $this->mapTrashItems($entries, $user); |
|
| 70 | - } |
|
| 67 | + public function listTrashRoot(IUser $user): array { |
|
| 68 | + $entries = Helper::getTrashFiles('/', $user->getUID()); |
|
| 69 | + return $this->mapTrashItems($entries, $user); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - public function listTrashFolder(ITrashItem $folder): array { |
|
| 73 | - $user = $folder->getUser(); |
|
| 74 | - $entries = Helper::getTrashFiles($folder->getTrashPath(), $user->getUID()); |
|
| 75 | - return $this->mapTrashItems($entries, $user ,$folder); |
|
| 76 | - } |
|
| 72 | + public function listTrashFolder(ITrashItem $folder): array { |
|
| 73 | + $user = $folder->getUser(); |
|
| 74 | + $entries = Helper::getTrashFiles($folder->getTrashPath(), $user->getUID()); |
|
| 75 | + return $this->mapTrashItems($entries, $user ,$folder); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - public function restoreItem(ITrashItem $item) { |
|
| 79 | - Trashbin::restore($item->getTrashPath(), $item->getName(), $item->isRootItem() ? $item->getDeletedTime() : null); |
|
| 80 | - } |
|
| 78 | + public function restoreItem(ITrashItem $item) { |
|
| 79 | + Trashbin::restore($item->getTrashPath(), $item->getName(), $item->isRootItem() ? $item->getDeletedTime() : null); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - public function removeItem(ITrashItem $item) { |
|
| 83 | - $user = $item->getUser(); |
|
| 84 | - if ($item->isRootItem()) { |
|
| 85 | - $path = substr($item->getTrashPath(), 0, -strlen('.d' . $item->getDeletedTime())); |
|
| 86 | - Trashbin::delete($path, $user->getUID(), $item->getDeletedTime()); |
|
| 87 | - } else { |
|
| 88 | - Trashbin::delete($item->getTrashPath(), $user->getUID(), null); |
|
| 89 | - } |
|
| 82 | + public function removeItem(ITrashItem $item) { |
|
| 83 | + $user = $item->getUser(); |
|
| 84 | + if ($item->isRootItem()) { |
|
| 85 | + $path = substr($item->getTrashPath(), 0, -strlen('.d' . $item->getDeletedTime())); |
|
| 86 | + Trashbin::delete($path, $user->getUID(), $item->getDeletedTime()); |
|
| 87 | + } else { |
|
| 88 | + Trashbin::delete($item->getTrashPath(), $user->getUID(), null); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - public function moveToTrash(IStorage $storage, string $internalPath): bool { |
|
| 94 | - if (!$storage instanceof Storage) { |
|
| 95 | - return false; |
|
| 96 | - } |
|
| 97 | - $normalized = Filesystem::normalizePath($storage->getMountPoint() . '/' . $internalPath, true, false, true); |
|
| 98 | - $view = Filesystem::getView(); |
|
| 99 | - if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) { |
|
| 100 | - $this->deletedFiles[$normalized] = $normalized; |
|
| 101 | - if ($filesPath = $view->getRelativePath($normalized)) { |
|
| 102 | - $filesPath = trim($filesPath, '/'); |
|
| 103 | - $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath); |
|
| 104 | - } else { |
|
| 105 | - $result = false; |
|
| 106 | - } |
|
| 107 | - unset($this->deletedFiles[$normalized]); |
|
| 108 | - } else { |
|
| 109 | - $result = false; |
|
| 110 | - } |
|
| 93 | + public function moveToTrash(IStorage $storage, string $internalPath): bool { |
|
| 94 | + if (!$storage instanceof Storage) { |
|
| 95 | + return false; |
|
| 96 | + } |
|
| 97 | + $normalized = Filesystem::normalizePath($storage->getMountPoint() . '/' . $internalPath, true, false, true); |
|
| 98 | + $view = Filesystem::getView(); |
|
| 99 | + if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) { |
|
| 100 | + $this->deletedFiles[$normalized] = $normalized; |
|
| 101 | + if ($filesPath = $view->getRelativePath($normalized)) { |
|
| 102 | + $filesPath = trim($filesPath, '/'); |
|
| 103 | + $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath); |
|
| 104 | + } else { |
|
| 105 | + $result = false; |
|
| 106 | + } |
|
| 107 | + unset($this->deletedFiles[$normalized]); |
|
| 108 | + } else { |
|
| 109 | + $result = false; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - return $result; |
|
| 113 | - } |
|
| 112 | + return $result; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - public function getTrashNodeById(IUser $user, int $fileId) { |
|
| 116 | - try { |
|
| 117 | - $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
|
| 118 | - $trash = $userFolder->getParent()->get('files_trashbin/files'); |
|
| 119 | - $trashFiles = $trash->getById($fileId); |
|
| 120 | - if (!$trashFiles) { |
|
| 121 | - return null; |
|
| 122 | - } |
|
| 123 | - return $trashFiles ? array_pop($trashFiles) : null; |
|
| 124 | - } catch (NotFoundException $e) { |
|
| 125 | - return null; |
|
| 126 | - } |
|
| 127 | - } |
|
| 115 | + public function getTrashNodeById(IUser $user, int $fileId) { |
|
| 116 | + try { |
|
| 117 | + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
|
| 118 | + $trash = $userFolder->getParent()->get('files_trashbin/files'); |
|
| 119 | + $trashFiles = $trash->getById($fileId); |
|
| 120 | + if (!$trashFiles) { |
|
| 121 | + return null; |
|
| 122 | + } |
|
| 123 | + return $trashFiles ? array_pop($trashFiles) : null; |
|
| 124 | + } catch (NotFoundException $e) { |
|
| 125 | + return null; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | 128 | } |
@@ -52,956 +52,956 @@ |
||
| 52 | 52 | |
| 53 | 53 | class Trashbin { |
| 54 | 54 | |
| 55 | - // unit: percentage; 50% of available disk space/quota |
|
| 56 | - const DEFAULTMAXSIZE = 50; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Whether versions have already be rescanned during this PHP request |
|
| 60 | - * |
|
| 61 | - * @var bool |
|
| 62 | - */ |
|
| 63 | - private static $scannedVersions = false; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Ensure we don't need to scan the file during the move to trash |
|
| 67 | - * by triggering the scan in the pre-hook |
|
| 68 | - * |
|
| 69 | - * @param array $params |
|
| 70 | - */ |
|
| 71 | - public static function ensureFileScannedHook($params) { |
|
| 72 | - try { |
|
| 73 | - self::getUidAndFilename($params['path']); |
|
| 74 | - } catch (NotFoundException $e) { |
|
| 75 | - // nothing to scan for non existing files |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * get the UID of the owner of the file and the path to the file relative to |
|
| 81 | - * owners files folder |
|
| 82 | - * |
|
| 83 | - * @param string $filename |
|
| 84 | - * @return array |
|
| 85 | - * @throws \OC\User\NoUserException |
|
| 86 | - */ |
|
| 87 | - public static function getUidAndFilename($filename) { |
|
| 88 | - $uid = Filesystem::getOwner($filename); |
|
| 89 | - $userManager = \OC::$server->getUserManager(); |
|
| 90 | - // if the user with the UID doesn't exists, e.g. because the UID points |
|
| 91 | - // to a remote user with a federated cloud ID we use the current logged-in |
|
| 92 | - // user. We need a valid local user to move the file to the right trash bin |
|
| 93 | - if (!$userManager->userExists($uid)) { |
|
| 94 | - $uid = User::getUser(); |
|
| 95 | - } |
|
| 96 | - if (!$uid) { |
|
| 97 | - // no owner, usually because of share link from ext storage |
|
| 98 | - return [null, null]; |
|
| 99 | - } |
|
| 100 | - Filesystem::initMountPoints($uid); |
|
| 101 | - if ($uid !== User::getUser()) { |
|
| 102 | - $info = Filesystem::getFileInfo($filename); |
|
| 103 | - $ownerView = new View('/' . $uid . '/files'); |
|
| 104 | - try { |
|
| 105 | - $filename = $ownerView->getPath($info['fileid']); |
|
| 106 | - } catch (NotFoundException $e) { |
|
| 107 | - $filename = null; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - return [$uid, $filename]; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * get original location of files for user |
|
| 115 | - * |
|
| 116 | - * @param string $user |
|
| 117 | - * @return array (filename => array (timestamp => original location)) |
|
| 118 | - */ |
|
| 119 | - public static function getLocations($user) { |
|
| 120 | - $query = \OC_DB::prepare('SELECT `id`, `timestamp`, `location`' |
|
| 121 | - . ' FROM `*PREFIX*files_trash` WHERE `user`=?'); |
|
| 122 | - $result = $query->execute(array($user)); |
|
| 123 | - $array = array(); |
|
| 124 | - while ($row = $result->fetchRow()) { |
|
| 125 | - if (isset($array[$row['id']])) { |
|
| 126 | - $array[$row['id']][$row['timestamp']] = $row['location']; |
|
| 127 | - } else { |
|
| 128 | - $array[$row['id']] = array($row['timestamp'] => $row['location']); |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - return $array; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * get original location of file |
|
| 136 | - * |
|
| 137 | - * @param string $user |
|
| 138 | - * @param string $filename |
|
| 139 | - * @param string $timestamp |
|
| 140 | - * @return string original location |
|
| 141 | - */ |
|
| 142 | - public static function getLocation($user, $filename, $timestamp) { |
|
| 143 | - $query = \OC_DB::prepare('SELECT `location` FROM `*PREFIX*files_trash`' |
|
| 144 | - . ' WHERE `user`=? AND `id`=? AND `timestamp`=?'); |
|
| 145 | - $result = $query->execute(array($user, $filename, $timestamp))->fetchAll(); |
|
| 146 | - if (isset($result[0]['location'])) { |
|
| 147 | - return $result[0]['location']; |
|
| 148 | - } else { |
|
| 149 | - return false; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - private static function setUpTrash($user) { |
|
| 154 | - $view = new View('/' . $user); |
|
| 155 | - if (!$view->is_dir('files_trashbin')) { |
|
| 156 | - $view->mkdir('files_trashbin'); |
|
| 157 | - } |
|
| 158 | - if (!$view->is_dir('files_trashbin/files')) { |
|
| 159 | - $view->mkdir('files_trashbin/files'); |
|
| 160 | - } |
|
| 161 | - if (!$view->is_dir('files_trashbin/versions')) { |
|
| 162 | - $view->mkdir('files_trashbin/versions'); |
|
| 163 | - } |
|
| 164 | - if (!$view->is_dir('files_trashbin/keys')) { |
|
| 165 | - $view->mkdir('files_trashbin/keys'); |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * copy file to owners trash |
|
| 172 | - * |
|
| 173 | - * @param string $sourcePath |
|
| 174 | - * @param string $owner |
|
| 175 | - * @param string $targetPath |
|
| 176 | - * @param $user |
|
| 177 | - * @param integer $timestamp |
|
| 178 | - */ |
|
| 179 | - private static function copyFilesToUser($sourcePath, $owner, $targetPath, $user, $timestamp) { |
|
| 180 | - self::setUpTrash($owner); |
|
| 181 | - |
|
| 182 | - $targetFilename = basename($targetPath); |
|
| 183 | - $targetLocation = dirname($targetPath); |
|
| 184 | - |
|
| 185 | - $sourceFilename = basename($sourcePath); |
|
| 186 | - |
|
| 187 | - $view = new View('/'); |
|
| 188 | - |
|
| 189 | - $target = $user . '/files_trashbin/files/' . $targetFilename . '.d' . $timestamp; |
|
| 190 | - $source = $owner . '/files_trashbin/files/' . $sourceFilename . '.d' . $timestamp; |
|
| 191 | - self::copy_recursive($source, $target, $view); |
|
| 192 | - |
|
| 193 | - |
|
| 194 | - if ($view->file_exists($target)) { |
|
| 195 | - $query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)"); |
|
| 196 | - $result = $query->execute(array($targetFilename, $timestamp, $targetLocation, $user)); |
|
| 197 | - if (!$result) { |
|
| 198 | - \OC::$server->getLogger()->error('trash bin database couldn\'t be updated for the files owner', ['app' => 'files_trashbin']); |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * move file to the trash bin |
|
| 206 | - * |
|
| 207 | - * @param string $file_path path to the deleted file/directory relative to the files root directory |
|
| 208 | - * @param bool $ownerOnly delete for owner only (if file gets moved out of a shared folder) |
|
| 209 | - * |
|
| 210 | - * @return bool |
|
| 211 | - */ |
|
| 212 | - public static function move2trash($file_path, $ownerOnly = false) { |
|
| 213 | - // get the user for which the filesystem is setup |
|
| 214 | - $root = Filesystem::getRoot(); |
|
| 215 | - list(, $user) = explode('/', $root); |
|
| 216 | - list($owner, $ownerPath) = self::getUidAndFilename($file_path); |
|
| 217 | - |
|
| 218 | - // if no owner found (ex: ext storage + share link), will use the current user's trashbin then |
|
| 219 | - if (is_null($owner)) { |
|
| 220 | - $owner = $user; |
|
| 221 | - $ownerPath = $file_path; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $ownerView = new View('/' . $owner); |
|
| 225 | - // file has been deleted in between |
|
| 226 | - if (is_null($ownerPath) || $ownerPath === '' || !$ownerView->file_exists('/files/' . $ownerPath)) { |
|
| 227 | - return true; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - self::setUpTrash($user); |
|
| 231 | - if ($owner !== $user) { |
|
| 232 | - // also setup for owner |
|
| 233 | - self::setUpTrash($owner); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - $path_parts = pathinfo($ownerPath); |
|
| 237 | - |
|
| 238 | - $filename = $path_parts['basename']; |
|
| 239 | - $location = $path_parts['dirname']; |
|
| 240 | - $timestamp = time(); |
|
| 241 | - |
|
| 242 | - // disable proxy to prevent recursive calls |
|
| 243 | - $trashPath = '/files_trashbin/files/' . $filename . '.d' . $timestamp; |
|
| 244 | - |
|
| 245 | - /** @var \OC\Files\Storage\Storage $trashStorage */ |
|
| 246 | - list($trashStorage, $trashInternalPath) = $ownerView->resolvePath($trashPath); |
|
| 247 | - /** @var \OC\Files\Storage\Storage $sourceStorage */ |
|
| 248 | - list($sourceStorage, $sourceInternalPath) = $ownerView->resolvePath('/files/' . $ownerPath); |
|
| 249 | - try { |
|
| 250 | - $moveSuccessful = true; |
|
| 251 | - if ($trashStorage->file_exists($trashInternalPath)) { |
|
| 252 | - $trashStorage->unlink($trashInternalPath); |
|
| 253 | - } |
|
| 254 | - $trashStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); |
|
| 255 | - } catch (\OCA\Files_Trashbin\Exceptions\CopyRecursiveException $e) { |
|
| 256 | - $moveSuccessful = false; |
|
| 257 | - if ($trashStorage->file_exists($trashInternalPath)) { |
|
| 258 | - $trashStorage->unlink($trashInternalPath); |
|
| 259 | - } |
|
| 260 | - \OC::$server->getLogger()->error('Couldn\'t move ' . $file_path . ' to the trash bin', ['app' => 'files_trashbin']); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - if ($sourceStorage->file_exists($sourceInternalPath)) { // failed to delete the original file, abort |
|
| 264 | - if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
| 265 | - $sourceStorage->rmdir($sourceInternalPath); |
|
| 266 | - } else { |
|
| 267 | - $sourceStorage->unlink($sourceInternalPath); |
|
| 268 | - } |
|
| 269 | - return false; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - $trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); |
|
| 273 | - |
|
| 274 | - if ($moveSuccessful) { |
|
| 275 | - $query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)"); |
|
| 276 | - $result = $query->execute(array($filename, $timestamp, $location, $owner)); |
|
| 277 | - if (!$result) { |
|
| 278 | - \OC::$server->getLogger()->error('trash bin database couldn\'t be updated', ['app' => 'files_trashbin']); |
|
| 279 | - } |
|
| 280 | - \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_moveToTrash', array('filePath' => Filesystem::normalizePath($file_path), |
|
| 281 | - 'trashPath' => Filesystem::normalizePath($filename . '.d' . $timestamp))); |
|
| 282 | - |
|
| 283 | - self::retainVersions($filename, $owner, $ownerPath, $timestamp); |
|
| 284 | - |
|
| 285 | - // if owner !== user we need to also add a copy to the users trash |
|
| 286 | - if ($user !== $owner && $ownerOnly === false) { |
|
| 287 | - self::copyFilesToUser($ownerPath, $owner, $file_path, $user, $timestamp); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - self::scheduleExpire($user); |
|
| 292 | - |
|
| 293 | - // if owner !== user we also need to update the owners trash size |
|
| 294 | - if ($owner !== $user) { |
|
| 295 | - self::scheduleExpire($owner); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - return $moveSuccessful; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Move file versions to trash so that they can be restored later |
|
| 303 | - * |
|
| 304 | - * @param string $filename of deleted file |
|
| 305 | - * @param string $owner owner user id |
|
| 306 | - * @param string $ownerPath path relative to the owner's home storage |
|
| 307 | - * @param integer $timestamp when the file was deleted |
|
| 308 | - */ |
|
| 309 | - private static function retainVersions($filename, $owner, $ownerPath, $timestamp) { |
|
| 310 | - if (\OCP\App::isEnabled('files_versions') && !empty($ownerPath)) { |
|
| 311 | - |
|
| 312 | - $user = User::getUser(); |
|
| 313 | - $rootView = new View('/'); |
|
| 314 | - |
|
| 315 | - if ($rootView->is_dir($owner . '/files_versions/' . $ownerPath)) { |
|
| 316 | - if ($owner !== $user) { |
|
| 317 | - self::copy_recursive($owner . '/files_versions/' . $ownerPath, $owner . '/files_trashbin/versions/' . basename($ownerPath) . '.d' . $timestamp, $rootView); |
|
| 318 | - } |
|
| 319 | - self::move($rootView, $owner . '/files_versions/' . $ownerPath, $user . '/files_trashbin/versions/' . $filename . '.d' . $timestamp); |
|
| 320 | - } else if ($versions = \OCA\Files_Versions\Storage::getVersions($owner, $ownerPath)) { |
|
| 321 | - |
|
| 322 | - foreach ($versions as $v) { |
|
| 323 | - if ($owner !== $user) { |
|
| 324 | - self::copy($rootView, $owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $owner . '/files_trashbin/versions/' . $v['name'] . '.v' . $v['version'] . '.d' . $timestamp); |
|
| 325 | - } |
|
| 326 | - self::move($rootView, $owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $user . '/files_trashbin/versions/' . $filename . '.v' . $v['version'] . '.d' . $timestamp); |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Move a file or folder on storage level |
|
| 334 | - * |
|
| 335 | - * @param View $view |
|
| 336 | - * @param string $source |
|
| 337 | - * @param string $target |
|
| 338 | - * @return bool |
|
| 339 | - */ |
|
| 340 | - private static function move(View $view, $source, $target) { |
|
| 341 | - /** @var \OC\Files\Storage\Storage $sourceStorage */ |
|
| 342 | - list($sourceStorage, $sourceInternalPath) = $view->resolvePath($source); |
|
| 343 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 344 | - list($targetStorage, $targetInternalPath) = $view->resolvePath($target); |
|
| 345 | - /** @var \OC\Files\Storage\Storage $ownerTrashStorage */ |
|
| 346 | - |
|
| 347 | - $result = $targetStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 348 | - if ($result) { |
|
| 349 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 350 | - } |
|
| 351 | - return $result; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Copy a file or folder on storage level |
|
| 356 | - * |
|
| 357 | - * @param View $view |
|
| 358 | - * @param string $source |
|
| 359 | - * @param string $target |
|
| 360 | - * @return bool |
|
| 361 | - */ |
|
| 362 | - private static function copy(View $view, $source, $target) { |
|
| 363 | - /** @var \OC\Files\Storage\Storage $sourceStorage */ |
|
| 364 | - list($sourceStorage, $sourceInternalPath) = $view->resolvePath($source); |
|
| 365 | - /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 366 | - list($targetStorage, $targetInternalPath) = $view->resolvePath($target); |
|
| 367 | - /** @var \OC\Files\Storage\Storage $ownerTrashStorage */ |
|
| 368 | - |
|
| 369 | - $result = $targetStorage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 370 | - if ($result) { |
|
| 371 | - $targetStorage->getUpdater()->update($targetInternalPath); |
|
| 372 | - } |
|
| 373 | - return $result; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Restore a file or folder from trash bin |
|
| 378 | - * |
|
| 379 | - * @param string $file path to the deleted file/folder relative to "files_trashbin/files/", |
|
| 380 | - * including the timestamp suffix ".d12345678" |
|
| 381 | - * @param string $filename name of the file/folder |
|
| 382 | - * @param int $timestamp time when the file/folder was deleted |
|
| 383 | - * |
|
| 384 | - * @return bool true on success, false otherwise |
|
| 385 | - */ |
|
| 386 | - public static function restore($file, $filename, $timestamp) { |
|
| 387 | - $user = User::getUser(); |
|
| 388 | - $view = new View('/' . $user); |
|
| 389 | - |
|
| 390 | - $location = ''; |
|
| 391 | - if ($timestamp) { |
|
| 392 | - $location = self::getLocation($user, $filename, $timestamp); |
|
| 393 | - if ($location === false) { |
|
| 394 | - \OC::$server->getLogger()->error('trash bin database inconsistent! ($user: ' . $user . ' $filename: ' . $filename . ', $timestamp: ' . $timestamp . ')', ['app' => 'files_trashbin']); |
|
| 395 | - } else { |
|
| 396 | - // if location no longer exists, restore file in the root directory |
|
| 397 | - if ($location !== '/' && |
|
| 398 | - (!$view->is_dir('files/' . $location) || |
|
| 399 | - !$view->isCreatable('files/' . $location)) |
|
| 400 | - ) { |
|
| 401 | - $location = ''; |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - // we need a extension in case a file/dir with the same name already exists |
|
| 407 | - $uniqueFilename = self::getUniqueFilename($location, $filename, $view); |
|
| 408 | - |
|
| 409 | - $source = Filesystem::normalizePath('files_trashbin/files/' . $file); |
|
| 410 | - $target = Filesystem::normalizePath('files/' . $location . '/' . $uniqueFilename); |
|
| 411 | - if (!$view->file_exists($source)) { |
|
| 412 | - return false; |
|
| 413 | - } |
|
| 414 | - $mtime = $view->filemtime($source); |
|
| 415 | - |
|
| 416 | - // restore file |
|
| 417 | - $restoreResult = $view->rename($source, $target); |
|
| 418 | - |
|
| 419 | - // handle the restore result |
|
| 420 | - if ($restoreResult) { |
|
| 421 | - $fakeRoot = $view->getRoot(); |
|
| 422 | - $view->chroot('/' . $user . '/files'); |
|
| 423 | - $view->touch('/' . $location . '/' . $uniqueFilename, $mtime); |
|
| 424 | - $view->chroot($fakeRoot); |
|
| 425 | - \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', array('filePath' => Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename), |
|
| 426 | - 'trashPath' => Filesystem::normalizePath($file))); |
|
| 427 | - |
|
| 428 | - self::restoreVersions($view, $file, $filename, $uniqueFilename, $location, $timestamp); |
|
| 429 | - |
|
| 430 | - if ($timestamp) { |
|
| 431 | - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=? AND `id`=? AND `timestamp`=?'); |
|
| 432 | - $query->execute(array($user, $filename, $timestamp)); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - return true; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - return false; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * restore versions from trash bin |
|
| 443 | - * |
|
| 444 | - * @param View $view file view |
|
| 445 | - * @param string $file complete path to file |
|
| 446 | - * @param string $filename name of file once it was deleted |
|
| 447 | - * @param string $uniqueFilename new file name to restore the file without overwriting existing files |
|
| 448 | - * @param string $location location if file |
|
| 449 | - * @param int $timestamp deletion time |
|
| 450 | - * @return false|null |
|
| 451 | - */ |
|
| 452 | - private static function restoreVersions(View $view, $file, $filename, $uniqueFilename, $location, $timestamp) { |
|
| 453 | - |
|
| 454 | - if (\OCP\App::isEnabled('files_versions')) { |
|
| 455 | - |
|
| 456 | - $user = User::getUser(); |
|
| 457 | - $rootView = new View('/'); |
|
| 458 | - |
|
| 459 | - $target = Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename); |
|
| 460 | - |
|
| 461 | - list($owner, $ownerPath) = self::getUidAndFilename($target); |
|
| 462 | - |
|
| 463 | - // file has been deleted in between |
|
| 464 | - if (empty($ownerPath)) { |
|
| 465 | - return false; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - if ($timestamp) { |
|
| 469 | - $versionedFile = $filename; |
|
| 470 | - } else { |
|
| 471 | - $versionedFile = $file; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - if ($view->is_dir('/files_trashbin/versions/' . $file)) { |
|
| 475 | - $rootView->rename(Filesystem::normalizePath($user . '/files_trashbin/versions/' . $file), Filesystem::normalizePath($owner . '/files_versions/' . $ownerPath)); |
|
| 476 | - } else if ($versions = self::getVersionsFromTrash($versionedFile, $timestamp, $user)) { |
|
| 477 | - foreach ($versions as $v) { |
|
| 478 | - if ($timestamp) { |
|
| 479 | - $rootView->rename($user . '/files_trashbin/versions/' . $versionedFile . '.v' . $v . '.d' . $timestamp, $owner . '/files_versions/' . $ownerPath . '.v' . $v); |
|
| 480 | - } else { |
|
| 481 | - $rootView->rename($user . '/files_trashbin/versions/' . $versionedFile . '.v' . $v, $owner . '/files_versions/' . $ownerPath . '.v' . $v); |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - } |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * delete all files from the trash |
|
| 490 | - */ |
|
| 491 | - public static function deleteAll() { |
|
| 492 | - $user = User::getUser(); |
|
| 493 | - $userRoot = \OC::$server->getUserFolder($user)->getParent(); |
|
| 494 | - $view = new View('/' . $user); |
|
| 495 | - $fileInfos = $view->getDirectoryContent('files_trashbin/files'); |
|
| 496 | - |
|
| 497 | - try { |
|
| 498 | - $trash = $userRoot->get('files_trashbin'); |
|
| 499 | - } catch (NotFoundException $e) { |
|
| 500 | - return false; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - // Array to store the relative path in (after the file is deleted, the view won't be able to relativise the path anymore) |
|
| 504 | - $filePaths = array(); |
|
| 505 | - foreach($fileInfos as $fileInfo){ |
|
| 506 | - $filePaths[] = $view->getRelativePath($fileInfo->getPath()); |
|
| 507 | - } |
|
| 508 | - unset($fileInfos); // save memory |
|
| 509 | - |
|
| 510 | - // Bulk PreDelete-Hook |
|
| 511 | - \OC_Hook::emit('\OCP\Trashbin', 'preDeleteAll', array('paths' => $filePaths)); |
|
| 512 | - |
|
| 513 | - // Single-File Hooks |
|
| 514 | - foreach($filePaths as $path){ |
|
| 515 | - self::emitTrashbinPreDelete($path); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - // actual file deletion |
|
| 519 | - $trash->delete(); |
|
| 520 | - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=?'); |
|
| 521 | - $query->execute(array($user)); |
|
| 522 | - |
|
| 523 | - // Bulk PostDelete-Hook |
|
| 524 | - \OC_Hook::emit('\OCP\Trashbin', 'deleteAll', array('paths' => $filePaths)); |
|
| 525 | - |
|
| 526 | - // Single-File Hooks |
|
| 527 | - foreach($filePaths as $path){ |
|
| 528 | - self::emitTrashbinPostDelete($path); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - $trash = $userRoot->newFolder('files_trashbin'); |
|
| 532 | - $trash->newFolder('files'); |
|
| 533 | - |
|
| 534 | - return true; |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * wrapper function to emit the 'preDelete' hook of \OCP\Trashbin before a file is deleted |
|
| 539 | - * @param string $path |
|
| 540 | - */ |
|
| 541 | - protected static function emitTrashbinPreDelete($path){ |
|
| 542 | - \OC_Hook::emit('\OCP\Trashbin', 'preDelete', array('path' => $path)); |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * wrapper function to emit the 'delete' hook of \OCP\Trashbin after a file has been deleted |
|
| 547 | - * @param string $path |
|
| 548 | - */ |
|
| 549 | - protected static function emitTrashbinPostDelete($path){ |
|
| 550 | - \OC_Hook::emit('\OCP\Trashbin', 'delete', array('path' => $path)); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * delete file from trash bin permanently |
|
| 555 | - * |
|
| 556 | - * @param string $filename path to the file |
|
| 557 | - * @param string $user |
|
| 558 | - * @param int $timestamp of deletion time |
|
| 559 | - * |
|
| 560 | - * @return int size of deleted files |
|
| 561 | - */ |
|
| 562 | - public static function delete($filename, $user, $timestamp = null) { |
|
| 563 | - $userRoot = \OC::$server->getUserFolder($user)->getParent(); |
|
| 564 | - $view = new View('/' . $user); |
|
| 565 | - $size = 0; |
|
| 566 | - |
|
| 567 | - if ($timestamp) { |
|
| 568 | - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=? AND `id`=? AND `timestamp`=?'); |
|
| 569 | - $query->execute(array($user, $filename, $timestamp)); |
|
| 570 | - $file = $filename . '.d' . $timestamp; |
|
| 571 | - } else { |
|
| 572 | - $file = $filename; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - $size += self::deleteVersions($view, $file, $filename, $timestamp, $user); |
|
| 576 | - |
|
| 577 | - try { |
|
| 578 | - $node = $userRoot->get('/files_trashbin/files/' . $file); |
|
| 579 | - } catch (NotFoundException $e) { |
|
| 580 | - return $size; |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - if ($node instanceof Folder) { |
|
| 584 | - $size += self::calculateSize(new View('/' . $user . '/files_trashbin/files/' . $file)); |
|
| 585 | - } else if ($node instanceof File) { |
|
| 586 | - $size += $view->filesize('/files_trashbin/files/' . $file); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - self::emitTrashbinPreDelete('/files_trashbin/files/' . $file); |
|
| 590 | - $node->delete(); |
|
| 591 | - self::emitTrashbinPostDelete('/files_trashbin/files/' . $file); |
|
| 592 | - |
|
| 593 | - return $size; |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - /** |
|
| 597 | - * @param View $view |
|
| 598 | - * @param string $file |
|
| 599 | - * @param string $filename |
|
| 600 | - * @param integer|null $timestamp |
|
| 601 | - * @param string $user |
|
| 602 | - * @return int |
|
| 603 | - */ |
|
| 604 | - private static function deleteVersions(View $view, $file, $filename, $timestamp, $user) { |
|
| 605 | - $size = 0; |
|
| 606 | - if (\OCP\App::isEnabled('files_versions')) { |
|
| 607 | - if ($view->is_dir('files_trashbin/versions/' . $file)) { |
|
| 608 | - $size += self::calculateSize(new View('/' . $user . '/files_trashbin/versions/' . $file)); |
|
| 609 | - $view->unlink('files_trashbin/versions/' . $file); |
|
| 610 | - } else if ($versions = self::getVersionsFromTrash($filename, $timestamp, $user)) { |
|
| 611 | - foreach ($versions as $v) { |
|
| 612 | - if ($timestamp) { |
|
| 613 | - $size += $view->filesize('/files_trashbin/versions/' . $filename . '.v' . $v . '.d' . $timestamp); |
|
| 614 | - $view->unlink('/files_trashbin/versions/' . $filename . '.v' . $v . '.d' . $timestamp); |
|
| 615 | - } else { |
|
| 616 | - $size += $view->filesize('/files_trashbin/versions/' . $filename . '.v' . $v); |
|
| 617 | - $view->unlink('/files_trashbin/versions/' . $filename . '.v' . $v); |
|
| 618 | - } |
|
| 619 | - } |
|
| 620 | - } |
|
| 621 | - } |
|
| 622 | - return $size; |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * check to see whether a file exists in trashbin |
|
| 627 | - * |
|
| 628 | - * @param string $filename path to the file |
|
| 629 | - * @param int $timestamp of deletion time |
|
| 630 | - * @return bool true if file exists, otherwise false |
|
| 631 | - */ |
|
| 632 | - public static function file_exists($filename, $timestamp = null) { |
|
| 633 | - $user = User::getUser(); |
|
| 634 | - $view = new View('/' . $user); |
|
| 635 | - |
|
| 636 | - if ($timestamp) { |
|
| 637 | - $filename = $filename . '.d' . $timestamp; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - $target = Filesystem::normalizePath('files_trashbin/files/' . $filename); |
|
| 641 | - return $view->file_exists($target); |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * deletes used space for trash bin in db if user was deleted |
|
| 646 | - * |
|
| 647 | - * @param string $uid id of deleted user |
|
| 648 | - * @return bool result of db delete operation |
|
| 649 | - */ |
|
| 650 | - public static function deleteUser($uid) { |
|
| 651 | - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=?'); |
|
| 652 | - return $query->execute(array($uid)); |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - /** |
|
| 656 | - * calculate remaining free space for trash bin |
|
| 657 | - * |
|
| 658 | - * @param integer $trashbinSize current size of the trash bin |
|
| 659 | - * @param string $user |
|
| 660 | - * @return int available free space for trash bin |
|
| 661 | - */ |
|
| 662 | - private static function calculateFreeSpace($trashbinSize, $user) { |
|
| 663 | - $softQuota = true; |
|
| 664 | - $userObject = \OC::$server->getUserManager()->get($user); |
|
| 665 | - if(is_null($userObject)) { |
|
| 666 | - return 0; |
|
| 667 | - } |
|
| 668 | - $quota = $userObject->getQuota(); |
|
| 669 | - if ($quota === null || $quota === 'none') { |
|
| 670 | - $quota = Filesystem::free_space('/'); |
|
| 671 | - $softQuota = false; |
|
| 672 | - // inf or unknown free space |
|
| 673 | - if ($quota < 0) { |
|
| 674 | - $quota = PHP_INT_MAX; |
|
| 675 | - } |
|
| 676 | - } else { |
|
| 677 | - $quota = \OCP\Util::computerFileSize($quota); |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - // calculate available space for trash bin |
|
| 681 | - // subtract size of files and current trash bin size from quota |
|
| 682 | - if ($softQuota) { |
|
| 683 | - $userFolder = \OC::$server->getUserFolder($user); |
|
| 684 | - if(is_null($userFolder)) { |
|
| 685 | - return 0; |
|
| 686 | - } |
|
| 687 | - $free = $quota - $userFolder->getSize(); // remaining free space for user |
|
| 688 | - if ($free > 0) { |
|
| 689 | - $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $trashbinSize; // how much space can be used for versions |
|
| 690 | - } else { |
|
| 691 | - $availableSpace = $free - $trashbinSize; |
|
| 692 | - } |
|
| 693 | - } else { |
|
| 694 | - $availableSpace = $quota; |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - return $availableSpace; |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * resize trash bin if necessary after a new file was added to Nextcloud |
|
| 702 | - * |
|
| 703 | - * @param string $user user id |
|
| 704 | - */ |
|
| 705 | - public static function resizeTrash($user) { |
|
| 706 | - |
|
| 707 | - $size = self::getTrashbinSize($user); |
|
| 708 | - |
|
| 709 | - $freeSpace = self::calculateFreeSpace($size, $user); |
|
| 710 | - |
|
| 711 | - if ($freeSpace < 0) { |
|
| 712 | - self::scheduleExpire($user); |
|
| 713 | - } |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - /** |
|
| 717 | - * clean up the trash bin |
|
| 718 | - * |
|
| 719 | - * @param string $user |
|
| 720 | - */ |
|
| 721 | - public static function expire($user) { |
|
| 722 | - $trashBinSize = self::getTrashbinSize($user); |
|
| 723 | - $availableSpace = self::calculateFreeSpace($trashBinSize, $user); |
|
| 724 | - |
|
| 725 | - $dirContent = Helper::getTrashFiles('/', $user, 'mtime'); |
|
| 726 | - |
|
| 727 | - // delete all files older then $retention_obligation |
|
| 728 | - list($delSize, $count) = self::deleteExpiredFiles($dirContent, $user); |
|
| 729 | - |
|
| 730 | - $availableSpace += $delSize; |
|
| 731 | - |
|
| 732 | - // delete files from trash until we meet the trash bin size limit again |
|
| 733 | - self::deleteFiles(array_slice($dirContent, $count), $user, $availableSpace); |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * @param string $user |
|
| 738 | - */ |
|
| 739 | - private static function scheduleExpire($user) { |
|
| 740 | - // let the admin disable auto expire |
|
| 741 | - $application = new Application(); |
|
| 742 | - $expiration = $application->getContainer()->query('Expiration'); |
|
| 743 | - if ($expiration->isEnabled()) { |
|
| 744 | - \OC::$server->getCommandBus()->push(new Expire($user)); |
|
| 745 | - } |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - /** |
|
| 749 | - * if the size limit for the trash bin is reached, we delete the oldest |
|
| 750 | - * files in the trash bin until we meet the limit again |
|
| 751 | - * |
|
| 752 | - * @param array $files |
|
| 753 | - * @param string $user |
|
| 754 | - * @param int $availableSpace available disc space |
|
| 755 | - * @return int size of deleted files |
|
| 756 | - */ |
|
| 757 | - protected static function deleteFiles($files, $user, $availableSpace) { |
|
| 758 | - $application = new Application(); |
|
| 759 | - $expiration = $application->getContainer()->query('Expiration'); |
|
| 760 | - $size = 0; |
|
| 761 | - |
|
| 762 | - if ($availableSpace < 0) { |
|
| 763 | - foreach ($files as $file) { |
|
| 764 | - if ($availableSpace < 0 && $expiration->isExpired($file['mtime'], true)) { |
|
| 765 | - $tmp = self::delete($file['name'], $user, $file['mtime']); |
|
| 766 | - \OC::$server->getLogger()->info('remove "' . $file['name'] . '" (' . $tmp . 'B) to meet the limit of trash bin size (50% of available quota)', ['app' => 'files_trashbin']); |
|
| 767 | - $availableSpace += $tmp; |
|
| 768 | - $size += $tmp; |
|
| 769 | - } else { |
|
| 770 | - break; |
|
| 771 | - } |
|
| 772 | - } |
|
| 773 | - } |
|
| 774 | - return $size; |
|
| 775 | - } |
|
| 776 | - |
|
| 777 | - /** |
|
| 778 | - * delete files older then max storage time |
|
| 779 | - * |
|
| 780 | - * @param array $files list of files sorted by mtime |
|
| 781 | - * @param string $user |
|
| 782 | - * @return integer[] size of deleted files and number of deleted files |
|
| 783 | - */ |
|
| 784 | - public static function deleteExpiredFiles($files, $user) { |
|
| 785 | - $application = new Application(); |
|
| 786 | - $expiration = $application->getContainer()->query('Expiration'); |
|
| 787 | - $size = 0; |
|
| 788 | - $count = 0; |
|
| 789 | - foreach ($files as $file) { |
|
| 790 | - $timestamp = $file['mtime']; |
|
| 791 | - $filename = $file['name']; |
|
| 792 | - if ($expiration->isExpired($timestamp)) { |
|
| 793 | - try { |
|
| 794 | - $size += self::delete($filename, $user, $timestamp); |
|
| 795 | - $count++; |
|
| 796 | - } catch (\OCP\Files\NotPermittedException $e) { |
|
| 797 | - \OC::$server->getLogger()->logException($e, ['app' => 'files_trashbin', 'level' => \OCP\ILogger::WARN, 'message' => 'Removing "' . $filename . '" from trashbin failed.']); |
|
| 798 | - } |
|
| 799 | - \OC::$server->getLogger()->info( |
|
| 800 | - 'Remove "' . $filename . '" from trashbin because it exceeds max retention obligation term.', |
|
| 801 | - ['app' => 'files_trashbin'] |
|
| 802 | - ); |
|
| 803 | - } else { |
|
| 804 | - break; |
|
| 805 | - } |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - return array($size, $count); |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - /** |
|
| 812 | - * recursive copy to copy a whole directory |
|
| 813 | - * |
|
| 814 | - * @param string $source source path, relative to the users files directory |
|
| 815 | - * @param string $destination destination path relative to the users root directoy |
|
| 816 | - * @param View $view file view for the users root directory |
|
| 817 | - * @return int |
|
| 818 | - * @throws Exceptions\CopyRecursiveException |
|
| 819 | - */ |
|
| 820 | - private static function copy_recursive($source, $destination, View $view) { |
|
| 821 | - $size = 0; |
|
| 822 | - if ($view->is_dir($source)) { |
|
| 823 | - $view->mkdir($destination); |
|
| 824 | - $view->touch($destination, $view->filemtime($source)); |
|
| 825 | - foreach ($view->getDirectoryContent($source) as $i) { |
|
| 826 | - $pathDir = $source . '/' . $i['name']; |
|
| 827 | - if ($view->is_dir($pathDir)) { |
|
| 828 | - $size += self::copy_recursive($pathDir, $destination . '/' . $i['name'], $view); |
|
| 829 | - } else { |
|
| 830 | - $size += $view->filesize($pathDir); |
|
| 831 | - $result = $view->copy($pathDir, $destination . '/' . $i['name']); |
|
| 832 | - if (!$result) { |
|
| 833 | - throw new \OCA\Files_Trashbin\Exceptions\CopyRecursiveException(); |
|
| 834 | - } |
|
| 835 | - $view->touch($destination . '/' . $i['name'], $view->filemtime($pathDir)); |
|
| 836 | - } |
|
| 837 | - } |
|
| 838 | - } else { |
|
| 839 | - $size += $view->filesize($source); |
|
| 840 | - $result = $view->copy($source, $destination); |
|
| 841 | - if (!$result) { |
|
| 842 | - throw new \OCA\Files_Trashbin\Exceptions\CopyRecursiveException(); |
|
| 843 | - } |
|
| 844 | - $view->touch($destination, $view->filemtime($source)); |
|
| 845 | - } |
|
| 846 | - return $size; |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - /** |
|
| 850 | - * find all versions which belong to the file we want to restore |
|
| 851 | - * |
|
| 852 | - * @param string $filename name of the file which should be restored |
|
| 853 | - * @param int $timestamp timestamp when the file was deleted |
|
| 854 | - * @return array |
|
| 855 | - */ |
|
| 856 | - private static function getVersionsFromTrash($filename, $timestamp, $user) { |
|
| 857 | - $view = new View('/' . $user . '/files_trashbin/versions'); |
|
| 858 | - $versions = array(); |
|
| 859 | - |
|
| 860 | - //force rescan of versions, local storage may not have updated the cache |
|
| 861 | - if (!self::$scannedVersions) { |
|
| 862 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 863 | - list($storage,) = $view->resolvePath('/'); |
|
| 864 | - $storage->getScanner()->scan('files_trashbin/versions'); |
|
| 865 | - self::$scannedVersions = true; |
|
| 866 | - } |
|
| 867 | - |
|
| 868 | - if ($timestamp) { |
|
| 869 | - // fetch for old versions |
|
| 870 | - $matches = $view->searchRaw($filename . '.v%.d' . $timestamp); |
|
| 871 | - $offset = -strlen($timestamp) - 2; |
|
| 872 | - } else { |
|
| 873 | - $matches = $view->searchRaw($filename . '.v%'); |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - if (is_array($matches)) { |
|
| 877 | - foreach ($matches as $ma) { |
|
| 878 | - if ($timestamp) { |
|
| 879 | - $parts = explode('.v', substr($ma['path'], 0, $offset)); |
|
| 880 | - $versions[] = end($parts); |
|
| 881 | - } else { |
|
| 882 | - $parts = explode('.v', $ma); |
|
| 883 | - $versions[] = end($parts); |
|
| 884 | - } |
|
| 885 | - } |
|
| 886 | - } |
|
| 887 | - return $versions; |
|
| 888 | - } |
|
| 889 | - |
|
| 890 | - /** |
|
| 891 | - * find unique extension for restored file if a file with the same name already exists |
|
| 892 | - * |
|
| 893 | - * @param string $location where the file should be restored |
|
| 894 | - * @param string $filename name of the file |
|
| 895 | - * @param View $view filesystem view relative to users root directory |
|
| 896 | - * @return string with unique extension |
|
| 897 | - */ |
|
| 898 | - private static function getUniqueFilename($location, $filename, View $view) { |
|
| 899 | - $ext = pathinfo($filename, PATHINFO_EXTENSION); |
|
| 900 | - $name = pathinfo($filename, PATHINFO_FILENAME); |
|
| 901 | - $l = \OC::$server->getL10N('files_trashbin'); |
|
| 902 | - |
|
| 903 | - $location = '/' . trim($location, '/'); |
|
| 904 | - |
|
| 905 | - // if extension is not empty we set a dot in front of it |
|
| 906 | - if ($ext !== '') { |
|
| 907 | - $ext = '.' . $ext; |
|
| 908 | - } |
|
| 909 | - |
|
| 910 | - if ($view->file_exists('files' . $location . '/' . $filename)) { |
|
| 911 | - $i = 2; |
|
| 912 | - $uniqueName = $name . " (" . $l->t("restored") . ")" . $ext; |
|
| 913 | - while ($view->file_exists('files' . $location . '/' . $uniqueName)) { |
|
| 914 | - $uniqueName = $name . " (" . $l->t("restored") . " " . $i . ")" . $ext; |
|
| 915 | - $i++; |
|
| 916 | - } |
|
| 917 | - |
|
| 918 | - return $uniqueName; |
|
| 919 | - } |
|
| 920 | - |
|
| 921 | - return $filename; |
|
| 922 | - } |
|
| 923 | - |
|
| 924 | - /** |
|
| 925 | - * get the size from a given root folder |
|
| 926 | - * |
|
| 927 | - * @param View $view file view on the root folder |
|
| 928 | - * @return integer size of the folder |
|
| 929 | - */ |
|
| 930 | - private static function calculateSize($view) { |
|
| 931 | - $root = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . $view->getAbsolutePath(''); |
|
| 932 | - if (!file_exists($root)) { |
|
| 933 | - return 0; |
|
| 934 | - } |
|
| 935 | - $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($root), \RecursiveIteratorIterator::CHILD_FIRST); |
|
| 936 | - $size = 0; |
|
| 937 | - |
|
| 938 | - /** |
|
| 939 | - * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach |
|
| 940 | - * This bug is fixed in PHP 5.5.9 or before |
|
| 941 | - * See #8376 |
|
| 942 | - */ |
|
| 943 | - $iterator->rewind(); |
|
| 944 | - while ($iterator->valid()) { |
|
| 945 | - $path = $iterator->current(); |
|
| 946 | - $relpath = substr($path, strlen($root) - 1); |
|
| 947 | - if (!$view->is_dir($relpath)) { |
|
| 948 | - $size += $view->filesize($relpath); |
|
| 949 | - } |
|
| 950 | - $iterator->next(); |
|
| 951 | - } |
|
| 952 | - return $size; |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - /** |
|
| 956 | - * get current size of trash bin from a given user |
|
| 957 | - * |
|
| 958 | - * @param string $user user who owns the trash bin |
|
| 959 | - * @return integer trash bin size |
|
| 960 | - */ |
|
| 961 | - private static function getTrashbinSize($user) { |
|
| 962 | - $view = new View('/' . $user); |
|
| 963 | - $fileInfo = $view->getFileInfo('/files_trashbin'); |
|
| 964 | - return isset($fileInfo['size']) ? $fileInfo['size'] : 0; |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - /** |
|
| 968 | - * register hooks |
|
| 969 | - */ |
|
| 970 | - public static function registerHooks() { |
|
| 971 | - // create storage wrapper on setup |
|
| 972 | - \OCP\Util::connectHook('OC_Filesystem', 'preSetup', 'OCA\Files_Trashbin\Storage', 'setupStorage'); |
|
| 973 | - //Listen to delete user signal |
|
| 974 | - \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook'); |
|
| 975 | - //Listen to post write hook |
|
| 976 | - \OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook'); |
|
| 977 | - // pre and post-rename, disable trash logic for the copy+unlink case |
|
| 978 | - \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook'); |
|
| 979 | - } |
|
| 980 | - |
|
| 981 | - /** |
|
| 982 | - * check if trash bin is empty for a given user |
|
| 983 | - * |
|
| 984 | - * @param string $user |
|
| 985 | - * @return bool |
|
| 986 | - */ |
|
| 987 | - public static function isEmpty($user) { |
|
| 988 | - |
|
| 989 | - $view = new View('/' . $user . '/files_trashbin'); |
|
| 990 | - if ($view->is_dir('/files') && $dh = $view->opendir('/files')) { |
|
| 991 | - while ($file = readdir($dh)) { |
|
| 992 | - if (!Filesystem::isIgnoredDir($file)) { |
|
| 993 | - return false; |
|
| 994 | - } |
|
| 995 | - } |
|
| 996 | - } |
|
| 997 | - return true; |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - /** |
|
| 1001 | - * @param $path |
|
| 1002 | - * @return string |
|
| 1003 | - */ |
|
| 1004 | - public static function preview_icon($path) { |
|
| 1005 | - return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_trashbin_preview', array('x' => 32, 'y' => 32, 'file' => $path)); |
|
| 1006 | - } |
|
| 55 | + // unit: percentage; 50% of available disk space/quota |
|
| 56 | + const DEFAULTMAXSIZE = 50; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Whether versions have already be rescanned during this PHP request |
|
| 60 | + * |
|
| 61 | + * @var bool |
|
| 62 | + */ |
|
| 63 | + private static $scannedVersions = false; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Ensure we don't need to scan the file during the move to trash |
|
| 67 | + * by triggering the scan in the pre-hook |
|
| 68 | + * |
|
| 69 | + * @param array $params |
|
| 70 | + */ |
|
| 71 | + public static function ensureFileScannedHook($params) { |
|
| 72 | + try { |
|
| 73 | + self::getUidAndFilename($params['path']); |
|
| 74 | + } catch (NotFoundException $e) { |
|
| 75 | + // nothing to scan for non existing files |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * get the UID of the owner of the file and the path to the file relative to |
|
| 81 | + * owners files folder |
|
| 82 | + * |
|
| 83 | + * @param string $filename |
|
| 84 | + * @return array |
|
| 85 | + * @throws \OC\User\NoUserException |
|
| 86 | + */ |
|
| 87 | + public static function getUidAndFilename($filename) { |
|
| 88 | + $uid = Filesystem::getOwner($filename); |
|
| 89 | + $userManager = \OC::$server->getUserManager(); |
|
| 90 | + // if the user with the UID doesn't exists, e.g. because the UID points |
|
| 91 | + // to a remote user with a federated cloud ID we use the current logged-in |
|
| 92 | + // user. We need a valid local user to move the file to the right trash bin |
|
| 93 | + if (!$userManager->userExists($uid)) { |
|
| 94 | + $uid = User::getUser(); |
|
| 95 | + } |
|
| 96 | + if (!$uid) { |
|
| 97 | + // no owner, usually because of share link from ext storage |
|
| 98 | + return [null, null]; |
|
| 99 | + } |
|
| 100 | + Filesystem::initMountPoints($uid); |
|
| 101 | + if ($uid !== User::getUser()) { |
|
| 102 | + $info = Filesystem::getFileInfo($filename); |
|
| 103 | + $ownerView = new View('/' . $uid . '/files'); |
|
| 104 | + try { |
|
| 105 | + $filename = $ownerView->getPath($info['fileid']); |
|
| 106 | + } catch (NotFoundException $e) { |
|
| 107 | + $filename = null; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + return [$uid, $filename]; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * get original location of files for user |
|
| 115 | + * |
|
| 116 | + * @param string $user |
|
| 117 | + * @return array (filename => array (timestamp => original location)) |
|
| 118 | + */ |
|
| 119 | + public static function getLocations($user) { |
|
| 120 | + $query = \OC_DB::prepare('SELECT `id`, `timestamp`, `location`' |
|
| 121 | + . ' FROM `*PREFIX*files_trash` WHERE `user`=?'); |
|
| 122 | + $result = $query->execute(array($user)); |
|
| 123 | + $array = array(); |
|
| 124 | + while ($row = $result->fetchRow()) { |
|
| 125 | + if (isset($array[$row['id']])) { |
|
| 126 | + $array[$row['id']][$row['timestamp']] = $row['location']; |
|
| 127 | + } else { |
|
| 128 | + $array[$row['id']] = array($row['timestamp'] => $row['location']); |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + return $array; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * get original location of file |
|
| 136 | + * |
|
| 137 | + * @param string $user |
|
| 138 | + * @param string $filename |
|
| 139 | + * @param string $timestamp |
|
| 140 | + * @return string original location |
|
| 141 | + */ |
|
| 142 | + public static function getLocation($user, $filename, $timestamp) { |
|
| 143 | + $query = \OC_DB::prepare('SELECT `location` FROM `*PREFIX*files_trash`' |
|
| 144 | + . ' WHERE `user`=? AND `id`=? AND `timestamp`=?'); |
|
| 145 | + $result = $query->execute(array($user, $filename, $timestamp))->fetchAll(); |
|
| 146 | + if (isset($result[0]['location'])) { |
|
| 147 | + return $result[0]['location']; |
|
| 148 | + } else { |
|
| 149 | + return false; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + private static function setUpTrash($user) { |
|
| 154 | + $view = new View('/' . $user); |
|
| 155 | + if (!$view->is_dir('files_trashbin')) { |
|
| 156 | + $view->mkdir('files_trashbin'); |
|
| 157 | + } |
|
| 158 | + if (!$view->is_dir('files_trashbin/files')) { |
|
| 159 | + $view->mkdir('files_trashbin/files'); |
|
| 160 | + } |
|
| 161 | + if (!$view->is_dir('files_trashbin/versions')) { |
|
| 162 | + $view->mkdir('files_trashbin/versions'); |
|
| 163 | + } |
|
| 164 | + if (!$view->is_dir('files_trashbin/keys')) { |
|
| 165 | + $view->mkdir('files_trashbin/keys'); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * copy file to owners trash |
|
| 172 | + * |
|
| 173 | + * @param string $sourcePath |
|
| 174 | + * @param string $owner |
|
| 175 | + * @param string $targetPath |
|
| 176 | + * @param $user |
|
| 177 | + * @param integer $timestamp |
|
| 178 | + */ |
|
| 179 | + private static function copyFilesToUser($sourcePath, $owner, $targetPath, $user, $timestamp) { |
|
| 180 | + self::setUpTrash($owner); |
|
| 181 | + |
|
| 182 | + $targetFilename = basename($targetPath); |
|
| 183 | + $targetLocation = dirname($targetPath); |
|
| 184 | + |
|
| 185 | + $sourceFilename = basename($sourcePath); |
|
| 186 | + |
|
| 187 | + $view = new View('/'); |
|
| 188 | + |
|
| 189 | + $target = $user . '/files_trashbin/files/' . $targetFilename . '.d' . $timestamp; |
|
| 190 | + $source = $owner . '/files_trashbin/files/' . $sourceFilename . '.d' . $timestamp; |
|
| 191 | + self::copy_recursive($source, $target, $view); |
|
| 192 | + |
|
| 193 | + |
|
| 194 | + if ($view->file_exists($target)) { |
|
| 195 | + $query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)"); |
|
| 196 | + $result = $query->execute(array($targetFilename, $timestamp, $targetLocation, $user)); |
|
| 197 | + if (!$result) { |
|
| 198 | + \OC::$server->getLogger()->error('trash bin database couldn\'t be updated for the files owner', ['app' => 'files_trashbin']); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * move file to the trash bin |
|
| 206 | + * |
|
| 207 | + * @param string $file_path path to the deleted file/directory relative to the files root directory |
|
| 208 | + * @param bool $ownerOnly delete for owner only (if file gets moved out of a shared folder) |
|
| 209 | + * |
|
| 210 | + * @return bool |
|
| 211 | + */ |
|
| 212 | + public static function move2trash($file_path, $ownerOnly = false) { |
|
| 213 | + // get the user for which the filesystem is setup |
|
| 214 | + $root = Filesystem::getRoot(); |
|
| 215 | + list(, $user) = explode('/', $root); |
|
| 216 | + list($owner, $ownerPath) = self::getUidAndFilename($file_path); |
|
| 217 | + |
|
| 218 | + // if no owner found (ex: ext storage + share link), will use the current user's trashbin then |
|
| 219 | + if (is_null($owner)) { |
|
| 220 | + $owner = $user; |
|
| 221 | + $ownerPath = $file_path; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $ownerView = new View('/' . $owner); |
|
| 225 | + // file has been deleted in between |
|
| 226 | + if (is_null($ownerPath) || $ownerPath === '' || !$ownerView->file_exists('/files/' . $ownerPath)) { |
|
| 227 | + return true; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + self::setUpTrash($user); |
|
| 231 | + if ($owner !== $user) { |
|
| 232 | + // also setup for owner |
|
| 233 | + self::setUpTrash($owner); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + $path_parts = pathinfo($ownerPath); |
|
| 237 | + |
|
| 238 | + $filename = $path_parts['basename']; |
|
| 239 | + $location = $path_parts['dirname']; |
|
| 240 | + $timestamp = time(); |
|
| 241 | + |
|
| 242 | + // disable proxy to prevent recursive calls |
|
| 243 | + $trashPath = '/files_trashbin/files/' . $filename . '.d' . $timestamp; |
|
| 244 | + |
|
| 245 | + /** @var \OC\Files\Storage\Storage $trashStorage */ |
|
| 246 | + list($trashStorage, $trashInternalPath) = $ownerView->resolvePath($trashPath); |
|
| 247 | + /** @var \OC\Files\Storage\Storage $sourceStorage */ |
|
| 248 | + list($sourceStorage, $sourceInternalPath) = $ownerView->resolvePath('/files/' . $ownerPath); |
|
| 249 | + try { |
|
| 250 | + $moveSuccessful = true; |
|
| 251 | + if ($trashStorage->file_exists($trashInternalPath)) { |
|
| 252 | + $trashStorage->unlink($trashInternalPath); |
|
| 253 | + } |
|
| 254 | + $trashStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); |
|
| 255 | + } catch (\OCA\Files_Trashbin\Exceptions\CopyRecursiveException $e) { |
|
| 256 | + $moveSuccessful = false; |
|
| 257 | + if ($trashStorage->file_exists($trashInternalPath)) { |
|
| 258 | + $trashStorage->unlink($trashInternalPath); |
|
| 259 | + } |
|
| 260 | + \OC::$server->getLogger()->error('Couldn\'t move ' . $file_path . ' to the trash bin', ['app' => 'files_trashbin']); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + if ($sourceStorage->file_exists($sourceInternalPath)) { // failed to delete the original file, abort |
|
| 264 | + if ($sourceStorage->is_dir($sourceInternalPath)) { |
|
| 265 | + $sourceStorage->rmdir($sourceInternalPath); |
|
| 266 | + } else { |
|
| 267 | + $sourceStorage->unlink($sourceInternalPath); |
|
| 268 | + } |
|
| 269 | + return false; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + $trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); |
|
| 273 | + |
|
| 274 | + if ($moveSuccessful) { |
|
| 275 | + $query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)"); |
|
| 276 | + $result = $query->execute(array($filename, $timestamp, $location, $owner)); |
|
| 277 | + if (!$result) { |
|
| 278 | + \OC::$server->getLogger()->error('trash bin database couldn\'t be updated', ['app' => 'files_trashbin']); |
|
| 279 | + } |
|
| 280 | + \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_moveToTrash', array('filePath' => Filesystem::normalizePath($file_path), |
|
| 281 | + 'trashPath' => Filesystem::normalizePath($filename . '.d' . $timestamp))); |
|
| 282 | + |
|
| 283 | + self::retainVersions($filename, $owner, $ownerPath, $timestamp); |
|
| 284 | + |
|
| 285 | + // if owner !== user we need to also add a copy to the users trash |
|
| 286 | + if ($user !== $owner && $ownerOnly === false) { |
|
| 287 | + self::copyFilesToUser($ownerPath, $owner, $file_path, $user, $timestamp); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + self::scheduleExpire($user); |
|
| 292 | + |
|
| 293 | + // if owner !== user we also need to update the owners trash size |
|
| 294 | + if ($owner !== $user) { |
|
| 295 | + self::scheduleExpire($owner); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + return $moveSuccessful; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Move file versions to trash so that they can be restored later |
|
| 303 | + * |
|
| 304 | + * @param string $filename of deleted file |
|
| 305 | + * @param string $owner owner user id |
|
| 306 | + * @param string $ownerPath path relative to the owner's home storage |
|
| 307 | + * @param integer $timestamp when the file was deleted |
|
| 308 | + */ |
|
| 309 | + private static function retainVersions($filename, $owner, $ownerPath, $timestamp) { |
|
| 310 | + if (\OCP\App::isEnabled('files_versions') && !empty($ownerPath)) { |
|
| 311 | + |
|
| 312 | + $user = User::getUser(); |
|
| 313 | + $rootView = new View('/'); |
|
| 314 | + |
|
| 315 | + if ($rootView->is_dir($owner . '/files_versions/' . $ownerPath)) { |
|
| 316 | + if ($owner !== $user) { |
|
| 317 | + self::copy_recursive($owner . '/files_versions/' . $ownerPath, $owner . '/files_trashbin/versions/' . basename($ownerPath) . '.d' . $timestamp, $rootView); |
|
| 318 | + } |
|
| 319 | + self::move($rootView, $owner . '/files_versions/' . $ownerPath, $user . '/files_trashbin/versions/' . $filename . '.d' . $timestamp); |
|
| 320 | + } else if ($versions = \OCA\Files_Versions\Storage::getVersions($owner, $ownerPath)) { |
|
| 321 | + |
|
| 322 | + foreach ($versions as $v) { |
|
| 323 | + if ($owner !== $user) { |
|
| 324 | + self::copy($rootView, $owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $owner . '/files_trashbin/versions/' . $v['name'] . '.v' . $v['version'] . '.d' . $timestamp); |
|
| 325 | + } |
|
| 326 | + self::move($rootView, $owner . '/files_versions' . $v['path'] . '.v' . $v['version'], $user . '/files_trashbin/versions/' . $filename . '.v' . $v['version'] . '.d' . $timestamp); |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Move a file or folder on storage level |
|
| 334 | + * |
|
| 335 | + * @param View $view |
|
| 336 | + * @param string $source |
|
| 337 | + * @param string $target |
|
| 338 | + * @return bool |
|
| 339 | + */ |
|
| 340 | + private static function move(View $view, $source, $target) { |
|
| 341 | + /** @var \OC\Files\Storage\Storage $sourceStorage */ |
|
| 342 | + list($sourceStorage, $sourceInternalPath) = $view->resolvePath($source); |
|
| 343 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 344 | + list($targetStorage, $targetInternalPath) = $view->resolvePath($target); |
|
| 345 | + /** @var \OC\Files\Storage\Storage $ownerTrashStorage */ |
|
| 346 | + |
|
| 347 | + $result = $targetStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 348 | + if ($result) { |
|
| 349 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 350 | + } |
|
| 351 | + return $result; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Copy a file or folder on storage level |
|
| 356 | + * |
|
| 357 | + * @param View $view |
|
| 358 | + * @param string $source |
|
| 359 | + * @param string $target |
|
| 360 | + * @return bool |
|
| 361 | + */ |
|
| 362 | + private static function copy(View $view, $source, $target) { |
|
| 363 | + /** @var \OC\Files\Storage\Storage $sourceStorage */ |
|
| 364 | + list($sourceStorage, $sourceInternalPath) = $view->resolvePath($source); |
|
| 365 | + /** @var \OC\Files\Storage\Storage $targetStorage */ |
|
| 366 | + list($targetStorage, $targetInternalPath) = $view->resolvePath($target); |
|
| 367 | + /** @var \OC\Files\Storage\Storage $ownerTrashStorage */ |
|
| 368 | + |
|
| 369 | + $result = $targetStorage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 370 | + if ($result) { |
|
| 371 | + $targetStorage->getUpdater()->update($targetInternalPath); |
|
| 372 | + } |
|
| 373 | + return $result; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Restore a file or folder from trash bin |
|
| 378 | + * |
|
| 379 | + * @param string $file path to the deleted file/folder relative to "files_trashbin/files/", |
|
| 380 | + * including the timestamp suffix ".d12345678" |
|
| 381 | + * @param string $filename name of the file/folder |
|
| 382 | + * @param int $timestamp time when the file/folder was deleted |
|
| 383 | + * |
|
| 384 | + * @return bool true on success, false otherwise |
|
| 385 | + */ |
|
| 386 | + public static function restore($file, $filename, $timestamp) { |
|
| 387 | + $user = User::getUser(); |
|
| 388 | + $view = new View('/' . $user); |
|
| 389 | + |
|
| 390 | + $location = ''; |
|
| 391 | + if ($timestamp) { |
|
| 392 | + $location = self::getLocation($user, $filename, $timestamp); |
|
| 393 | + if ($location === false) { |
|
| 394 | + \OC::$server->getLogger()->error('trash bin database inconsistent! ($user: ' . $user . ' $filename: ' . $filename . ', $timestamp: ' . $timestamp . ')', ['app' => 'files_trashbin']); |
|
| 395 | + } else { |
|
| 396 | + // if location no longer exists, restore file in the root directory |
|
| 397 | + if ($location !== '/' && |
|
| 398 | + (!$view->is_dir('files/' . $location) || |
|
| 399 | + !$view->isCreatable('files/' . $location)) |
|
| 400 | + ) { |
|
| 401 | + $location = ''; |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + // we need a extension in case a file/dir with the same name already exists |
|
| 407 | + $uniqueFilename = self::getUniqueFilename($location, $filename, $view); |
|
| 408 | + |
|
| 409 | + $source = Filesystem::normalizePath('files_trashbin/files/' . $file); |
|
| 410 | + $target = Filesystem::normalizePath('files/' . $location . '/' . $uniqueFilename); |
|
| 411 | + if (!$view->file_exists($source)) { |
|
| 412 | + return false; |
|
| 413 | + } |
|
| 414 | + $mtime = $view->filemtime($source); |
|
| 415 | + |
|
| 416 | + // restore file |
|
| 417 | + $restoreResult = $view->rename($source, $target); |
|
| 418 | + |
|
| 419 | + // handle the restore result |
|
| 420 | + if ($restoreResult) { |
|
| 421 | + $fakeRoot = $view->getRoot(); |
|
| 422 | + $view->chroot('/' . $user . '/files'); |
|
| 423 | + $view->touch('/' . $location . '/' . $uniqueFilename, $mtime); |
|
| 424 | + $view->chroot($fakeRoot); |
|
| 425 | + \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', array('filePath' => Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename), |
|
| 426 | + 'trashPath' => Filesystem::normalizePath($file))); |
|
| 427 | + |
|
| 428 | + self::restoreVersions($view, $file, $filename, $uniqueFilename, $location, $timestamp); |
|
| 429 | + |
|
| 430 | + if ($timestamp) { |
|
| 431 | + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=? AND `id`=? AND `timestamp`=?'); |
|
| 432 | + $query->execute(array($user, $filename, $timestamp)); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + return true; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + return false; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * restore versions from trash bin |
|
| 443 | + * |
|
| 444 | + * @param View $view file view |
|
| 445 | + * @param string $file complete path to file |
|
| 446 | + * @param string $filename name of file once it was deleted |
|
| 447 | + * @param string $uniqueFilename new file name to restore the file without overwriting existing files |
|
| 448 | + * @param string $location location if file |
|
| 449 | + * @param int $timestamp deletion time |
|
| 450 | + * @return false|null |
|
| 451 | + */ |
|
| 452 | + private static function restoreVersions(View $view, $file, $filename, $uniqueFilename, $location, $timestamp) { |
|
| 453 | + |
|
| 454 | + if (\OCP\App::isEnabled('files_versions')) { |
|
| 455 | + |
|
| 456 | + $user = User::getUser(); |
|
| 457 | + $rootView = new View('/'); |
|
| 458 | + |
|
| 459 | + $target = Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename); |
|
| 460 | + |
|
| 461 | + list($owner, $ownerPath) = self::getUidAndFilename($target); |
|
| 462 | + |
|
| 463 | + // file has been deleted in between |
|
| 464 | + if (empty($ownerPath)) { |
|
| 465 | + return false; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + if ($timestamp) { |
|
| 469 | + $versionedFile = $filename; |
|
| 470 | + } else { |
|
| 471 | + $versionedFile = $file; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + if ($view->is_dir('/files_trashbin/versions/' . $file)) { |
|
| 475 | + $rootView->rename(Filesystem::normalizePath($user . '/files_trashbin/versions/' . $file), Filesystem::normalizePath($owner . '/files_versions/' . $ownerPath)); |
|
| 476 | + } else if ($versions = self::getVersionsFromTrash($versionedFile, $timestamp, $user)) { |
|
| 477 | + foreach ($versions as $v) { |
|
| 478 | + if ($timestamp) { |
|
| 479 | + $rootView->rename($user . '/files_trashbin/versions/' . $versionedFile . '.v' . $v . '.d' . $timestamp, $owner . '/files_versions/' . $ownerPath . '.v' . $v); |
|
| 480 | + } else { |
|
| 481 | + $rootView->rename($user . '/files_trashbin/versions/' . $versionedFile . '.v' . $v, $owner . '/files_versions/' . $ownerPath . '.v' . $v); |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + } |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * delete all files from the trash |
|
| 490 | + */ |
|
| 491 | + public static function deleteAll() { |
|
| 492 | + $user = User::getUser(); |
|
| 493 | + $userRoot = \OC::$server->getUserFolder($user)->getParent(); |
|
| 494 | + $view = new View('/' . $user); |
|
| 495 | + $fileInfos = $view->getDirectoryContent('files_trashbin/files'); |
|
| 496 | + |
|
| 497 | + try { |
|
| 498 | + $trash = $userRoot->get('files_trashbin'); |
|
| 499 | + } catch (NotFoundException $e) { |
|
| 500 | + return false; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + // Array to store the relative path in (after the file is deleted, the view won't be able to relativise the path anymore) |
|
| 504 | + $filePaths = array(); |
|
| 505 | + foreach($fileInfos as $fileInfo){ |
|
| 506 | + $filePaths[] = $view->getRelativePath($fileInfo->getPath()); |
|
| 507 | + } |
|
| 508 | + unset($fileInfos); // save memory |
|
| 509 | + |
|
| 510 | + // Bulk PreDelete-Hook |
|
| 511 | + \OC_Hook::emit('\OCP\Trashbin', 'preDeleteAll', array('paths' => $filePaths)); |
|
| 512 | + |
|
| 513 | + // Single-File Hooks |
|
| 514 | + foreach($filePaths as $path){ |
|
| 515 | + self::emitTrashbinPreDelete($path); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + // actual file deletion |
|
| 519 | + $trash->delete(); |
|
| 520 | + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=?'); |
|
| 521 | + $query->execute(array($user)); |
|
| 522 | + |
|
| 523 | + // Bulk PostDelete-Hook |
|
| 524 | + \OC_Hook::emit('\OCP\Trashbin', 'deleteAll', array('paths' => $filePaths)); |
|
| 525 | + |
|
| 526 | + // Single-File Hooks |
|
| 527 | + foreach($filePaths as $path){ |
|
| 528 | + self::emitTrashbinPostDelete($path); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + $trash = $userRoot->newFolder('files_trashbin'); |
|
| 532 | + $trash->newFolder('files'); |
|
| 533 | + |
|
| 534 | + return true; |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * wrapper function to emit the 'preDelete' hook of \OCP\Trashbin before a file is deleted |
|
| 539 | + * @param string $path |
|
| 540 | + */ |
|
| 541 | + protected static function emitTrashbinPreDelete($path){ |
|
| 542 | + \OC_Hook::emit('\OCP\Trashbin', 'preDelete', array('path' => $path)); |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * wrapper function to emit the 'delete' hook of \OCP\Trashbin after a file has been deleted |
|
| 547 | + * @param string $path |
|
| 548 | + */ |
|
| 549 | + protected static function emitTrashbinPostDelete($path){ |
|
| 550 | + \OC_Hook::emit('\OCP\Trashbin', 'delete', array('path' => $path)); |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + /** |
|
| 554 | + * delete file from trash bin permanently |
|
| 555 | + * |
|
| 556 | + * @param string $filename path to the file |
|
| 557 | + * @param string $user |
|
| 558 | + * @param int $timestamp of deletion time |
|
| 559 | + * |
|
| 560 | + * @return int size of deleted files |
|
| 561 | + */ |
|
| 562 | + public static function delete($filename, $user, $timestamp = null) { |
|
| 563 | + $userRoot = \OC::$server->getUserFolder($user)->getParent(); |
|
| 564 | + $view = new View('/' . $user); |
|
| 565 | + $size = 0; |
|
| 566 | + |
|
| 567 | + if ($timestamp) { |
|
| 568 | + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=? AND `id`=? AND `timestamp`=?'); |
|
| 569 | + $query->execute(array($user, $filename, $timestamp)); |
|
| 570 | + $file = $filename . '.d' . $timestamp; |
|
| 571 | + } else { |
|
| 572 | + $file = $filename; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + $size += self::deleteVersions($view, $file, $filename, $timestamp, $user); |
|
| 576 | + |
|
| 577 | + try { |
|
| 578 | + $node = $userRoot->get('/files_trashbin/files/' . $file); |
|
| 579 | + } catch (NotFoundException $e) { |
|
| 580 | + return $size; |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + if ($node instanceof Folder) { |
|
| 584 | + $size += self::calculateSize(new View('/' . $user . '/files_trashbin/files/' . $file)); |
|
| 585 | + } else if ($node instanceof File) { |
|
| 586 | + $size += $view->filesize('/files_trashbin/files/' . $file); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + self::emitTrashbinPreDelete('/files_trashbin/files/' . $file); |
|
| 590 | + $node->delete(); |
|
| 591 | + self::emitTrashbinPostDelete('/files_trashbin/files/' . $file); |
|
| 592 | + |
|
| 593 | + return $size; |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + /** |
|
| 597 | + * @param View $view |
|
| 598 | + * @param string $file |
|
| 599 | + * @param string $filename |
|
| 600 | + * @param integer|null $timestamp |
|
| 601 | + * @param string $user |
|
| 602 | + * @return int |
|
| 603 | + */ |
|
| 604 | + private static function deleteVersions(View $view, $file, $filename, $timestamp, $user) { |
|
| 605 | + $size = 0; |
|
| 606 | + if (\OCP\App::isEnabled('files_versions')) { |
|
| 607 | + if ($view->is_dir('files_trashbin/versions/' . $file)) { |
|
| 608 | + $size += self::calculateSize(new View('/' . $user . '/files_trashbin/versions/' . $file)); |
|
| 609 | + $view->unlink('files_trashbin/versions/' . $file); |
|
| 610 | + } else if ($versions = self::getVersionsFromTrash($filename, $timestamp, $user)) { |
|
| 611 | + foreach ($versions as $v) { |
|
| 612 | + if ($timestamp) { |
|
| 613 | + $size += $view->filesize('/files_trashbin/versions/' . $filename . '.v' . $v . '.d' . $timestamp); |
|
| 614 | + $view->unlink('/files_trashbin/versions/' . $filename . '.v' . $v . '.d' . $timestamp); |
|
| 615 | + } else { |
|
| 616 | + $size += $view->filesize('/files_trashbin/versions/' . $filename . '.v' . $v); |
|
| 617 | + $view->unlink('/files_trashbin/versions/' . $filename . '.v' . $v); |
|
| 618 | + } |
|
| 619 | + } |
|
| 620 | + } |
|
| 621 | + } |
|
| 622 | + return $size; |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * check to see whether a file exists in trashbin |
|
| 627 | + * |
|
| 628 | + * @param string $filename path to the file |
|
| 629 | + * @param int $timestamp of deletion time |
|
| 630 | + * @return bool true if file exists, otherwise false |
|
| 631 | + */ |
|
| 632 | + public static function file_exists($filename, $timestamp = null) { |
|
| 633 | + $user = User::getUser(); |
|
| 634 | + $view = new View('/' . $user); |
|
| 635 | + |
|
| 636 | + if ($timestamp) { |
|
| 637 | + $filename = $filename . '.d' . $timestamp; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + $target = Filesystem::normalizePath('files_trashbin/files/' . $filename); |
|
| 641 | + return $view->file_exists($target); |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * deletes used space for trash bin in db if user was deleted |
|
| 646 | + * |
|
| 647 | + * @param string $uid id of deleted user |
|
| 648 | + * @return bool result of db delete operation |
|
| 649 | + */ |
|
| 650 | + public static function deleteUser($uid) { |
|
| 651 | + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=?'); |
|
| 652 | + return $query->execute(array($uid)); |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + /** |
|
| 656 | + * calculate remaining free space for trash bin |
|
| 657 | + * |
|
| 658 | + * @param integer $trashbinSize current size of the trash bin |
|
| 659 | + * @param string $user |
|
| 660 | + * @return int available free space for trash bin |
|
| 661 | + */ |
|
| 662 | + private static function calculateFreeSpace($trashbinSize, $user) { |
|
| 663 | + $softQuota = true; |
|
| 664 | + $userObject = \OC::$server->getUserManager()->get($user); |
|
| 665 | + if(is_null($userObject)) { |
|
| 666 | + return 0; |
|
| 667 | + } |
|
| 668 | + $quota = $userObject->getQuota(); |
|
| 669 | + if ($quota === null || $quota === 'none') { |
|
| 670 | + $quota = Filesystem::free_space('/'); |
|
| 671 | + $softQuota = false; |
|
| 672 | + // inf or unknown free space |
|
| 673 | + if ($quota < 0) { |
|
| 674 | + $quota = PHP_INT_MAX; |
|
| 675 | + } |
|
| 676 | + } else { |
|
| 677 | + $quota = \OCP\Util::computerFileSize($quota); |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + // calculate available space for trash bin |
|
| 681 | + // subtract size of files and current trash bin size from quota |
|
| 682 | + if ($softQuota) { |
|
| 683 | + $userFolder = \OC::$server->getUserFolder($user); |
|
| 684 | + if(is_null($userFolder)) { |
|
| 685 | + return 0; |
|
| 686 | + } |
|
| 687 | + $free = $quota - $userFolder->getSize(); // remaining free space for user |
|
| 688 | + if ($free > 0) { |
|
| 689 | + $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $trashbinSize; // how much space can be used for versions |
|
| 690 | + } else { |
|
| 691 | + $availableSpace = $free - $trashbinSize; |
|
| 692 | + } |
|
| 693 | + } else { |
|
| 694 | + $availableSpace = $quota; |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + return $availableSpace; |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * resize trash bin if necessary after a new file was added to Nextcloud |
|
| 702 | + * |
|
| 703 | + * @param string $user user id |
|
| 704 | + */ |
|
| 705 | + public static function resizeTrash($user) { |
|
| 706 | + |
|
| 707 | + $size = self::getTrashbinSize($user); |
|
| 708 | + |
|
| 709 | + $freeSpace = self::calculateFreeSpace($size, $user); |
|
| 710 | + |
|
| 711 | + if ($freeSpace < 0) { |
|
| 712 | + self::scheduleExpire($user); |
|
| 713 | + } |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + /** |
|
| 717 | + * clean up the trash bin |
|
| 718 | + * |
|
| 719 | + * @param string $user |
|
| 720 | + */ |
|
| 721 | + public static function expire($user) { |
|
| 722 | + $trashBinSize = self::getTrashbinSize($user); |
|
| 723 | + $availableSpace = self::calculateFreeSpace($trashBinSize, $user); |
|
| 724 | + |
|
| 725 | + $dirContent = Helper::getTrashFiles('/', $user, 'mtime'); |
|
| 726 | + |
|
| 727 | + // delete all files older then $retention_obligation |
|
| 728 | + list($delSize, $count) = self::deleteExpiredFiles($dirContent, $user); |
|
| 729 | + |
|
| 730 | + $availableSpace += $delSize; |
|
| 731 | + |
|
| 732 | + // delete files from trash until we meet the trash bin size limit again |
|
| 733 | + self::deleteFiles(array_slice($dirContent, $count), $user, $availableSpace); |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * @param string $user |
|
| 738 | + */ |
|
| 739 | + private static function scheduleExpire($user) { |
|
| 740 | + // let the admin disable auto expire |
|
| 741 | + $application = new Application(); |
|
| 742 | + $expiration = $application->getContainer()->query('Expiration'); |
|
| 743 | + if ($expiration->isEnabled()) { |
|
| 744 | + \OC::$server->getCommandBus()->push(new Expire($user)); |
|
| 745 | + } |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + /** |
|
| 749 | + * if the size limit for the trash bin is reached, we delete the oldest |
|
| 750 | + * files in the trash bin until we meet the limit again |
|
| 751 | + * |
|
| 752 | + * @param array $files |
|
| 753 | + * @param string $user |
|
| 754 | + * @param int $availableSpace available disc space |
|
| 755 | + * @return int size of deleted files |
|
| 756 | + */ |
|
| 757 | + protected static function deleteFiles($files, $user, $availableSpace) { |
|
| 758 | + $application = new Application(); |
|
| 759 | + $expiration = $application->getContainer()->query('Expiration'); |
|
| 760 | + $size = 0; |
|
| 761 | + |
|
| 762 | + if ($availableSpace < 0) { |
|
| 763 | + foreach ($files as $file) { |
|
| 764 | + if ($availableSpace < 0 && $expiration->isExpired($file['mtime'], true)) { |
|
| 765 | + $tmp = self::delete($file['name'], $user, $file['mtime']); |
|
| 766 | + \OC::$server->getLogger()->info('remove "' . $file['name'] . '" (' . $tmp . 'B) to meet the limit of trash bin size (50% of available quota)', ['app' => 'files_trashbin']); |
|
| 767 | + $availableSpace += $tmp; |
|
| 768 | + $size += $tmp; |
|
| 769 | + } else { |
|
| 770 | + break; |
|
| 771 | + } |
|
| 772 | + } |
|
| 773 | + } |
|
| 774 | + return $size; |
|
| 775 | + } |
|
| 776 | + |
|
| 777 | + /** |
|
| 778 | + * delete files older then max storage time |
|
| 779 | + * |
|
| 780 | + * @param array $files list of files sorted by mtime |
|
| 781 | + * @param string $user |
|
| 782 | + * @return integer[] size of deleted files and number of deleted files |
|
| 783 | + */ |
|
| 784 | + public static function deleteExpiredFiles($files, $user) { |
|
| 785 | + $application = new Application(); |
|
| 786 | + $expiration = $application->getContainer()->query('Expiration'); |
|
| 787 | + $size = 0; |
|
| 788 | + $count = 0; |
|
| 789 | + foreach ($files as $file) { |
|
| 790 | + $timestamp = $file['mtime']; |
|
| 791 | + $filename = $file['name']; |
|
| 792 | + if ($expiration->isExpired($timestamp)) { |
|
| 793 | + try { |
|
| 794 | + $size += self::delete($filename, $user, $timestamp); |
|
| 795 | + $count++; |
|
| 796 | + } catch (\OCP\Files\NotPermittedException $e) { |
|
| 797 | + \OC::$server->getLogger()->logException($e, ['app' => 'files_trashbin', 'level' => \OCP\ILogger::WARN, 'message' => 'Removing "' . $filename . '" from trashbin failed.']); |
|
| 798 | + } |
|
| 799 | + \OC::$server->getLogger()->info( |
|
| 800 | + 'Remove "' . $filename . '" from trashbin because it exceeds max retention obligation term.', |
|
| 801 | + ['app' => 'files_trashbin'] |
|
| 802 | + ); |
|
| 803 | + } else { |
|
| 804 | + break; |
|
| 805 | + } |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + return array($size, $count); |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + /** |
|
| 812 | + * recursive copy to copy a whole directory |
|
| 813 | + * |
|
| 814 | + * @param string $source source path, relative to the users files directory |
|
| 815 | + * @param string $destination destination path relative to the users root directoy |
|
| 816 | + * @param View $view file view for the users root directory |
|
| 817 | + * @return int |
|
| 818 | + * @throws Exceptions\CopyRecursiveException |
|
| 819 | + */ |
|
| 820 | + private static function copy_recursive($source, $destination, View $view) { |
|
| 821 | + $size = 0; |
|
| 822 | + if ($view->is_dir($source)) { |
|
| 823 | + $view->mkdir($destination); |
|
| 824 | + $view->touch($destination, $view->filemtime($source)); |
|
| 825 | + foreach ($view->getDirectoryContent($source) as $i) { |
|
| 826 | + $pathDir = $source . '/' . $i['name']; |
|
| 827 | + if ($view->is_dir($pathDir)) { |
|
| 828 | + $size += self::copy_recursive($pathDir, $destination . '/' . $i['name'], $view); |
|
| 829 | + } else { |
|
| 830 | + $size += $view->filesize($pathDir); |
|
| 831 | + $result = $view->copy($pathDir, $destination . '/' . $i['name']); |
|
| 832 | + if (!$result) { |
|
| 833 | + throw new \OCA\Files_Trashbin\Exceptions\CopyRecursiveException(); |
|
| 834 | + } |
|
| 835 | + $view->touch($destination . '/' . $i['name'], $view->filemtime($pathDir)); |
|
| 836 | + } |
|
| 837 | + } |
|
| 838 | + } else { |
|
| 839 | + $size += $view->filesize($source); |
|
| 840 | + $result = $view->copy($source, $destination); |
|
| 841 | + if (!$result) { |
|
| 842 | + throw new \OCA\Files_Trashbin\Exceptions\CopyRecursiveException(); |
|
| 843 | + } |
|
| 844 | + $view->touch($destination, $view->filemtime($source)); |
|
| 845 | + } |
|
| 846 | + return $size; |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + /** |
|
| 850 | + * find all versions which belong to the file we want to restore |
|
| 851 | + * |
|
| 852 | + * @param string $filename name of the file which should be restored |
|
| 853 | + * @param int $timestamp timestamp when the file was deleted |
|
| 854 | + * @return array |
|
| 855 | + */ |
|
| 856 | + private static function getVersionsFromTrash($filename, $timestamp, $user) { |
|
| 857 | + $view = new View('/' . $user . '/files_trashbin/versions'); |
|
| 858 | + $versions = array(); |
|
| 859 | + |
|
| 860 | + //force rescan of versions, local storage may not have updated the cache |
|
| 861 | + if (!self::$scannedVersions) { |
|
| 862 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 863 | + list($storage,) = $view->resolvePath('/'); |
|
| 864 | + $storage->getScanner()->scan('files_trashbin/versions'); |
|
| 865 | + self::$scannedVersions = true; |
|
| 866 | + } |
|
| 867 | + |
|
| 868 | + if ($timestamp) { |
|
| 869 | + // fetch for old versions |
|
| 870 | + $matches = $view->searchRaw($filename . '.v%.d' . $timestamp); |
|
| 871 | + $offset = -strlen($timestamp) - 2; |
|
| 872 | + } else { |
|
| 873 | + $matches = $view->searchRaw($filename . '.v%'); |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + if (is_array($matches)) { |
|
| 877 | + foreach ($matches as $ma) { |
|
| 878 | + if ($timestamp) { |
|
| 879 | + $parts = explode('.v', substr($ma['path'], 0, $offset)); |
|
| 880 | + $versions[] = end($parts); |
|
| 881 | + } else { |
|
| 882 | + $parts = explode('.v', $ma); |
|
| 883 | + $versions[] = end($parts); |
|
| 884 | + } |
|
| 885 | + } |
|
| 886 | + } |
|
| 887 | + return $versions; |
|
| 888 | + } |
|
| 889 | + |
|
| 890 | + /** |
|
| 891 | + * find unique extension for restored file if a file with the same name already exists |
|
| 892 | + * |
|
| 893 | + * @param string $location where the file should be restored |
|
| 894 | + * @param string $filename name of the file |
|
| 895 | + * @param View $view filesystem view relative to users root directory |
|
| 896 | + * @return string with unique extension |
|
| 897 | + */ |
|
| 898 | + private static function getUniqueFilename($location, $filename, View $view) { |
|
| 899 | + $ext = pathinfo($filename, PATHINFO_EXTENSION); |
|
| 900 | + $name = pathinfo($filename, PATHINFO_FILENAME); |
|
| 901 | + $l = \OC::$server->getL10N('files_trashbin'); |
|
| 902 | + |
|
| 903 | + $location = '/' . trim($location, '/'); |
|
| 904 | + |
|
| 905 | + // if extension is not empty we set a dot in front of it |
|
| 906 | + if ($ext !== '') { |
|
| 907 | + $ext = '.' . $ext; |
|
| 908 | + } |
|
| 909 | + |
|
| 910 | + if ($view->file_exists('files' . $location . '/' . $filename)) { |
|
| 911 | + $i = 2; |
|
| 912 | + $uniqueName = $name . " (" . $l->t("restored") . ")" . $ext; |
|
| 913 | + while ($view->file_exists('files' . $location . '/' . $uniqueName)) { |
|
| 914 | + $uniqueName = $name . " (" . $l->t("restored") . " " . $i . ")" . $ext; |
|
| 915 | + $i++; |
|
| 916 | + } |
|
| 917 | + |
|
| 918 | + return $uniqueName; |
|
| 919 | + } |
|
| 920 | + |
|
| 921 | + return $filename; |
|
| 922 | + } |
|
| 923 | + |
|
| 924 | + /** |
|
| 925 | + * get the size from a given root folder |
|
| 926 | + * |
|
| 927 | + * @param View $view file view on the root folder |
|
| 928 | + * @return integer size of the folder |
|
| 929 | + */ |
|
| 930 | + private static function calculateSize($view) { |
|
| 931 | + $root = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . $view->getAbsolutePath(''); |
|
| 932 | + if (!file_exists($root)) { |
|
| 933 | + return 0; |
|
| 934 | + } |
|
| 935 | + $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($root), \RecursiveIteratorIterator::CHILD_FIRST); |
|
| 936 | + $size = 0; |
|
| 937 | + |
|
| 938 | + /** |
|
| 939 | + * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach |
|
| 940 | + * This bug is fixed in PHP 5.5.9 or before |
|
| 941 | + * See #8376 |
|
| 942 | + */ |
|
| 943 | + $iterator->rewind(); |
|
| 944 | + while ($iterator->valid()) { |
|
| 945 | + $path = $iterator->current(); |
|
| 946 | + $relpath = substr($path, strlen($root) - 1); |
|
| 947 | + if (!$view->is_dir($relpath)) { |
|
| 948 | + $size += $view->filesize($relpath); |
|
| 949 | + } |
|
| 950 | + $iterator->next(); |
|
| 951 | + } |
|
| 952 | + return $size; |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + /** |
|
| 956 | + * get current size of trash bin from a given user |
|
| 957 | + * |
|
| 958 | + * @param string $user user who owns the trash bin |
|
| 959 | + * @return integer trash bin size |
|
| 960 | + */ |
|
| 961 | + private static function getTrashbinSize($user) { |
|
| 962 | + $view = new View('/' . $user); |
|
| 963 | + $fileInfo = $view->getFileInfo('/files_trashbin'); |
|
| 964 | + return isset($fileInfo['size']) ? $fileInfo['size'] : 0; |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + /** |
|
| 968 | + * register hooks |
|
| 969 | + */ |
|
| 970 | + public static function registerHooks() { |
|
| 971 | + // create storage wrapper on setup |
|
| 972 | + \OCP\Util::connectHook('OC_Filesystem', 'preSetup', 'OCA\Files_Trashbin\Storage', 'setupStorage'); |
|
| 973 | + //Listen to delete user signal |
|
| 974 | + \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook'); |
|
| 975 | + //Listen to post write hook |
|
| 976 | + \OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook'); |
|
| 977 | + // pre and post-rename, disable trash logic for the copy+unlink case |
|
| 978 | + \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook'); |
|
| 979 | + } |
|
| 980 | + |
|
| 981 | + /** |
|
| 982 | + * check if trash bin is empty for a given user |
|
| 983 | + * |
|
| 984 | + * @param string $user |
|
| 985 | + * @return bool |
|
| 986 | + */ |
|
| 987 | + public static function isEmpty($user) { |
|
| 988 | + |
|
| 989 | + $view = new View('/' . $user . '/files_trashbin'); |
|
| 990 | + if ($view->is_dir('/files') && $dh = $view->opendir('/files')) { |
|
| 991 | + while ($file = readdir($dh)) { |
|
| 992 | + if (!Filesystem::isIgnoredDir($file)) { |
|
| 993 | + return false; |
|
| 994 | + } |
|
| 995 | + } |
|
| 996 | + } |
|
| 997 | + return true; |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + /** |
|
| 1001 | + * @param $path |
|
| 1002 | + * @return string |
|
| 1003 | + */ |
|
| 1004 | + public static function preview_icon($path) { |
|
| 1005 | + return \OC::$server->getURLGenerator()->linkToRoute('core_ajax_trashbin_preview', array('x' => 32, 'y' => 32, 'file' => $path)); |
|
| 1006 | + } |
|
| 1007 | 1007 | } |
@@ -33,67 +33,67 @@ |
||
| 33 | 33 | use OCA\Files_Trashbin\Capabilities; |
| 34 | 34 | |
| 35 | 35 | class Application extends App { |
| 36 | - public function __construct (array $urlParams = []) { |
|
| 37 | - parent::__construct('files_trashbin', $urlParams); |
|
| 36 | + public function __construct (array $urlParams = []) { |
|
| 37 | + parent::__construct('files_trashbin', $urlParams); |
|
| 38 | 38 | |
| 39 | - $container = $this->getContainer(); |
|
| 40 | - /* |
|
| 39 | + $container = $this->getContainer(); |
|
| 40 | + /* |
|
| 41 | 41 | * Register capabilities |
| 42 | 42 | */ |
| 43 | - $container->registerCapability(Capabilities::class); |
|
| 43 | + $container->registerCapability(Capabilities::class); |
|
| 44 | 44 | |
| 45 | - /* |
|
| 45 | + /* |
|
| 46 | 46 | * Register expiration |
| 47 | 47 | */ |
| 48 | - $container->registerService('Expiration', function($c) { |
|
| 49 | - return new Expiration( |
|
| 50 | - $c->query('ServerContainer')->getConfig(), |
|
| 51 | - $c->query(ITimeFactory::class) |
|
| 52 | - ); |
|
| 53 | - }); |
|
| 48 | + $container->registerService('Expiration', function($c) { |
|
| 49 | + return new Expiration( |
|
| 50 | + $c->query('ServerContainer')->getConfig(), |
|
| 51 | + $c->query(ITimeFactory::class) |
|
| 52 | + ); |
|
| 53 | + }); |
|
| 54 | 54 | |
| 55 | - /* |
|
| 55 | + /* |
|
| 56 | 56 | * Register $principalBackend for the DAV collection |
| 57 | 57 | */ |
| 58 | - $container->registerService('principalBackend', function () { |
|
| 59 | - return new Principal( |
|
| 60 | - \OC::$server->getUserManager(), |
|
| 61 | - \OC::$server->getGroupManager(), |
|
| 62 | - \OC::$server->getShareManager(), |
|
| 63 | - \OC::$server->getUserSession(), |
|
| 64 | - \OC::$server->getConfig() |
|
| 65 | - ); |
|
| 66 | - }); |
|
| 58 | + $container->registerService('principalBackend', function () { |
|
| 59 | + return new Principal( |
|
| 60 | + \OC::$server->getUserManager(), |
|
| 61 | + \OC::$server->getGroupManager(), |
|
| 62 | + \OC::$server->getShareManager(), |
|
| 63 | + \OC::$server->getUserSession(), |
|
| 64 | + \OC::$server->getConfig() |
|
| 65 | + ); |
|
| 66 | + }); |
|
| 67 | 67 | |
| 68 | - $container->registerService(ITrashManager::class, function(IAppContainer $c) { |
|
| 69 | - return new TrashManager(); |
|
| 70 | - }); |
|
| 68 | + $container->registerService(ITrashManager::class, function(IAppContainer $c) { |
|
| 69 | + return new TrashManager(); |
|
| 70 | + }); |
|
| 71 | 71 | |
| 72 | - $this->registerTrashBackends(); |
|
| 73 | - } |
|
| 72 | + $this->registerTrashBackends(); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - public function registerTrashBackends() { |
|
| 76 | - $server = $this->getContainer()->getServer(); |
|
| 77 | - $logger = $server->getLogger(); |
|
| 78 | - $appManager = $server->getAppManager(); |
|
| 79 | - /** @var ITrashManager $trashManager */ |
|
| 80 | - $trashManager = $this->getContainer()->getServer()->query(ITrashManager::class); |
|
| 81 | - foreach($appManager->getInstalledApps() as $app) { |
|
| 82 | - $appInfo = $appManager->getAppInfo($app); |
|
| 83 | - if (isset($appInfo['trash'])) { |
|
| 84 | - $backends = $appInfo['trash']; |
|
| 85 | - foreach($backends as $backend) { |
|
| 86 | - $class = $backend['@value']; |
|
| 87 | - $for = $backend['@attributes']['for']; |
|
| 75 | + public function registerTrashBackends() { |
|
| 76 | + $server = $this->getContainer()->getServer(); |
|
| 77 | + $logger = $server->getLogger(); |
|
| 78 | + $appManager = $server->getAppManager(); |
|
| 79 | + /** @var ITrashManager $trashManager */ |
|
| 80 | + $trashManager = $this->getContainer()->getServer()->query(ITrashManager::class); |
|
| 81 | + foreach($appManager->getInstalledApps() as $app) { |
|
| 82 | + $appInfo = $appManager->getAppInfo($app); |
|
| 83 | + if (isset($appInfo['trash'])) { |
|
| 84 | + $backends = $appInfo['trash']; |
|
| 85 | + foreach($backends as $backend) { |
|
| 86 | + $class = $backend['@value']; |
|
| 87 | + $for = $backend['@attributes']['for']; |
|
| 88 | 88 | |
| 89 | - try { |
|
| 90 | - $backendObject = $server->query($class); |
|
| 91 | - $trashManager->registerBackend($for, $backendObject); |
|
| 92 | - } catch (\Exception $e) { |
|
| 93 | - $logger->logException($e); |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - } |
|
| 89 | + try { |
|
| 90 | + $backendObject = $server->query($class); |
|
| 91 | + $trashManager->registerBackend($for, $backendObject); |
|
| 92 | + } catch (\Exception $e) { |
|
| 93 | + $logger->logException($e); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | 99 | } |
@@ -28,51 +28,51 @@ |
||
| 28 | 28 | * @since 15.0.0 |
| 29 | 29 | */ |
| 30 | 30 | interface ITrashItem extends FileInfo { |
| 31 | - /** |
|
| 32 | - * Get the trash backend for this item |
|
| 33 | - * |
|
| 34 | - * @return ITrashBackend |
|
| 35 | - * @since 15.0.0 |
|
| 36 | - */ |
|
| 37 | - public function getTrashBackend(): ITrashBackend; |
|
| 31 | + /** |
|
| 32 | + * Get the trash backend for this item |
|
| 33 | + * |
|
| 34 | + * @return ITrashBackend |
|
| 35 | + * @since 15.0.0 |
|
| 36 | + */ |
|
| 37 | + public function getTrashBackend(): ITrashBackend; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Get the original location for the trash item |
|
| 41 | - * |
|
| 42 | - * @return string |
|
| 43 | - * @since 15.0.0 |
|
| 44 | - */ |
|
| 45 | - public function getOriginalLocation(): string; |
|
| 39 | + /** |
|
| 40 | + * Get the original location for the trash item |
|
| 41 | + * |
|
| 42 | + * @return string |
|
| 43 | + * @since 15.0.0 |
|
| 44 | + */ |
|
| 45 | + public function getOriginalLocation(): string; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Get the timestamp that the file was moved to trash |
|
| 49 | - * |
|
| 50 | - * @return int |
|
| 51 | - * @since 15.0.0 |
|
| 52 | - */ |
|
| 53 | - public function getDeletedTime(): int; |
|
| 47 | + /** |
|
| 48 | + * Get the timestamp that the file was moved to trash |
|
| 49 | + * |
|
| 50 | + * @return int |
|
| 51 | + * @since 15.0.0 |
|
| 52 | + */ |
|
| 53 | + public function getDeletedTime(): int; |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Get the path of the item relative to the users trashbin |
|
| 57 | - * |
|
| 58 | - * @return string |
|
| 59 | - * @since 15.0.0 |
|
| 60 | - */ |
|
| 61 | - public function getTrashPath(): string; |
|
| 55 | + /** |
|
| 56 | + * Get the path of the item relative to the users trashbin |
|
| 57 | + * |
|
| 58 | + * @return string |
|
| 59 | + * @since 15.0.0 |
|
| 60 | + */ |
|
| 61 | + public function getTrashPath(): string; |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Whether the item is a deleted item in the root of the trash, or a file in a subfolder |
|
| 65 | - * |
|
| 66 | - * @return bool |
|
| 67 | - * @since 15.0.0 |
|
| 68 | - */ |
|
| 69 | - public function isRootItem(): bool; |
|
| 63 | + /** |
|
| 64 | + * Whether the item is a deleted item in the root of the trash, or a file in a subfolder |
|
| 65 | + * |
|
| 66 | + * @return bool |
|
| 67 | + * @since 15.0.0 |
|
| 68 | + */ |
|
| 69 | + public function isRootItem(): bool; |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Get the user for which this trash item applies |
|
| 73 | - * |
|
| 74 | - * @return IUser |
|
| 75 | - * @since 15.0.0 |
|
| 76 | - */ |
|
| 77 | - public function getUser(): IUser; |
|
| 71 | + /** |
|
| 72 | + * Get the user for which this trash item applies |
|
| 73 | + * |
|
| 74 | + * @return IUser |
|
| 75 | + * @since 15.0.0 |
|
| 76 | + */ |
|
| 77 | + public function getUser(): IUser; |
|
| 78 | 78 | } |
@@ -24,33 +24,33 @@ |
||
| 24 | 24 | use OCP\IUser; |
| 25 | 25 | |
| 26 | 26 | interface ITrashManager extends ITrashBackend { |
| 27 | - /** |
|
| 28 | - * Add a backend for the trashbin |
|
| 29 | - * |
|
| 30 | - * @param string $storageType |
|
| 31 | - * @param ITrashBackend $backend |
|
| 32 | - * @since 15.0.0 |
|
| 33 | - */ |
|
| 34 | - public function registerBackend(string $storageType, ITrashBackend $backend); |
|
| 27 | + /** |
|
| 28 | + * Add a backend for the trashbin |
|
| 29 | + * |
|
| 30 | + * @param string $storageType |
|
| 31 | + * @param ITrashBackend $backend |
|
| 32 | + * @since 15.0.0 |
|
| 33 | + */ |
|
| 34 | + public function registerBackend(string $storageType, ITrashBackend $backend); |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * List all trash items in the root of the trashbin |
|
| 38 | - * |
|
| 39 | - * @param IUser $user |
|
| 40 | - * @return ITrashItem[] |
|
| 41 | - * @since 15.0.0 |
|
| 42 | - */ |
|
| 43 | - public function listTrashRoot(IUser $user): array; |
|
| 36 | + /** |
|
| 37 | + * List all trash items in the root of the trashbin |
|
| 38 | + * |
|
| 39 | + * @param IUser $user |
|
| 40 | + * @return ITrashItem[] |
|
| 41 | + * @since 15.0.0 |
|
| 42 | + */ |
|
| 43 | + public function listTrashRoot(IUser $user): array; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Temporally prevent files from being moved to the trash |
|
| 47 | - * |
|
| 48 | - * @since 15.0.0 |
|
| 49 | - */ |
|
| 50 | - public function pauseTrash(); |
|
| 45 | + /** |
|
| 46 | + * Temporally prevent files from being moved to the trash |
|
| 47 | + * |
|
| 48 | + * @since 15.0.0 |
|
| 49 | + */ |
|
| 50 | + public function pauseTrash(); |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @since 15.0.0 |
|
| 54 | - */ |
|
| 55 | - public function resumeTrash(); |
|
| 52 | + /** |
|
| 53 | + * @since 15.0.0 |
|
| 54 | + */ |
|
| 55 | + public function resumeTrash(); |
|
| 56 | 56 | } |
@@ -25,148 +25,148 @@ |
||
| 25 | 25 | use OCP\IUser; |
| 26 | 26 | |
| 27 | 27 | class TrashItem implements ITrashItem { |
| 28 | - /** @var ITrashBackend */ |
|
| 29 | - private $backend; |
|
| 30 | - /** @var string */ |
|
| 31 | - private $orignalLocation; |
|
| 32 | - /** @var int */ |
|
| 33 | - private $deletedTime; |
|
| 34 | - /** @var string */ |
|
| 35 | - private $trashPath; |
|
| 36 | - /** @var FileInfo */ |
|
| 37 | - private $fileInfo; |
|
| 38 | - /** @var IUser */ |
|
| 39 | - private $user; |
|
| 40 | - |
|
| 41 | - public function __construct( |
|
| 42 | - ITrashBackend $backend, |
|
| 43 | - string $originalLocation, |
|
| 44 | - int $deletedTime, |
|
| 45 | - string $trashPath, |
|
| 46 | - FileInfo $fileInfo, |
|
| 47 | - IUser $user |
|
| 48 | - ) { |
|
| 49 | - $this->backend = $backend; |
|
| 50 | - $this->orignalLocation = $originalLocation; |
|
| 51 | - $this->deletedTime = $deletedTime; |
|
| 52 | - $this->trashPath = $trashPath; |
|
| 53 | - $this->fileInfo = $fileInfo; |
|
| 54 | - $this->user = $user; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public function getTrashBackend(): ITrashBackend { |
|
| 58 | - return $this->backend; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - public function getOriginalLocation(): string { |
|
| 62 | - return $this->orignalLocation; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - public function getDeletedTime(): int { |
|
| 66 | - return $this->deletedTime; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - public function getTrashPath(): string { |
|
| 70 | - return $this->trashPath; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - public function isRootItem(): bool { |
|
| 74 | - return substr_count($this->getTrashPath(), '/') === 1; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function getUser(): IUser { |
|
| 78 | - return $this->user; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function getEtag() { |
|
| 82 | - return $this->fileInfo->getEtag(); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public function getSize() { |
|
| 86 | - return $this->fileInfo->getSize(); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function getMtime() { |
|
| 90 | - return $this->fileInfo->getMtime(); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - public function getName() { |
|
| 94 | - return $this->fileInfo->getName(); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - public function getInternalPath() { |
|
| 98 | - return $this->fileInfo->getInternalPath(); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public function getPath() { |
|
| 102 | - return $this->fileInfo->getPath(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - public function getMimetype() { |
|
| 106 | - return $this->fileInfo->getMimetype(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - public function getMimePart() { |
|
| 110 | - return $this->fileInfo->getMimePart(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function getStorage() { |
|
| 114 | - return $this->fileInfo->getStorage(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - public function getId() { |
|
| 118 | - return $this->fileInfo->getId(); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function isEncrypted() { |
|
| 122 | - return $this->fileInfo->isEncrypted(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - public function getPermissions() { |
|
| 126 | - return $this->fileInfo->getPermissions(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - public function getType() { |
|
| 130 | - return $this->fileInfo->getType(); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - public function isReadable() { |
|
| 134 | - return $this->fileInfo->isReadable(); |
|
| 135 | - } |
|
| 28 | + /** @var ITrashBackend */ |
|
| 29 | + private $backend; |
|
| 30 | + /** @var string */ |
|
| 31 | + private $orignalLocation; |
|
| 32 | + /** @var int */ |
|
| 33 | + private $deletedTime; |
|
| 34 | + /** @var string */ |
|
| 35 | + private $trashPath; |
|
| 36 | + /** @var FileInfo */ |
|
| 37 | + private $fileInfo; |
|
| 38 | + /** @var IUser */ |
|
| 39 | + private $user; |
|
| 40 | + |
|
| 41 | + public function __construct( |
|
| 42 | + ITrashBackend $backend, |
|
| 43 | + string $originalLocation, |
|
| 44 | + int $deletedTime, |
|
| 45 | + string $trashPath, |
|
| 46 | + FileInfo $fileInfo, |
|
| 47 | + IUser $user |
|
| 48 | + ) { |
|
| 49 | + $this->backend = $backend; |
|
| 50 | + $this->orignalLocation = $originalLocation; |
|
| 51 | + $this->deletedTime = $deletedTime; |
|
| 52 | + $this->trashPath = $trashPath; |
|
| 53 | + $this->fileInfo = $fileInfo; |
|
| 54 | + $this->user = $user; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public function getTrashBackend(): ITrashBackend { |
|
| 58 | + return $this->backend; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + public function getOriginalLocation(): string { |
|
| 62 | + return $this->orignalLocation; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + public function getDeletedTime(): int { |
|
| 66 | + return $this->deletedTime; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + public function getTrashPath(): string { |
|
| 70 | + return $this->trashPath; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + public function isRootItem(): bool { |
|
| 74 | + return substr_count($this->getTrashPath(), '/') === 1; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function getUser(): IUser { |
|
| 78 | + return $this->user; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function getEtag() { |
|
| 82 | + return $this->fileInfo->getEtag(); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public function getSize() { |
|
| 86 | + return $this->fileInfo->getSize(); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function getMtime() { |
|
| 90 | + return $this->fileInfo->getMtime(); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + public function getName() { |
|
| 94 | + return $this->fileInfo->getName(); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + public function getInternalPath() { |
|
| 98 | + return $this->fileInfo->getInternalPath(); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public function getPath() { |
|
| 102 | + return $this->fileInfo->getPath(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + public function getMimetype() { |
|
| 106 | + return $this->fileInfo->getMimetype(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + public function getMimePart() { |
|
| 110 | + return $this->fileInfo->getMimePart(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function getStorage() { |
|
| 114 | + return $this->fileInfo->getStorage(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + public function getId() { |
|
| 118 | + return $this->fileInfo->getId(); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function isEncrypted() { |
|
| 122 | + return $this->fileInfo->isEncrypted(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + public function getPermissions() { |
|
| 126 | + return $this->fileInfo->getPermissions(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + public function getType() { |
|
| 130 | + return $this->fileInfo->getType(); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + public function isReadable() { |
|
| 134 | + return $this->fileInfo->isReadable(); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - public function isUpdateable() { |
|
| 138 | - return $this->fileInfo->isUpdateable(); |
|
| 139 | - } |
|
| 137 | + public function isUpdateable() { |
|
| 138 | + return $this->fileInfo->isUpdateable(); |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - public function isCreatable() { |
|
| 142 | - return $this->fileInfo->isCreatable(); |
|
| 143 | - } |
|
| 141 | + public function isCreatable() { |
|
| 142 | + return $this->fileInfo->isCreatable(); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - public function isDeletable() { |
|
| 146 | - return $this->fileInfo->isDeletable(); |
|
| 147 | - } |
|
| 145 | + public function isDeletable() { |
|
| 146 | + return $this->fileInfo->isDeletable(); |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - public function isShareable() { |
|
| 150 | - return $this->fileInfo->isShareable(); |
|
| 151 | - } |
|
| 149 | + public function isShareable() { |
|
| 150 | + return $this->fileInfo->isShareable(); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - public function isShared() { |
|
| 154 | - return $this->fileInfo->isShared(); |
|
| 155 | - } |
|
| 153 | + public function isShared() { |
|
| 154 | + return $this->fileInfo->isShared(); |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - public function isMounted() { |
|
| 158 | - return $this->fileInfo->isMounted(); |
|
| 159 | - } |
|
| 157 | + public function isMounted() { |
|
| 158 | + return $this->fileInfo->isMounted(); |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - public function getMountPoint() { |
|
| 162 | - return $this->fileInfo->getMountPoint(); |
|
| 163 | - } |
|
| 161 | + public function getMountPoint() { |
|
| 162 | + return $this->fileInfo->getMountPoint(); |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - public function getOwner() { |
|
| 166 | - return $this->fileInfo->getOwner(); |
|
| 167 | - } |
|
| 165 | + public function getOwner() { |
|
| 166 | + return $this->fileInfo->getOwner(); |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - public function getChecksum() { |
|
| 170 | - return $this->fileInfo->getChecksum(); |
|
| 171 | - } |
|
| 169 | + public function getChecksum() { |
|
| 170 | + return $this->fileInfo->getChecksum(); |
|
| 171 | + } |
|
| 172 | 172 | } |
@@ -31,54 +31,54 @@ |
||
| 31 | 31 | * @since 15.0.0 |
| 32 | 32 | */ |
| 33 | 33 | interface ITrashBackend { |
| 34 | - /** |
|
| 35 | - * List all trash items in the root of the trashbin |
|
| 36 | - * |
|
| 37 | - * @param IUser $user |
|
| 38 | - * @return ITrashItem[] |
|
| 39 | - * @since 15.0.0 |
|
| 40 | - */ |
|
| 41 | - public function listTrashRoot(IUser $user): array; |
|
| 34 | + /** |
|
| 35 | + * List all trash items in the root of the trashbin |
|
| 36 | + * |
|
| 37 | + * @param IUser $user |
|
| 38 | + * @return ITrashItem[] |
|
| 39 | + * @since 15.0.0 |
|
| 40 | + */ |
|
| 41 | + public function listTrashRoot(IUser $user): array; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * List all trash items in a subfolder in the trashbin |
|
| 45 | - * |
|
| 46 | - * @param ITrashItem $folder |
|
| 47 | - * @return ITrashItem[] |
|
| 48 | - * @since 15.0.0 |
|
| 49 | - */ |
|
| 50 | - public function listTrashFolder(ITrashItem $folder): array; |
|
| 43 | + /** |
|
| 44 | + * List all trash items in a subfolder in the trashbin |
|
| 45 | + * |
|
| 46 | + * @param ITrashItem $folder |
|
| 47 | + * @return ITrashItem[] |
|
| 48 | + * @since 15.0.0 |
|
| 49 | + */ |
|
| 50 | + public function listTrashFolder(ITrashItem $folder): array; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Restore a trashbin item |
|
| 54 | - * |
|
| 55 | - * @param ITrashItem $item |
|
| 56 | - * @since 15.0.0 |
|
| 57 | - */ |
|
| 58 | - public function restoreItem(ITrashItem $item); |
|
| 52 | + /** |
|
| 53 | + * Restore a trashbin item |
|
| 54 | + * |
|
| 55 | + * @param ITrashItem $item |
|
| 56 | + * @since 15.0.0 |
|
| 57 | + */ |
|
| 58 | + public function restoreItem(ITrashItem $item); |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Permanently remove an item from trash |
|
| 62 | - * |
|
| 63 | - * @param ITrashItem $item |
|
| 64 | - * @since 15.0.0 |
|
| 65 | - */ |
|
| 66 | - public function removeItem(ITrashItem $item); |
|
| 60 | + /** |
|
| 61 | + * Permanently remove an item from trash |
|
| 62 | + * |
|
| 63 | + * @param ITrashItem $item |
|
| 64 | + * @since 15.0.0 |
|
| 65 | + */ |
|
| 66 | + public function removeItem(ITrashItem $item); |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Move a file or folder to trash |
|
| 70 | - * |
|
| 71 | - * @param IStorage $storage |
|
| 72 | - * @param string $internalPath |
|
| 73 | - * @return boolean whether or not the file was moved to trash, if false then the file should be deleted normally |
|
| 74 | - * @since 15.0.0 |
|
| 75 | - */ |
|
| 76 | - public function moveToTrash(IStorage $storage, string $internalPath): bool; |
|
| 68 | + /** |
|
| 69 | + * Move a file or folder to trash |
|
| 70 | + * |
|
| 71 | + * @param IStorage $storage |
|
| 72 | + * @param string $internalPath |
|
| 73 | + * @return boolean whether or not the file was moved to trash, if false then the file should be deleted normally |
|
| 74 | + * @since 15.0.0 |
|
| 75 | + */ |
|
| 76 | + public function moveToTrash(IStorage $storage, string $internalPath): bool; |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * @param IUser $user |
|
| 80 | - * @param int $fileId |
|
| 81 | - * @return Node|null |
|
| 82 | - */ |
|
| 83 | - public function getTrashNodeById(IUser $user, int $fileId); |
|
| 78 | + /** |
|
| 79 | + * @param IUser $user |
|
| 80 | + * @param int $fileId |
|
| 81 | + * @return Node|null |
|
| 82 | + */ |
|
| 83 | + public function getTrashNodeById(IUser $user, int $fileId); |
|
| 84 | 84 | } |
@@ -27,99 +27,99 @@ |
||
| 27 | 27 | use OCP\IUser; |
| 28 | 28 | |
| 29 | 29 | class TrashManager implements ITrashManager { |
| 30 | - /** @var ITrashBackend[] */ |
|
| 31 | - private $backends = []; |
|
| 32 | - |
|
| 33 | - private $trashPaused = false; |
|
| 34 | - |
|
| 35 | - public function registerBackend(string $storageType, ITrashBackend $backend) { |
|
| 36 | - $this->backends[$storageType] = $backend; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @return ITrashBackend[] |
|
| 41 | - */ |
|
| 42 | - private function getBackends(): array { |
|
| 43 | - return $this->backends; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function listTrashRoot(IUser $user): array { |
|
| 47 | - $items = array_reduce($this->getBackends(), function (array $items, ITrashBackend $backend) use ($user) { |
|
| 48 | - return array_merge($items, $backend->listTrashRoot($user)); |
|
| 49 | - }, []); |
|
| 50 | - usort($items, function (ITrashItem $a, ITrashItem $b) { |
|
| 51 | - return $a->getDeletedTime() - $b->getDeletedTime(); |
|
| 52 | - }); |
|
| 53 | - return $items; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - private function getBackendForItem(ITrashItem $item) { |
|
| 57 | - return $item->getTrashBackend(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public function listTrashFolder(ITrashItem $folder): array { |
|
| 61 | - return $this->getBackendForItem($folder)->listTrashFolder($folder); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - public function restoreItem(ITrashItem $item) { |
|
| 65 | - return $this->getBackendForItem($item)->restoreItem($item); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public function removeItem(ITrashItem $item) { |
|
| 69 | - $this->getBackendForItem($item)->removeItem($item); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param IStorage $storage |
|
| 74 | - * @return ITrashBackend |
|
| 75 | - * @throws BackendNotFoundException |
|
| 76 | - */ |
|
| 77 | - public function getBackendForStorage(IStorage $storage): ITrashBackend { |
|
| 78 | - $fullType = get_class($storage); |
|
| 79 | - $foundType = array_reduce(array_keys($this->backends), function ($type, $registeredType) use ($storage) { |
|
| 80 | - if ( |
|
| 81 | - $storage->instanceOfStorage($registeredType) && |
|
| 82 | - ($type === '' || is_subclass_of($registeredType, $type)) |
|
| 83 | - ) { |
|
| 84 | - return $registeredType; |
|
| 85 | - } else { |
|
| 86 | - return $type; |
|
| 87 | - } |
|
| 88 | - }, ''); |
|
| 89 | - if ($foundType === '') { |
|
| 90 | - throw new BackendNotFoundException("Trash backend for $fullType not found"); |
|
| 91 | - } else { |
|
| 92 | - return $this->backends[$foundType]; |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - public function moveToTrash(IStorage $storage, string $internalPath): bool { |
|
| 97 | - if ($this->trashPaused) { |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 100 | - try { |
|
| 101 | - $backend = $this->getBackendForStorage($storage); |
|
| 102 | - return $backend->moveToTrash($storage, $internalPath); |
|
| 103 | - } catch (BackendNotFoundException $e) { |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - public function getTrashNodeById(IUser $user, int $fileId) { |
|
| 109 | - foreach ($this->backends as $backend) { |
|
| 110 | - $item = $backend->getTrashNodeById($user, $fileId); |
|
| 111 | - if ($item !== null) { |
|
| 112 | - return $item; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - return null; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function pauseTrash() { |
|
| 119 | - $this->trashPaused = true; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - public function resumeTrash() { |
|
| 123 | - $this->trashPaused = false; |
|
| 124 | - } |
|
| 30 | + /** @var ITrashBackend[] */ |
|
| 31 | + private $backends = []; |
|
| 32 | + |
|
| 33 | + private $trashPaused = false; |
|
| 34 | + |
|
| 35 | + public function registerBackend(string $storageType, ITrashBackend $backend) { |
|
| 36 | + $this->backends[$storageType] = $backend; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @return ITrashBackend[] |
|
| 41 | + */ |
|
| 42 | + private function getBackends(): array { |
|
| 43 | + return $this->backends; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function listTrashRoot(IUser $user): array { |
|
| 47 | + $items = array_reduce($this->getBackends(), function (array $items, ITrashBackend $backend) use ($user) { |
|
| 48 | + return array_merge($items, $backend->listTrashRoot($user)); |
|
| 49 | + }, []); |
|
| 50 | + usort($items, function (ITrashItem $a, ITrashItem $b) { |
|
| 51 | + return $a->getDeletedTime() - $b->getDeletedTime(); |
|
| 52 | + }); |
|
| 53 | + return $items; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + private function getBackendForItem(ITrashItem $item) { |
|
| 57 | + return $item->getTrashBackend(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public function listTrashFolder(ITrashItem $folder): array { |
|
| 61 | + return $this->getBackendForItem($folder)->listTrashFolder($folder); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + public function restoreItem(ITrashItem $item) { |
|
| 65 | + return $this->getBackendForItem($item)->restoreItem($item); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public function removeItem(ITrashItem $item) { |
|
| 69 | + $this->getBackendForItem($item)->removeItem($item); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param IStorage $storage |
|
| 74 | + * @return ITrashBackend |
|
| 75 | + * @throws BackendNotFoundException |
|
| 76 | + */ |
|
| 77 | + public function getBackendForStorage(IStorage $storage): ITrashBackend { |
|
| 78 | + $fullType = get_class($storage); |
|
| 79 | + $foundType = array_reduce(array_keys($this->backends), function ($type, $registeredType) use ($storage) { |
|
| 80 | + if ( |
|
| 81 | + $storage->instanceOfStorage($registeredType) && |
|
| 82 | + ($type === '' || is_subclass_of($registeredType, $type)) |
|
| 83 | + ) { |
|
| 84 | + return $registeredType; |
|
| 85 | + } else { |
|
| 86 | + return $type; |
|
| 87 | + } |
|
| 88 | + }, ''); |
|
| 89 | + if ($foundType === '') { |
|
| 90 | + throw new BackendNotFoundException("Trash backend for $fullType not found"); |
|
| 91 | + } else { |
|
| 92 | + return $this->backends[$foundType]; |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + public function moveToTrash(IStorage $storage, string $internalPath): bool { |
|
| 97 | + if ($this->trashPaused) { |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | + try { |
|
| 101 | + $backend = $this->getBackendForStorage($storage); |
|
| 102 | + return $backend->moveToTrash($storage, $internalPath); |
|
| 103 | + } catch (BackendNotFoundException $e) { |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + public function getTrashNodeById(IUser $user, int $fileId) { |
|
| 109 | + foreach ($this->backends as $backend) { |
|
| 110 | + $item = $backend->getTrashNodeById($user, $fileId); |
|
| 111 | + if ($item !== null) { |
|
| 112 | + return $item; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + return null; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function pauseTrash() { |
|
| 119 | + $this->trashPaused = true; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + public function resumeTrash() { |
|
| 123 | + $this->trashPaused = false; |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -25,7 +25,7 @@ |
||
| 25 | 25 | |
| 26 | 26 | |
| 27 | 27 | class TrashFolderFile extends AbstractTrashFile { |
| 28 | - public function get() { |
|
| 29 | - return $this->data->getStorage()->fopen($this->data->getInternalPath(), 'rb'); |
|
| 30 | - } |
|
| 28 | + public function get() { |
|
| 29 | + return $this->data->getStorage()->fopen($this->data->getInternalPath(), 'rb'); |
|
| 30 | + } |
|
| 31 | 31 | } |