diff options
Diffstat (limited to 'core/Command/Info/File.php')
-rw-r--r-- | core/Command/Info/File.php | 136 |
1 files changed, 7 insertions, 129 deletions
diff --git a/core/Command/Info/File.php b/core/Command/Info/File.php index bf7b9ae4e0a..8ca7a3d0264 100644 --- a/core/Command/Info/File.php +++ b/core/Command/Info/File.php @@ -5,13 +5,9 @@ declare(strict_types=1); namespace OC\Core\Command\Info; use OC\Files\ObjectStore\ObjectStoreStorage; -use OCA\Circles\MountManager\CircleMount; use OCA\Files_External\Config\ExternalMountPoint; -use OCA\Files_Sharing\SharedMount; use OCA\GroupFolders\Mount\GroupMountPoint; -use OCP\Constants; use OCP\Files\Config\IUserMountCache; -use OCP\Files\FileInfo; use OCP\Files\Folder; use OCP\Files\IHomeStorage; use OCP\Files\IRootFolder; @@ -20,7 +16,6 @@ use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\IL10N; use OCP\L10N\IFactory; -use OCP\Share\IShare; use OCP\Util; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -29,14 +24,12 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class File extends Command { - private IRootFolder $rootFolder; - private IUserMountCache $userMountCache; private IL10N $l10n; + private FileUtils $fileUtils; - public function __construct(IRootFolder $rootFolder, IUserMountCache $userMountCache, IFactory $l10nFactory) { - $this->rootFolder = $rootFolder; - $this->userMountCache = $userMountCache; + public function __construct(IFactory $l10nFactory, FileUtils $fileUtils) { $this->l10n = $l10nFactory->get("core"); + $this->fileUtils = $fileUtils; parent::__construct(); } @@ -51,7 +44,7 @@ class File extends Command { public function execute(InputInterface $input, OutputInterface $output): int { $fileInput = $input->getArgument('file'); $showChildren = $input->getOption('children'); - $node = $this->getNode($fileInput); + $node = $this->fileUtils->getNode($fileInput); if (!$node) { $output->writeln("<error>file $fileInput not found</error>"); return 1; @@ -83,137 +76,22 @@ class File extends Command { } $this->outputStorageDetails($node->getMountPoint(), $node, $output); - $filesPerUser = $this->getFilesByUser($node); + $filesPerUser = $this->fileUtils->getFilesByUser($node); $output->writeln(""); $output->writeln("The following users have access to the file"); $output->writeln(""); foreach ($filesPerUser as $user => $files) { $output->writeln("$user:"); foreach ($files as $userFile) { - $output->writeln(" " . $userFile->getPath() . ": " . $this->formatPermissions($userFile->getType(), $userFile->getPermissions())); + $output->writeln(" " . $userFile->getPath() . ": " . $this->fileUtils->formatPermissions($userFile->getType(), $userFile->getPermissions())); $mount = $userFile->getMountPoint(); - $output->writeln(" " . $this->formatMountType($mount)); + $output->writeln(" " . $this->fileUtils->formatMountType($mount)); } } return 0; } - private function getNode(string $fileInput): ?Node { - if (is_numeric($fileInput)) { - $mounts = $this->userMountCache->getMountsForFileId((int)$fileInput); - if (!$mounts) { - return null; - } - $mount = $mounts[0]; - $userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID()); - $nodes = $userFolder->getById((int)$fileInput); - if (!$nodes) { - return null; - } - return $nodes[0]; - } else { - try { - return $this->rootFolder->get($fileInput); - } catch (NotFoundException $e) { - return null; - } - } - } - - /** - * @param FileInfo $file - * @return array<string, Node[]> - * @throws \OCP\Files\NotPermittedException - * @throws \OC\User\NoUserException - */ - private function getFilesByUser(FileInfo $file): array { - $id = $file->getId(); - if (!$id) { - return []; - } - - $mounts = $this->userMountCache->getMountsForFileId($id); - $result = []; - foreach ($mounts as $mount) { - if (isset($result[$mount->getUser()->getUID()])) { - continue; - } - - $userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID()); - $result[$mount->getUser()->getUID()] = $userFolder->getById($id); - } - - return $result; - } - - private function formatPermissions(string $type, int $permissions): string { - if ($permissions == Constants::PERMISSION_ALL || ($type === 'file' && $permissions == (Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE))) { - return "full permissions"; - } - - $perms = []; - $allPerms = [Constants::PERMISSION_READ => "read", Constants::PERMISSION_UPDATE => "update", Constants::PERMISSION_CREATE => "create", Constants::PERMISSION_DELETE => "delete", Constants::PERMISSION_SHARE => "share"]; - foreach ($allPerms as $perm => $name) { - if (($permissions & $perm) === $perm) { - $perms[] = $name; - } - } - - return implode(", ", $perms); - } - - /** - * @psalm-suppress UndefinedClass - * @psalm-suppress UndefinedInterfaceMethod - */ - private function formatMountType(IMountPoint $mountPoint): string { - $storage = $mountPoint->getStorage(); - if ($storage && $storage->instanceOfStorage(IHomeStorage::class)) { - return "home storage"; - } elseif ($mountPoint instanceof SharedMount) { - $share = $mountPoint->getShare(); - $shares = $mountPoint->getGroupedShares(); - $sharedBy = array_map(function (IShare $share) { - $shareType = $this->formatShareType($share); - if ($shareType) { - return $share->getSharedBy() . " (via " . $shareType . " " . $share->getSharedWith() . ")"; - } else { - return $share->getSharedBy(); - } - }, $shares); - $description = "shared by " . implode(', ', $sharedBy); - if ($share->getSharedBy() !== $share->getShareOwner()) { - $description .= " owned by " . $share->getShareOwner(); - } - return $description; - } elseif ($mountPoint instanceof GroupMountPoint) { - return "groupfolder " . $mountPoint->getFolderId(); - } elseif ($mountPoint instanceof ExternalMountPoint) { - return "external storage " . $mountPoint->getStorageConfig()->getId(); - } elseif ($mountPoint instanceof CircleMount) { - return "circle"; - } - return get_class($mountPoint); - } - - private function formatShareType(IShare $share): ?string { - switch ($share->getShareType()) { - case IShare::TYPE_GROUP: - return "group"; - case IShare::TYPE_CIRCLE: - return "circle"; - case IShare::TYPE_DECK: - return "deck"; - case IShare::TYPE_ROOM: - return "room"; - case IShare::TYPE_USER: - return null; - default: - return "Unknown (".$share->getShareType().")"; - } - } - /** * @psalm-suppress UndefinedClass * @psalm-suppress UndefinedInterfaceMethod |