diff options
Diffstat (limited to 'core/Command/Info/FileUtils.php')
-rw-r--r-- | core/Command/Info/FileUtils.php | 155 |
1 files changed, 129 insertions, 26 deletions
diff --git a/core/Command/Info/FileUtils.php b/core/Command/Info/FileUtils.php index 6c490d31c3d..bc07535a289 100644 --- a/core/Command/Info/FileUtils.php +++ b/core/Command/Info/FileUtils.php @@ -8,11 +8,13 @@ declare(strict_types=1); namespace OC\Core\Command\Info; +use OC\User\NoUserException; 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\DB\QueryBuilder\IQueryBuilder; use OCP\Files\Config\IUserMountCache; use OCP\Files\FileInfo; use OCP\Files\Folder; @@ -21,22 +23,28 @@ use OCP\Files\IRootFolder; use OCP\Files\Mount\IMountPoint; use OCP\Files\Node; use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\IDBConnection; use OCP\Share\IShare; use OCP\Util; use Symfony\Component\Console\Output\OutputInterface; +/** + * @psalm-type StorageInfo array{numeric_id: int, id: string, available: bool, last_checked: ?\DateTime, files: int, mount_id: ?int} + */ class FileUtils { public function __construct( private IRootFolder $rootFolder, private IUserMountCache $userMountCache, + private IDBConnection $connection, ) { } /** * @param FileInfo $file * @return array<string, Node[]> - * @throws \OCP\Files\NotPermittedException - * @throws \OC\User\NoUserException + * @throws NotPermittedException + * @throws NoUserException */ public function getFilesByUser(FileInfo $file): array { $id = $file->getId(); @@ -46,13 +54,12 @@ class FileUtils { $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); + foreach ($mounts as $cachedMount) { + $mount = $this->rootFolder->getMount($cachedMount->getMountPoint()); + $cache = $mount->getStorage()->getCache(); + $cacheEntry = $cache->get($id); + $node = $this->rootFolder->getNodeFromCacheEntryAndMount($cacheEntry, $mount); + $result[$cachedMount->getUser()->getUID()][] = $node; } return $result; @@ -84,18 +91,18 @@ class FileUtils { public function formatPermissions(string $type, int $permissions): string { if ($permissions == Constants::PERMISSION_ALL || ($type === 'file' && $permissions == (Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE))) { - return "full permissions"; + 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"]; + $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); + return implode(', ', $perms); } /** @@ -105,29 +112,29 @@ class FileUtils { public function formatMountType(IMountPoint $mountPoint): string { $storage = $mountPoint->getStorage(); if ($storage && $storage->instanceOfStorage(IHomeStorage::class)) { - return "home storage"; + 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() . ")"; + return $share->getSharedBy() . ' (via ' . $shareType . ' ' . $share->getSharedWith() . ')'; } else { return $share->getSharedBy(); } }, $shares); - $description = "shared by " . implode(', ', $sharedBy); + $description = 'shared by ' . implode(', ', $sharedBy); if ($share->getSharedBy() !== $share->getShareOwner()) { - $description .= " owned by " . $share->getShareOwner(); + $description .= ' owned by ' . $share->getShareOwner(); } return $description; } elseif ($mountPoint instanceof GroupMountPoint) { - return "groupfolder " . $mountPoint->getFolderId(); + return 'groupfolder ' . $mountPoint->getFolderId(); } elseif ($mountPoint instanceof ExternalMountPoint) { - return "external storage " . $mountPoint->getStorageConfig()->getId(); + return 'external storage ' . $mountPoint->getStorageConfig()->getId(); } elseif ($mountPoint instanceof CircleMount) { - return "circle"; + return 'circle'; } return get_class($mountPoint); } @@ -135,17 +142,17 @@ class FileUtils { public function formatShareType(IShare $share): ?string { switch ($share->getShareType()) { case IShare::TYPE_GROUP: - return "group"; + return 'group'; case IShare::TYPE_CIRCLE: - return "circle"; + return 'circle'; case IShare::TYPE_DECK: - return "deck"; + return 'deck'; case IShare::TYPE_ROOM: - return "room"; + return 'room'; case IShare::TYPE_USER: return null; default: - return "Unknown (" . $share->getShareType() . ")"; + return 'Unknown (' . $share->getShareType() . ')'; } } @@ -197,7 +204,7 @@ class FileUtils { $count += 1; /** @var Node $child */ - $output->writeln("$prefix- " . $child->getName() . ": <info>" . Util::humanFileSize($child->getSize()) . "</info>"); + $output->writeln("$prefix- " . $child->getName() . ': <info>' . Util::humanFileSize($child->getSize()) . '</info>'); if ($child instanceof Folder) { $recurseSizeLimits = $sizeLimits; if (!$all) { @@ -212,11 +219,107 @@ class FileUtils { } sort($recurseSizeLimits); } - $recurseCount = $this->outputLargeFilesTree($output, $child, $prefix . " ", $recurseSizeLimits, $all); + $recurseCount = $this->outputLargeFilesTree($output, $child, $prefix . ' ', $recurseSizeLimits, $all); $sizeLimits = array_slice($sizeLimits, $recurseCount); $count += $recurseCount; } } return $count; } + + public function getNumericStorageId(string $id): ?int { + if (is_numeric($id)) { + return (int)$id; + } + $query = $this->connection->getQueryBuilder(); + $query->select('numeric_id') + ->from('storages') + ->where($query->expr()->eq('id', $query->createNamedParameter($id))); + $result = $query->executeQuery()->fetchOne(); + return $result ? (int)$result : null; + } + + /** + * @param int|null $limit + * @return ?StorageInfo + * @throws \OCP\DB\Exception + */ + public function getStorage(int $id): ?array { + $query = $this->connection->getQueryBuilder(); + $query->select('numeric_id', 's.id', 'available', 'last_checked', 'mount_id') + ->selectAlias($query->func()->count('fileid'), 'files') + ->from('storages', 's') + ->innerJoin('s', 'filecache', 'f', $query->expr()->eq('f.storage', 's.numeric_id')) + ->leftJoin('s', 'mounts', 'm', $query->expr()->eq('s.numeric_id', 'm.storage_id')) + ->where($query->expr()->eq('s.numeric_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))) + ->groupBy('s.numeric_id', 's.id', 's.available', 's.last_checked', 'mount_id'); + $row = $query->executeQuery()->fetch(); + if ($row) { + return [ + 'numeric_id' => $row['numeric_id'], + 'id' => $row['id'], + 'files' => $row['files'], + 'available' => (bool)$row['available'], + 'last_checked' => $row['last_checked'] ? new \DateTime('@' . $row['last_checked']) : null, + 'mount_id' => $row['mount_id'], + ]; + } else { + return null; + } + } + + /** + * @param int|null $limit + * @return \Iterator<StorageInfo> + * @throws \OCP\DB\Exception + */ + public function listStorages(?int $limit): \Iterator { + $query = $this->connection->getQueryBuilder(); + $query->select('numeric_id', 's.id', 'available', 'last_checked', 'mount_id') + ->selectAlias($query->func()->count('fileid'), 'files') + ->from('storages', 's') + ->innerJoin('s', 'filecache', 'f', $query->expr()->eq('f.storage', 's.numeric_id')) + ->leftJoin('s', 'mounts', 'm', $query->expr()->eq('s.numeric_id', 'm.storage_id')) + ->groupBy('s.numeric_id', 's.id', 's.available', 's.last_checked', 'mount_id') + ->orderBy('files', 'DESC'); + if ($limit !== null) { + $query->setMaxResults($limit); + } + $result = $query->executeQuery(); + while ($row = $result->fetch()) { + yield [ + 'numeric_id' => $row['numeric_id'], + 'id' => $row['id'], + 'files' => $row['files'], + 'available' => (bool)$row['available'], + 'last_checked' => $row['last_checked'] ? new \DateTime('@' . $row['last_checked']) : null, + 'mount_id' => $row['mount_id'], + ]; + } + } + + /** + * @param StorageInfo $storage + * @return array + */ + public function formatStorage(array $storage): array { + return [ + 'numeric_id' => $storage['numeric_id'], + 'id' => $storage['id'], + 'files' => $storage['files'], + 'available' => $storage['available'] ? 'true' : 'false', + 'last_checked' => $storage['last_checked']?->format(\DATE_ATOM), + 'external_mount_id' => $storage['mount_id'], + ]; + } + + /** + * @param \Iterator<StorageInfo> $storages + * @return \Iterator + */ + public function formatStorages(\Iterator $storages): \Iterator { + foreach ($storages as $storage) { + yield $this->formatStorage($storage); + } + } } |