Skip to content

Commit

Permalink
Files trashbin app: Update doc types
Browse files Browse the repository at this point in the history
Signed-off-by: Danial Rahimi <48244647+danialRahimy@users.noreply.github.com>
  • Loading branch information
danialRahimy committed Aug 16, 2023
1 parent dfceb76 commit a531255
Showing 1 changed file with 101 additions and 5 deletions.
106 changes: 101 additions & 5 deletions apps/files_trashbin/lib/Trashbin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
$uid = Filesystem::getOwner($filename);
Expand Down Expand Up @@ -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 {
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
Expand All @@ -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 {
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
public static function move2trash($file_path, $ownerOnly = false): bool {
// get the user for which the filesystem is setup
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -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');
Expand Down

0 comments on commit a531255

Please sign in to comment.