diff --git a/apps/admin_audit/composer/composer/autoload_classmap.php b/apps/admin_audit/composer/composer/autoload_classmap.php index 72bf58847b828..b9883639590fd 100644 --- a/apps/admin_audit/composer/composer/autoload_classmap.php +++ b/apps/admin_audit/composer/composer/autoload_classmap.php @@ -10,7 +10,6 @@ 'OCA\\AdminAudit\\Actions\\Action' => $baseDir . '/../lib/Actions/Action.php', 'OCA\\AdminAudit\\Actions\\Files' => $baseDir . '/../lib/Actions/Files.php', 'OCA\\AdminAudit\\Actions\\Sharing' => $baseDir . '/../lib/Actions/Sharing.php', - 'OCA\\AdminAudit\\Actions\\Trashbin' => $baseDir . '/../lib/Actions/Trashbin.php', 'OCA\\AdminAudit\\Actions\\Versions' => $baseDir . '/../lib/Actions/Versions.php', 'OCA\\AdminAudit\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php', 'OCA\\AdminAudit\\AuditLogger' => $baseDir . '/../lib/AuditLogger.php', @@ -26,6 +25,7 @@ 'OCA\\AdminAudit\\Listener\\SecurityEventListener' => $baseDir . '/../lib/Listener/SecurityEventListener.php', 'OCA\\AdminAudit\\Listener\\SharingEventListener' => $baseDir . '/../lib/Listener/SharingEventListener.php', 'OCA\\AdminAudit\\Listener\\TagEventListener' => $baseDir . '/../lib/Listener/TagEventListener.php', + 'OCA\\AdminAudit\\Listener\\TrashbinEventListener' => $baseDir . '/../lib/Listener/TrashbinEventListener.php', 'OCA\\AdminAudit\\Listener\\UserManagementEventListener' => $baseDir . '/../lib/Listener/UserManagementEventListener.php', 'OCA\\AdminAudit\\Operation' => $baseDir . '/../lib/Operation.php', ); diff --git a/apps/admin_audit/composer/composer/autoload_static.php b/apps/admin_audit/composer/composer/autoload_static.php index 25b67cae98869..8e4b32c5f77f1 100644 --- a/apps/admin_audit/composer/composer/autoload_static.php +++ b/apps/admin_audit/composer/composer/autoload_static.php @@ -25,7 +25,6 @@ class ComposerStaticInitAdminAudit 'OCA\\AdminAudit\\Actions\\Action' => __DIR__ . '/..' . '/../lib/Actions/Action.php', 'OCA\\AdminAudit\\Actions\\Files' => __DIR__ . '/..' . '/../lib/Actions/Files.php', 'OCA\\AdminAudit\\Actions\\Sharing' => __DIR__ . '/..' . '/../lib/Actions/Sharing.php', - 'OCA\\AdminAudit\\Actions\\Trashbin' => __DIR__ . '/..' . '/../lib/Actions/Trashbin.php', 'OCA\\AdminAudit\\Actions\\Versions' => __DIR__ . '/..' . '/../lib/Actions/Versions.php', 'OCA\\AdminAudit\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php', 'OCA\\AdminAudit\\AuditLogger' => __DIR__ . '/..' . '/../lib/AuditLogger.php', @@ -41,6 +40,7 @@ class ComposerStaticInitAdminAudit 'OCA\\AdminAudit\\Listener\\SecurityEventListener' => __DIR__ . '/..' . '/../lib/Listener/SecurityEventListener.php', 'OCA\\AdminAudit\\Listener\\SharingEventListener' => __DIR__ . '/..' . '/../lib/Listener/SharingEventListener.php', 'OCA\\AdminAudit\\Listener\\TagEventListener' => __DIR__ . '/..' . '/../lib/Listener/TagEventListener.php', + 'OCA\\AdminAudit\\Listener\\TrashbinEventListener' => __DIR__ . '/..' . '/../lib/Listener/TrashbinEventListener.php', 'OCA\\AdminAudit\\Listener\\UserManagementEventListener' => __DIR__ . '/..' . '/../lib/Listener/UserManagementEventListener.php', 'OCA\\AdminAudit\\Operation' => __DIR__ . '/..' . '/../lib/Operation.php', ); diff --git a/apps/admin_audit/lib/Actions/Trashbin.php b/apps/admin_audit/lib/Actions/Trashbin.php deleted file mode 100644 index 83463a1624f70..0000000000000 --- a/apps/admin_audit/lib/Actions/Trashbin.php +++ /dev/null @@ -1,25 +0,0 @@ -log(Operation::TrashbinFileDeleted, 'File "%s" deleted from trash bin.', - ['path' => $params['path']], ['path'] - ); - } - - public function restore(array $params): void { - $this->log(Operation::TrashbinFileRestored, 'File "%s" restored from trash bin.', - ['path' => $params['filePath']], ['path'] - ); - } -} diff --git a/apps/admin_audit/lib/AppInfo/Application.php b/apps/admin_audit/lib/AppInfo/Application.php index 3c331dac5a4c3..68cd0c84c5ca9 100644 --- a/apps/admin_audit/lib/AppInfo/Application.php +++ b/apps/admin_audit/lib/AppInfo/Application.php @@ -11,7 +11,6 @@ use OCA\AdminAudit\Actions\Files; use OCA\AdminAudit\Actions\Sharing; -use OCA\AdminAudit\Actions\Trashbin; use OCA\AdminAudit\Actions\Versions; use OCA\AdminAudit\AuditLogger; use OCA\AdminAudit\IAuditLogger; @@ -25,7 +24,10 @@ use OCA\AdminAudit\Listener\SecurityEventListener; use OCA\AdminAudit\Listener\SharingEventListener; use OCA\AdminAudit\Listener\TagEventListener; +use OCA\AdminAudit\Listener\TrashbinEventListener; use OCA\AdminAudit\Listener\UserManagementEventListener; +use OCA\Files_Trashbin\Events\BeforeNodeDeletedEvent as TrashbinBeforeNodeDeletedEvent; +use OCA\Files_Trashbin\Events\NodeRestoredEvent; use OCA\Files_Versions\Events\VersionRestoredEvent; use OCP\App\Events\AppDisableEvent; use OCP\App\Events\AppEnableEvent; @@ -156,6 +158,10 @@ public function register(IRegistrationContext $context): void { // Files sharing recipient events $context->registerEventListener(ShareAcceptedEvent::class, SharingEventListener::class); $context->registerEventListener(ShareDeletedFromSelfEvent::class, SharingEventListener::class); + + // Trashbin events + $context->registerEventListener(TrashbinBeforeNodeDeletedEvent::class, TrashbinEventListener::class); + $context->registerEventListener(NodeRestoredEvent::class, TrashbinEventListener::class); } #[\Override] @@ -178,7 +184,6 @@ private function registerLegacyHooks(IAuditLogger $logger, ContainerInterface $s $eventDispatcher = $serverContainer->get(IEventDispatcher::class); $this->sharingLegacyHooks($logger); $this->fileHooks($logger, $eventDispatcher); - $this->trashbinHooks($logger); $this->versionsHooks($logger); } @@ -241,10 +246,4 @@ private function versionsHooks(IAuditLogger $logger): void { $versionsActions = new Versions($logger); Util::connectHook('\OCP\Versions', 'delete', $versionsActions, 'delete'); } - - private function trashbinHooks(IAuditLogger $logger): void { - $trashActions = new Trashbin($logger); - Util::connectHook('\OCP\Trashbin', 'preDelete', $trashActions, 'delete'); - Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', $trashActions, 'restore'); - } } diff --git a/apps/admin_audit/lib/Listener/TrashbinEventListener.php b/apps/admin_audit/lib/Listener/TrashbinEventListener.php new file mode 100644 index 0000000000000..1e75acbb4220b --- /dev/null +++ b/apps/admin_audit/lib/Listener/TrashbinEventListener.php @@ -0,0 +1,37 @@ + + */ +class TrashbinEventListener extends Action implements IEventListener { + + #[Override] + public function handle(Event $event): void { + if ($event instanceof BeforeNodeDeletedEvent) { + $this->log(Operation::TrashbinFileDeleted, 'File "%s" deleted from trash bin.', + ['path' => $event->getSource()->getPath()], ['path'] + ); + } elseif ($event instanceof NodeRestoredEvent) { + $this->log(Operation::TrashbinFileRestored, 'File "%s" restored from trash bin.', + ['path' => $event->getTarget()->getPath()], ['path'] + ); + } + } +} diff --git a/apps/admin_audit/tests/Actions/TrashbinTest.php b/apps/admin_audit/tests/Actions/TrashbinTest.php deleted file mode 100644 index 6b9a4fa60ab72..0000000000000 --- a/apps/admin_audit/tests/Actions/TrashbinTest.php +++ /dev/null @@ -1,43 +0,0 @@ -logger = $this->createMock(IAuditLogger::class); - $this->trashbin = new Trashbin($this->logger); - } - - public function testDelete(): void { - $this->logger->expects($this->once()) - ->method('info') - ->with('File "/a.txt.d1700000000" deleted from trash bin.', ['app' => 'admin_audit', 'operation' => 'trashbin.file.deleted', 'params' => ['path' => '/a.txt.d1700000000']]); - - $this->trashbin->delete(['path' => '/a.txt.d1700000000']); - } - - public function testRestore(): void { - $this->logger->expects($this->once()) - ->method('info') - ->with('File "/a.txt" restored from trash bin.', ['app' => 'admin_audit', 'operation' => 'trashbin.file.restored', 'params' => ['path' => '/a.txt']]); - - $this->trashbin->restore(['filePath' => '/a.txt', 'trashPath' => '/a.txt.d1700000000']); - } -} diff --git a/apps/admin_audit/tests/Listener/TrashbinEventListenerTest.php b/apps/admin_audit/tests/Listener/TrashbinEventListenerTest.php new file mode 100644 index 0000000000000..9a168933453da --- /dev/null +++ b/apps/admin_audit/tests/Listener/TrashbinEventListenerTest.php @@ -0,0 +1,64 @@ +logger = $this->createMock(IAuditLogger::class); + $this->listener = new TrashbinEventListener($this->logger); + } + + private function createNode(string $path): Node&MockObject { + $node = $this->createMock(Node::class); + $node->method('getPath')->willReturn($path); + return $node; + } + + private function expectInfo(string $message, string $operation, array $params): void { + $this->logger->expects($this->once()) + ->method('info') + ->with($message, ['app' => 'admin_audit', 'operation' => $operation, 'params' => $params]); + } + + public function testDelete(): void { + $path = '/alice/files_trashbin/files/a.txt.d1700000000'; + $this->expectInfo('File "' . $path . '" deleted from trash bin.', 'trashbin.file.deleted', ['path' => $path]); + + $this->listener->handle(new BeforeNodeDeletedEvent($this->createNode($path))); + } + + public function testRestore(): void { + $source = $this->createNode('/alice/files_trashbin/files/a.txt.d1700000000'); + $target = $this->createNode('/alice/files/a.txt'); + $this->expectInfo('File "/alice/files/a.txt" restored from trash bin.', 'trashbin.file.restored', ['path' => '/alice/files/a.txt']); + + $this->listener->handle(new NodeRestoredEvent($source, $target)); + } + + public function testUnrelatedEventIsIgnored(): void { + $this->logger->expects($this->never())->method('info'); + + $this->listener->handle(new Event()); + } +} diff --git a/apps/files_trashbin/composer/composer/autoload_classmap.php b/apps/files_trashbin/composer/composer/autoload_classmap.php index 23e2e7baff64c..af10eb102c612 100644 --- a/apps/files_trashbin/composer/composer/autoload_classmap.php +++ b/apps/files_trashbin/composer/composer/autoload_classmap.php @@ -16,8 +16,10 @@ 'OCA\\Files_Trashbin\\Command\\RestoreAllFiles' => $baseDir . '/../lib/Command/RestoreAllFiles.php', 'OCA\\Files_Trashbin\\Command\\Size' => $baseDir . '/../lib/Command/Size.php', 'OCA\\Files_Trashbin\\Controller\\PreviewController' => $baseDir . '/../lib/Controller/PreviewController.php', + 'OCA\\Files_Trashbin\\Events\\BeforeNodeDeletedEvent' => $baseDir . '/../lib/Events/BeforeNodeDeletedEvent.php', 'OCA\\Files_Trashbin\\Events\\BeforeNodeRestoredEvent' => $baseDir . '/../lib/Events/BeforeNodeRestoredEvent.php', 'OCA\\Files_Trashbin\\Events\\MoveToTrashEvent' => $baseDir . '/../lib/Events/MoveToTrashEvent.php', + 'OCA\\Files_Trashbin\\Events\\NodeDeletedEvent' => $baseDir . '/../lib/Events/NodeDeletedEvent.php', 'OCA\\Files_Trashbin\\Events\\NodeRestoredEvent' => $baseDir . '/../lib/Events/NodeRestoredEvent.php', 'OCA\\Files_Trashbin\\Exceptions\\CopyRecursiveException' => $baseDir . '/../lib/Exceptions/CopyRecursiveException.php', 'OCA\\Files_Trashbin\\Expiration' => $baseDir . '/../lib/Expiration.php', diff --git a/apps/files_trashbin/composer/composer/autoload_static.php b/apps/files_trashbin/composer/composer/autoload_static.php index 307f813aadb6b..4f9d1dc87226b 100644 --- a/apps/files_trashbin/composer/composer/autoload_static.php +++ b/apps/files_trashbin/composer/composer/autoload_static.php @@ -31,8 +31,10 @@ class ComposerStaticInitFiles_Trashbin 'OCA\\Files_Trashbin\\Command\\RestoreAllFiles' => __DIR__ . '/..' . '/../lib/Command/RestoreAllFiles.php', 'OCA\\Files_Trashbin\\Command\\Size' => __DIR__ . '/..' . '/../lib/Command/Size.php', 'OCA\\Files_Trashbin\\Controller\\PreviewController' => __DIR__ . '/..' . '/../lib/Controller/PreviewController.php', + 'OCA\\Files_Trashbin\\Events\\BeforeNodeDeletedEvent' => __DIR__ . '/..' . '/../lib/Events/BeforeNodeDeletedEvent.php', 'OCA\\Files_Trashbin\\Events\\BeforeNodeRestoredEvent' => __DIR__ . '/..' . '/../lib/Events/BeforeNodeRestoredEvent.php', 'OCA\\Files_Trashbin\\Events\\MoveToTrashEvent' => __DIR__ . '/..' . '/../lib/Events/MoveToTrashEvent.php', + 'OCA\\Files_Trashbin\\Events\\NodeDeletedEvent' => __DIR__ . '/..' . '/../lib/Events/NodeDeletedEvent.php', 'OCA\\Files_Trashbin\\Events\\NodeRestoredEvent' => __DIR__ . '/..' . '/../lib/Events/NodeRestoredEvent.php', 'OCA\\Files_Trashbin\\Exceptions\\CopyRecursiveException' => __DIR__ . '/..' . '/../lib/Exceptions/CopyRecursiveException.php', 'OCA\\Files_Trashbin\\Expiration' => __DIR__ . '/..' . '/../lib/Expiration.php', diff --git a/apps/files_trashbin/lib/Events/BeforeNodeDeletedEvent.php b/apps/files_trashbin/lib/Events/BeforeNodeDeletedEvent.php new file mode 100644 index 0000000000000..8bddb36eaac47 --- /dev/null +++ b/apps/files_trashbin/lib/Events/BeforeNodeDeletedEvent.php @@ -0,0 +1,30 @@ +source; + } +} diff --git a/apps/files_trashbin/lib/Events/NodeDeletedEvent.php b/apps/files_trashbin/lib/Events/NodeDeletedEvent.php new file mode 100644 index 0000000000000..5a60183df4d06 --- /dev/null +++ b/apps/files_trashbin/lib/Events/NodeDeletedEvent.php @@ -0,0 +1,29 @@ +sourcePath; + } +} diff --git a/apps/files_trashbin/lib/Sabre/TrashRoot.php b/apps/files_trashbin/lib/Sabre/TrashRoot.php index 0d124c3d6eb1a..1c66abf0b5e82 100644 --- a/apps/files_trashbin/lib/Sabre/TrashRoot.php +++ b/apps/files_trashbin/lib/Sabre/TrashRoot.php @@ -22,7 +22,6 @@ use Sabre\DAV\INode; class TrashRoot implements ICollection { - public function __construct( private readonly IUser $user, private readonly ITrashManager $trashManager, @@ -35,7 +34,7 @@ public function delete() { throw new Forbidden('Not allowed to delete items from the trash bin'); } - Trashbin::deleteAll(); + Trashbin::deleteAll($this->user); foreach ($this->trashManager->listTrashRoot($this->user) as $trashItem) { $this->trashManager->removeItem($trashItem); } diff --git a/apps/files_trashbin/lib/Trashbin.php b/apps/files_trashbin/lib/Trashbin.php index 456fc1f804ecb..c8684ae33832a 100644 --- a/apps/files_trashbin/lib/Trashbin.php +++ b/apps/files_trashbin/lib/Trashbin.php @@ -43,7 +43,6 @@ use OCP\Files\Storage\IStorage; use OCP\FilesMetadata\IFilesMetadataManager; use OCP\IAppConfig; -use OCP\IConfig; use OCP\IDBConnection; use OCP\IRequest; use OCP\IURLGenerator; @@ -631,7 +630,7 @@ public static function restore($file, $filename, $timestamp) { * @param string $uniqueFilename new file name to restore the file without overwriting existing files * @param string $location location if file * @param int $timestamp deletion time - * @return false|null + * @return bool|null */ private static function restoreVersions(View $view, $file, $filename, $uniqueFilename, $location, $timestamp) { if (Server::get(IAppManager::class)->isEnabledForUser('files_versions')) { @@ -664,53 +663,50 @@ private static function restoreVersions(View $view, $file, $filename, $uniqueFil } } } + return true; } + return null; } /** * delete all files from the trash */ - public static function deleteAll() { - $user = self::getUser(); - $userRoot = Server::get(IRootFolder::class)->getUserFolder($user)->getParent(); - $view = new View('/' . $user); - $fileInfos = $view->getDirectoryContent('files_trashbin/files'); + public static function deleteAll(IUser $user): bool { + $rootFolder = Server::get(IRootFolder::class); + $dispatcher = Server::get(IEventDispatcher::class); + $dbConnection = Server::get(IDBConnection::class); + + $userRoot = $rootFolder->getUserFolder($user->getUID())->getParent(); try { - $trash = $userRoot->get('files_trashbin'); - } catch (NotFoundException $e) { + /** @var Folder $trashRoot */ + $trashRoot = $userRoot->get('files_trashbin'); + /** @var Folder $trashFilesRoot */ + $trashFilesRoot = $trashRoot->get('files'); + } catch (NotFoundException|NotPermittedException) { return false; } - // Array to store the relative path in (after the file is deleted, the view won't be able to relativise the path anymore) - $filePaths = []; - foreach ($fileInfos as $fileInfo) { - $filePaths[] = $view->getRelativePath($fileInfo->getPath()); - } - unset($fileInfos); // save memory - - // Bulk PreDelete-Hook - \OC_Hook::emit('\OCP\Trashbin', 'preDeleteAll', ['paths' => $filePaths]); + $trashNodes = $trashFilesRoot->getDirectoryListing(); // Single-File Hooks - foreach ($filePaths as $path) { - self::emitTrashbinPreDelete($path); + foreach ($trashNodes as $trashNode) { + $event = new Events\BeforeNodeDeletedEvent($trashNode); + $dispatcher->dispatchTyped($event); } // actual file deletion - $trash->delete(); + $trashRoot->delete(); - $query = Server::get(IDBConnection::class)->getQueryBuilder(); + $query = $dbConnection->getQueryBuilder(); $query->delete('files_trash') - ->where($query->expr()->eq('user', $query->createNamedParameter($user))); + ->where($query->expr()->eq('user', $query->createNamedParameter($user->getUID()))); $query->executeStatement(); - // Bulk PostDelete-Hook - \OC_Hook::emit('\OCP\Trashbin', 'deleteAll', ['paths' => $filePaths]); - // Single-File Hooks - foreach ($filePaths as $path) { - self::emitTrashbinPostDelete($path); + foreach ($trashNodes as $trashNode) { + $event = new Events\NodeDeletedEvent($trashNode->getPath()); + $dispatcher->dispatchTyped($event); } $trash = $userRoot->newFolder('files_trashbin'); @@ -720,35 +716,26 @@ public static function deleteAll() { } /** - * wrapper function to emit the 'preDelete' hook of \OCP\Trashbin before a file is deleted - * - * @param string $path - */ - protected static function emitTrashbinPreDelete($path) { - \OC_Hook::emit('\OCP\Trashbin', 'preDelete', ['path' => $path]); - } - - /** - * wrapper function to emit the 'delete' hook of \OCP\Trashbin after a file has been deleted - * - * @param string $path - */ - protected static function emitTrashbinPostDelete($path) { - \OC_Hook::emit('\OCP\Trashbin', 'delete', ['path' => $path]); - } - - /** - * delete file from trash bin permanently + * Delete file from trash bin permanently * * @param string $filename path to the file - * @param string $user - * @param int $timestamp of deletion time - * + * @param ?int $timestamp of deletion time * @return int|float size of deleted files */ - public static function delete($filename, $user, $timestamp = null) { - $userRoot = Server::get(IRootFolder::class)->getUserFolder($user)->getParent(); - $view = new View('/' . $user); + public static function delete(string $filename, string $user, ?int $timestamp = null): int|float { + $rootFolder = Server::get(IRootFolder::class); + $appManager = Server::get(IAppManager::class); + $dispatcher = Server::get(IEventDispatcher::class); + + $userRoot = $rootFolder->getUserFolder($user)->getParent(); + + try { + /** @var Folder $trashRoot */ + $trashRoot = $userRoot->get('files_trashbin'); + } catch (NotFoundException|NotPermittedException) { + return 0; + } + $size = 0; if ($timestamp) { @@ -757,7 +744,9 @@ public static function delete($filename, $user, $timestamp = null) { $file = $filename; } - $size += self::deleteVersions($view, $file, $filename, $timestamp, $user); + if ($appManager->isEnabledForUser('files_versions')) { + $size += Trashbin::deleteVersions($trashRoot, $file, $filename, $timestamp, $user); + } try { $node = $userRoot->get('/files_trashbin/files/' . $file); @@ -768,15 +757,15 @@ public static function delete($filename, $user, $timestamp = null) { return $size; } - if ($node instanceof Folder) { - $size += self::calculateSize(new View('/' . $user . '/files_trashbin/files/' . $file)); - } elseif ($node instanceof File) { - $size += $view->filesize('/files_trashbin/files/' . $file); - } + $size += $node->getSize(); + + $event = new Events\BeforeNodeDeletedEvent($node); + $dispatcher->dispatchTyped($event); - self::emitTrashbinPreDelete('/files_trashbin/files/' . $file); $node->delete(); - self::emitTrashbinPostDelete('/files_trashbin/files/' . $file); + + $event = new Events\NodeDeletedEvent($node->getPath()); + $dispatcher->dispatchTyped($event); if ($timestamp) { self::deleteTrashRow($user, $filename, $timestamp); @@ -795,26 +784,33 @@ private static function deleteTrashRow(string $user, string $filename, int $time } /** - * @param string $file - * @param string $filename - * @param ?int $timestamp + * Delete version files corresponding to a given file from trash bin permanently. */ - private static function deleteVersions(View $view, $file, $filename, $timestamp, string $user): int|float { + private static function deleteVersions(Folder $trashRoot, string $file, string $filename, ?int $timestamp, string $user): int|float { + if (!Server::get(IAppManager::class)->isEnabledForUser('files_versions')) { + return 0; + } + $size = 0; - if (Server::get(IAppManager::class)->isEnabledForUser('files_versions')) { - if ($view->is_dir('files_trashbin/versions/' . $file)) { - $size += self::calculateSize(new View('/' . $user . '/files_trashbin/versions/' . $file)); - $view->unlink('files_trashbin/versions/' . $file); - } elseif ($versions = self::getVersionsFromTrash($filename, $timestamp, $user)) { - foreach ($versions as $v) { - if ($timestamp) { - $size += $view->filesize('/files_trashbin/versions/' . static::getTrashFilename($filename . '.v' . $v, $timestamp)); - $view->unlink('/files_trashbin/versions/' . static::getTrashFilename($filename . '.v' . $v, $timestamp)); - } else { - $size += $view->filesize('/files_trashbin/versions/' . $filename . '.v' . $v); - $view->unlink('/files_trashbin/versions/' . $filename . '.v' . $v); - } + try { + $fileVersion = $trashRoot->get('versions/' . $file); + } catch (NotFoundException) { + $fileVersion = null; + } + + if ($fileVersion) { + $size += $fileVersion->getSize(); + $fileVersion->delete(); + } elseif ($versions = self::getVersionsFromTrash($filename, $timestamp, $user)) { + foreach ($versions as $v) { + if ($timestamp) { + $node = $trashRoot->get('/files_trashbin/versions/' . static::getTrashFilename($filename . '.v' . $v, $timestamp)); + } else { + $node = $trashRoot->get('/files_trashbin/versions/' . $filename . '.v' . $v); } + + $size += $node->getSize(); + $node->delete(); } } return $size; @@ -1170,37 +1166,6 @@ private static function getUniqueFilename($location, $filename, View $view) { return $filename; } - /** - * get the size from a given root folder - * - * @param View $view file view on the root folder - * @return int|float size of the folder - */ - private static function calculateSize(View $view): int|float { - $root = Server::get(IConfig::class)->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . $view->getAbsolutePath(''); - if (!file_exists($root)) { - return 0; - } - $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($root), \RecursiveIteratorIterator::CHILD_FIRST); - $size = 0; - - /** - * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach - * This bug is fixed in PHP 5.5.9 or before - * See #8376 - */ - $iterator->rewind(); - while ($iterator->valid()) { - $path = $iterator->current(); - $relpath = substr($path, strlen($root) - 1); - if (!$view->is_dir($relpath)) { - $size += $view->filesize($relpath); - } - $iterator->next(); - } - return $size; - } - /** * get current size of trash bin from a given user * diff --git a/apps/files_trashbin/tests/TrashbinTest.php b/apps/files_trashbin/tests/TrashbinTest.php index dec8c335d15f3..8e608f227ed0e 100644 --- a/apps/files_trashbin/tests/TrashbinTest.php +++ b/apps/files_trashbin/tests/TrashbinTest.php @@ -689,6 +689,27 @@ public function testTrashSizePropagation(): void { $this->assertEquals(3, $view->getFileInfo('')->getSize()); } + public function testDeleteReturnsSizeOfDeletedNode(): void { + $userFolder = Server::get(IRootFolder::class)->getUserFolder(self::TEST_TRASHBIN_USER1); + $userFolder->newFile('file.txt', '1'); + $folder = $userFolder->newFolder('folder'); + $folder->newFile('a.txt', '22'); + $folder->newFolder('sub')->newFile('b.txt', '333'); + + $userFolder->get('file.txt')->delete(); + $folder->delete(); + + $filesInTrash = Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name'); + $this->assertCount(2, $filesInTrash); + $sizes = []; + foreach ($filesInTrash as $file) { + $sizes[$file['name']] = Trashbin::delete($file['name'], self::TEST_TRASHBIN_USER1, $file['mtime']); + } + + $this->assertEquals(['file.txt' => 1, 'folder' => 5], $sizes); + $this->assertCount(0, Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1)); + } + /** * @param string $user * @param bool $create diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index 35edea86710aa..5c19fe7861f9e 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -8,8 +8,6 @@ - - @@ -1775,11 +1773,7 @@ - - - - @@ -1805,18 +1799,11 @@ - - - - - - - @@ -1830,8 +1817,6 @@ - - @@ -1840,11 +1825,7 @@ - - - - @@ -1867,9 +1848,6 @@ - - -