-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Refactor files trashbin app #39883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
danialRahimy
wants to merge
14
commits into
nextcloud:master
from
danialRahimy:app_files_trashbin_refactor
Closed
Refactor files trashbin app #39883
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
558c146
Files trashbin app: Return early in method retainVersions
danialRahimy 81c4780
Files trashbin app: Change the const name for more readability
danialRahimy 2c298c0
Files trashbin app: Adding functions' return type
danialRahimy 36338e8
Files trashbin app: Return early
danialRahimy dfceb76
Files trashbin app: Remove extra spaces between methods
danialRahimy a531255
Files trashbin app: Update doc types
danialRahimy 82dd6fa
Files trashbin app: Add remaining return type.
danialRahimy 8bd6a2f
Files trashbin app: Add parameter type to the methods according to th…
danialRahimy dcfcce8
Files trashbin app: Remove unrelated throw tag for clarification exc…
danialRahimy e1fa130
Update apps/files_trashbin/lib/Trashbin.php
danialRahimy b55f595
Update apps/files_trashbin/lib/Trashbin.php
danialRahimy 863dd39
Update apps/files_trashbin/lib/Trashbin.php
danialRahimy a766334
Update apps/files_trashbin/lib/Trashbin.php
danialRahimy 2186f0d
Update apps/files_trashbin/lib/Trashbin.php
danialRahimy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Files trashbin app: Update doc types
Signed-off-by: Danial Rahimi <[email protected]>
- Loading branch information
commit a531255032b4055443e28640c779acfccc2019fe
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -44,6 +44,8 @@ | |||||
| */ | ||||||
| namespace OCA\Files_Trashbin; | ||||||
|
|
||||||
| use OC\ServerNotAvailableException; | ||||||
| use OC\User\NoUserException; | ||||||
| use OC_User; | ||||||
| use OC\Files\Cache\Cache; | ||||||
| use OC\Files\Cache\CacheEntry; | ||||||
|
|
@@ -53,15 +55,22 @@ | |||||
| use OC\Files\View; | ||||||
| use OCA\Files_Trashbin\AppInfo\Application; | ||||||
| use OCA\Files_Trashbin\Command\Expire; | ||||||
| use OCA\Files_Trashbin\Exceptions\CopyRecursiveException; | ||||||
| use OCP\AppFramework\QueryException; | ||||||
| use OCP\AppFramework\Utility\ITimeFactory; | ||||||
| use OCP\App\IAppManager; | ||||||
| use OCP\DB\Exception; | ||||||
| use OCP\Files\File; | ||||||
| use OCP\Files\Folder; | ||||||
| use OCP\Files\InvalidPathException; | ||||||
| use OCP\Files\NotFoundException; | ||||||
| use OCP\Files\NotPermittedException; | ||||||
| use OCP\HintException; | ||||||
| use OCP\IConfig; | ||||||
| use OCP\Lock\ILockingProvider; | ||||||
| use OCP\Lock\LockedException; | ||||||
| use Psr\Container\ContainerExceptionInterface; | ||||||
| use Psr\Container\NotFoundExceptionInterface; | ||||||
| use Psr\Log\LoggerInterface; | ||||||
|
|
||||||
| class Trashbin { | ||||||
|
|
@@ -73,6 +82,7 @@ class Trashbin { | |||||
| * by triggering the scan in the pre-hook | ||||||
| * | ||||||
| * @param array $params | ||||||
| * @throws NoUserException | ||||||
| */ | ||||||
| public static function ensureFileScannedHook($params): void { | ||||||
| try { | ||||||
|
|
@@ -88,7 +98,7 @@ public static function ensureFileScannedHook($params): void { | |||||
| * | ||||||
| * @param string $filename | ||||||
| * @return array | ||||||
| * @throws \OC\User\NoUserException | ||||||
| * @throws NoUserException | ||||||
| */ | ||||||
| public static function getUidAndFilename($filename): array { | ||||||
danialRahimy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| $uid = Filesystem::getOwner($filename); | ||||||
|
|
@@ -121,6 +131,7 @@ public static function getUidAndFilename($filename): array { | |||||
| * | ||||||
| * @param string $user | ||||||
| * @return array (filename => array (timestamp => original location)) | ||||||
| * @throws Exception | ||||||
| */ | ||||||
| public static function getLocations($user): array { | ||||||
danialRahimy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); | ||||||
|
|
@@ -147,6 +158,7 @@ public static function getLocations($user): array { | |||||
| * @param string $filename | ||||||
| * @param string $timestamp | ||||||
| * @return bool|string original location | ||||||
| * @throws Exception | ||||||
| */ | ||||||
| public static function getLocation($user, $filename, $timestamp): bool|string { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); | ||||||
|
|
@@ -191,6 +203,10 @@ private static function setUpTrash($user): void { | |||||
| * @param string $targetPath | ||||||
| * @param $user | ||||||
| * @param int $timestamp | ||||||
| * @throws Exception | ||||||
| * @throws CopyRecursiveException | ||||||
| * @throws QueryException | ||||||
| * @throws InvalidPathException | ||||||
| */ | ||||||
| private static function copyFilesToUser($sourcePath, $owner, $targetPath, $user, $timestamp): void { | ||||||
|
||||||
| self::setUpTrash($owner); | ||||||
|
|
@@ -231,8 +247,14 @@ private static function copyFilesToUser($sourcePath, $owner, $targetPath, $user, | |||||
| * | ||||||
| * @param string $file_path path to the deleted file/directory relative to the files root directory | ||||||
| * @param bool $ownerOnly delete for owner only (if file gets moved out of a shared folder) | ||||||
| * | ||||||
| * @return bool | ||||||
| * @throws Exception | ||||||
| * @throws CopyRecursiveException | ||||||
| * @throws InvalidPathException | ||||||
| * @throws LockedException | ||||||
| * @throws NoUserException | ||||||
| * @throws QueryException | ||||||
| * @throws ContainerExceptionInterface | ||||||
danialRahimy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| */ | ||||||
| public static function move2trash($file_path, $ownerOnly = false): bool { | ||||||
| // get the user for which the filesystem is setup | ||||||
|
|
@@ -395,6 +417,9 @@ private static function getConfiguredTrashbinSize(string $user): int|float { | |||||
| * @param string $owner owner user id | ||||||
| * @param string $ownerPath path relative to the owner's home storage | ||||||
| * @param int $timestamp when the file was deleted | ||||||
| * @throws CopyRecursiveException | ||||||
| * @throws ContainerExceptionInterface | ||||||
| * @throws NotFoundExceptionInterface | ||||||
| */ | ||||||
| private static function retainVersions($filename, $owner, $ownerPath, $timestamp): void { | ||||||
| if (!\OCP\Server::get(IAppManager::class)->isEnabledForUser('files_versions') || empty($ownerPath)) { | ||||||
|
|
@@ -470,8 +495,14 @@ private static function copy(View $view, $source, $target) { | |||||
| * including the timestamp suffix ".d12345678" | ||||||
| * @param string $filename name of the file/folder | ||||||
| * @param int $timestamp time when the file/folder was deleted | ||||||
| * | ||||||
| * @return bool true on success, false otherwise | ||||||
| * @throws Exception | ||||||
| * @throws ContainerExceptionInterface | ||||||
| * @throws LockedException | ||||||
| * @throws NoUserException | ||||||
| * @throws NotFoundExceptionInterface | ||||||
| * @throws NotPermittedException | ||||||
| * @throws QueryException | ||||||
| */ | ||||||
| public static function restore($file, $filename, $timestamp): bool { | ||||||
| $user = OC_User::getUser(); | ||||||
|
|
@@ -545,6 +576,11 @@ public static function restore($file, $filename, $timestamp): bool { | |||||
| * @param string $location location if file | ||||||
| * @param int $timestamp deletion time | ||||||
| * @return false|null | ||||||
| * @throws Exception | ||||||
| * @throws ContainerExceptionInterface | ||||||
| * @throws LockedException | ||||||
| * @throws NoUserException | ||||||
| * @throws NotFoundExceptionInterface | ||||||
| */ | ||||||
| private static function restoreVersions(View $view, $file, $filename, $uniqueFilename, $location, $timestamp): ?bool { | ||||||
| if (! \OCP\Server::get(IAppManager::class)->isEnabledForUser('files_versions')) { | ||||||
|
|
@@ -638,6 +674,8 @@ public static function deleteAll(): bool { | |||||
| * wrapper function to emit the 'preDelete' hook of \OCP\Trashbin before a file is deleted | ||||||
| * | ||||||
| * @param string $path | ||||||
| * @throws HintException | ||||||
| * @throws ServerNotAvailableException | ||||||
| */ | ||||||
| protected static function emitTrashbinPreDelete($path): void { | ||||||
| \OC_Hook::emit('\OCP\Trashbin', 'preDelete', ['path' => $path]); | ||||||
|
|
@@ -647,6 +685,8 @@ protected static function emitTrashbinPreDelete($path): void { | |||||
| * wrapper function to emit the 'delete' hook of \OCP\Trashbin after a file has been deleted | ||||||
| * | ||||||
| * @param string $path | ||||||
| * @throws HintException | ||||||
| * @throws ServerNotAvailableException | ||||||
| */ | ||||||
| protected static function emitTrashbinPostDelete($path): void { | ||||||
| \OC_Hook::emit('\OCP\Trashbin', 'delete', ['path' => $path]); | ||||||
|
|
@@ -657,9 +697,16 @@ protected static function emitTrashbinPostDelete($path): void { | |||||
| * | ||||||
| * @param string $filename path to the file | ||||||
| * @param string $user | ||||||
| * @param int $timestamp of deletion time | ||||||
| * | ||||||
| * @param null $timestamp of deletion time | ||||||
| * @return int|float size of deleted files | ||||||
| * @throws Exception | ||||||
| * @throws ContainerExceptionInterface | ||||||
| * @throws HintException | ||||||
| * @throws InvalidPathException | ||||||
| * @throws NotFoundException | ||||||
| * @throws NotFoundExceptionInterface | ||||||
| * @throws NotPermittedException | ||||||
| * @throws ServerNotAvailableException | ||||||
| */ | ||||||
| public static function delete($filename, $user, $timestamp = null): float|int { | ||||||
| $userRoot = \OC::$server->getUserFolder($user)->getParent(); | ||||||
|
|
@@ -701,9 +748,15 @@ public static function delete($filename, $user, $timestamp = null): float|int { | |||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @param View $view | ||||||
| * @param string $file | ||||||
| * @param string $filename | ||||||
| * @param ?int $timestamp | ||||||
| * @param string $user | ||||||
| * @return int|float | ||||||
| * @throws Exception | ||||||
| * @throws ContainerExceptionInterface | ||||||
| * @throws NotFoundExceptionInterface | ||||||
| */ | ||||||
| private static function deleteVersions(View $view, $file, $filename, $timestamp, string $user): int|float { | ||||||
| $size = 0; | ||||||
|
|
@@ -736,6 +789,7 @@ private static function deleteVersions(View $view, $file, $filename, $timestamp, | |||||
| * @param string $filename path to the file | ||||||
| * @param int $timestamp of deletion time | ||||||
| * @return bool true if file exists, otherwise false | ||||||
| * @throws \Exception | ||||||
| */ | ||||||
| public static function file_exists($filename, $timestamp = null) { | ||||||
| $user = OC_User::getUser(); | ||||||
|
|
@@ -754,6 +808,7 @@ public static function file_exists($filename, $timestamp = null) { | |||||
| * | ||||||
| * @param string $uid id of deleted user | ||||||
| * @return bool result of db delete operation | ||||||
| * @throws Exception | ||||||
| */ | ||||||
| public static function deleteUser($uid): bool { | ||||||
| $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); | ||||||
|
|
@@ -768,6 +823,8 @@ public static function deleteUser($uid): bool { | |||||
| * @param int|float $trashbinSize current size of the trash bin | ||||||
| * @param string $user | ||||||
| * @return int|float available free space for trash bin | ||||||
| * @throws InvalidPathException | ||||||
| * @throws NotFoundException | ||||||
| */ | ||||||
| private static function calculateFreeSpace(int|float $trashbinSize, string $user): int|float { | ||||||
| $configuredTrashbinSize = static::getConfiguredTrashbinSize($user); | ||||||
|
|
@@ -820,6 +877,11 @@ private static function calculateFreeSpace(int|float $trashbinSize, string $user | |||||
| * resize trash bin if necessary after a new file was added to Nextcloud | ||||||
| * | ||||||
| * @param string $user user id | ||||||
| * @throws ContainerExceptionInterface | ||||||
| * @throws InvalidPathException | ||||||
| * @throws NotFoundException | ||||||
| * @throws NotFoundExceptionInterface | ||||||
| * @throws QueryException | ||||||
| */ | ||||||
| public static function resizeTrash($user): void { | ||||||
| $size = self::getTrashbinSize($user); | ||||||
|
|
@@ -835,6 +897,15 @@ public static function resizeTrash($user): void { | |||||
| * clean up the trash bin | ||||||
| * | ||||||
| * @param string $user | ||||||
| * @throws Exception | ||||||
| * @throws ContainerExceptionInterface | ||||||
| * @throws HintException | ||||||
| * @throws InvalidPathException | ||||||
| * @throws NotFoundException | ||||||
| * @throws NotFoundExceptionInterface | ||||||
| * @throws NotPermittedException | ||||||
| * @throws QueryException | ||||||
| * @throws ServerNotAvailableException | ||||||
| */ | ||||||
| public static function expire($user): void { | ||||||
| $trashBinSize = self::getTrashbinSize($user); | ||||||
|
|
@@ -853,6 +924,9 @@ public static function expire($user): void { | |||||
|
|
||||||
| /** | ||||||
| * @param string $user | ||||||
| * @throws ContainerExceptionInterface | ||||||
| * @throws NotFoundExceptionInterface | ||||||
| * @throws QueryException | ||||||
| */ | ||||||
| private static function scheduleExpire($user): void { | ||||||
| // let the admin disable auto expire | ||||||
|
|
@@ -872,6 +946,15 @@ private static function scheduleExpire($user): void { | |||||
| * @param string $user | ||||||
| * @param int|float $availableSpace available disc space | ||||||
| * @return int|float size of deleted files | ||||||
| * @throws Exception | ||||||
| * @throws ContainerExceptionInterface | ||||||
| * @throws HintException | ||||||
| * @throws InvalidPathException | ||||||
| * @throws NotFoundException | ||||||
| * @throws NotFoundExceptionInterface | ||||||
| * @throws NotPermittedException | ||||||
| * @throws QueryException | ||||||
| * @throws ServerNotAvailableException | ||||||
| */ | ||||||
| protected static function deleteFiles(array $files, string $user, int|float $availableSpace): int|float { | ||||||
| /** @var Application $application */ | ||||||
|
|
@@ -900,6 +983,13 @@ protected static function deleteFiles(array $files, string $user, int|float $ava | |||||
| * @param array $files list of files sorted by mtime | ||||||
| * @param string $user | ||||||
| * @return array{int|float, int} size of deleted files and number of deleted files | ||||||
| * @throws Exception | ||||||
| * @throws HintException | ||||||
| * @throws InvalidPathException | ||||||
| * @throws NotFoundException | ||||||
| * @throws QueryException | ||||||
| * @throws ServerNotAvailableException | ||||||
| * @throws ContainerExceptionInterface | ||||||
| */ | ||||||
| public static function deleteExpiredFiles($files, $user): array { | ||||||
| /** @var Expiration $expiration */ | ||||||
|
|
@@ -976,6 +1066,10 @@ private static function copy_recursive($source, $destination, View $view): int|f | |||||
| * | ||||||
| * @param string $filename name of the file which should be restored | ||||||
| * @param int $timestamp timestamp when the file was deleted | ||||||
| * @param string $user | ||||||
| * @return array | ||||||
| * @throws Exception | ||||||
| * @throws QueryException | ||||||
| */ | ||||||
| private static function getVersionsFromTrash($filename, $timestamp, string $user): array { | ||||||
| $view = new View('/' . $user . '/files_trashbin/versions'); | ||||||
|
|
@@ -1104,6 +1198,7 @@ private static function calculateSize(View $view): int|float { | |||||
| * | ||||||
| * @param string $user user who owns the trash bin | ||||||
| * @return int|float trash bin size | ||||||
| * @throws \Exception | ||||||
| */ | ||||||
| private static function getTrashbinSize(string $user): int|float { | ||||||
| $view = new View('/' . $user); | ||||||
|
|
@@ -1116,6 +1211,7 @@ private static function getTrashbinSize(string $user): int|float { | |||||
| * | ||||||
| * @param string $user | ||||||
| * @return bool | ||||||
| * @throws \Exception | ||||||
| */ | ||||||
| public static function isEmpty($user): bool { | ||||||
| $view = new View('/' . $user . '/files_trashbin'); | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.