Skip to content

Commit f8b4e00

Browse files
authored
Merge pull request #37865 from nextcloud/info-file-space
add command to summarize space usage
2 parents 8ada0ce + 5c31c35 commit f8b4e00

File tree

6 files changed

+333
-131
lines changed

6 files changed

+333
-131
lines changed

core/Command/Info/File.php

Lines changed: 7 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@
55
namespace OC\Core\Command\Info;
66

77
use OC\Files\ObjectStore\ObjectStoreStorage;
8-
use OCA\Circles\MountManager\CircleMount;
98
use OCA\Files_External\Config\ExternalMountPoint;
10-
use OCA\Files_Sharing\SharedMount;
119
use OCA\GroupFolders\Mount\GroupMountPoint;
12-
use OCP\Constants;
13-
use OCP\Files\Config\IUserMountCache;
14-
use OCP\Files\FileInfo;
1510
use OCP\Files\Folder;
1611
use OCP\Files\IHomeStorage;
17-
use OCP\Files\IRootFolder;
1812
use OCP\Files\Mount\IMountPoint;
1913
use OCP\Files\Node;
2014
use OCP\Files\NotFoundException;
2115
use OCP\IL10N;
2216
use OCP\L10N\IFactory;
23-
use OCP\Share\IShare;
2417
use OCP\Util;
2518
use Symfony\Component\Console\Command\Command;
2619
use Symfony\Component\Console\Input\InputArgument;
@@ -29,14 +22,12 @@
2922
use Symfony\Component\Console\Output\OutputInterface;
3023

3124
class File extends Command {
32-
private IRootFolder $rootFolder;
33-
private IUserMountCache $userMountCache;
3425
private IL10N $l10n;
26+
private FileUtils $fileUtils;
3527

36-
public function __construct(IRootFolder $rootFolder, IUserMountCache $userMountCache, IFactory $l10nFactory) {
37-
$this->rootFolder = $rootFolder;
38-
$this->userMountCache = $userMountCache;
28+
public function __construct(IFactory $l10nFactory, FileUtils $fileUtils) {
3929
$this->l10n = $l10nFactory->get("core");
30+
$this->fileUtils = $fileUtils;
4031
parent::__construct();
4132
}
4233

@@ -51,7 +42,7 @@ protected function configure(): void {
5142
public function execute(InputInterface $input, OutputInterface $output): int {
5243
$fileInput = $input->getArgument('file');
5344
$showChildren = $input->getOption('children');
54-
$node = $this->getNode($fileInput);
45+
$node = $this->fileUtils->getNode($fileInput);
5546
if (!$node) {
5647
$output->writeln("<error>file $fileInput not found</error>");
5748
return 1;
@@ -83,137 +74,22 @@ public function execute(InputInterface $input, OutputInterface $output): int {
8374
}
8475
$this->outputStorageDetails($node->getMountPoint(), $node, $output);
8576

86-
$filesPerUser = $this->getFilesByUser($node);
77+
$filesPerUser = $this->fileUtils->getFilesByUser($node);
8778
$output->writeln("");
8879
$output->writeln("The following users have access to the file");
8980
$output->writeln("");
9081
foreach ($filesPerUser as $user => $files) {
9182
$output->writeln("$user:");
9283
foreach ($files as $userFile) {
93-
$output->writeln(" " . $userFile->getPath() . ": " . $this->formatPermissions($userFile->getType(), $userFile->getPermissions()));
84+
$output->writeln(" " . $userFile->getPath() . ": " . $this->fileUtils->formatPermissions($userFile->getType(), $userFile->getPermissions()));
9485
$mount = $userFile->getMountPoint();
95-
$output->writeln(" " . $this->formatMountType($mount));
86+
$output->writeln(" " . $this->fileUtils->formatMountType($mount));
9687
}
9788
}
9889

9990
return 0;
10091
}
10192

102-
private function getNode(string $fileInput): ?Node {
103-
if (is_numeric($fileInput)) {
104-
$mounts = $this->userMountCache->getMountsForFileId((int)$fileInput);
105-
if (!$mounts) {
106-
return null;
107-
}
108-
$mount = $mounts[0];
109-
$userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID());
110-
$nodes = $userFolder->getById((int)$fileInput);
111-
if (!$nodes) {
112-
return null;
113-
}
114-
return $nodes[0];
115-
} else {
116-
try {
117-
return $this->rootFolder->get($fileInput);
118-
} catch (NotFoundException $e) {
119-
return null;
120-
}
121-
}
122-
}
123-
124-
/**
125-
* @param FileInfo $file
126-
* @return array<string, Node[]>
127-
* @throws \OCP\Files\NotPermittedException
128-
* @throws \OC\User\NoUserException
129-
*/
130-
private function getFilesByUser(FileInfo $file): array {
131-
$id = $file->getId();
132-
if (!$id) {
133-
return [];
134-
}
135-
136-
$mounts = $this->userMountCache->getMountsForFileId($id);
137-
$result = [];
138-
foreach ($mounts as $mount) {
139-
if (isset($result[$mount->getUser()->getUID()])) {
140-
continue;
141-
}
142-
143-
$userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID());
144-
$result[$mount->getUser()->getUID()] = $userFolder->getById($id);
145-
}
146-
147-
return $result;
148-
}
149-
150-
private function formatPermissions(string $type, int $permissions): string {
151-
if ($permissions == Constants::PERMISSION_ALL || ($type === 'file' && $permissions == (Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE))) {
152-
return "full permissions";
153-
}
154-
155-
$perms = [];
156-
$allPerms = [Constants::PERMISSION_READ => "read", Constants::PERMISSION_UPDATE => "update", Constants::PERMISSION_CREATE => "create", Constants::PERMISSION_DELETE => "delete", Constants::PERMISSION_SHARE => "share"];
157-
foreach ($allPerms as $perm => $name) {
158-
if (($permissions & $perm) === $perm) {
159-
$perms[] = $name;
160-
}
161-
}
162-
163-
return implode(", ", $perms);
164-
}
165-
166-
/**
167-
* @psalm-suppress UndefinedClass
168-
* @psalm-suppress UndefinedInterfaceMethod
169-
*/
170-
private function formatMountType(IMountPoint $mountPoint): string {
171-
$storage = $mountPoint->getStorage();
172-
if ($storage && $storage->instanceOfStorage(IHomeStorage::class)) {
173-
return "home storage";
174-
} elseif ($mountPoint instanceof SharedMount) {
175-
$share = $mountPoint->getShare();
176-
$shares = $mountPoint->getGroupedShares();
177-
$sharedBy = array_map(function (IShare $share) {
178-
$shareType = $this->formatShareType($share);
179-
if ($shareType) {
180-
return $share->getSharedBy() . " (via " . $shareType . " " . $share->getSharedWith() . ")";
181-
} else {
182-
return $share->getSharedBy();
183-
}
184-
}, $shares);
185-
$description = "shared by " . implode(', ', $sharedBy);
186-
if ($share->getSharedBy() !== $share->getShareOwner()) {
187-
$description .= " owned by " . $share->getShareOwner();
188-
}
189-
return $description;
190-
} elseif ($mountPoint instanceof GroupMountPoint) {
191-
return "groupfolder " . $mountPoint->getFolderId();
192-
} elseif ($mountPoint instanceof ExternalMountPoint) {
193-
return "external storage " . $mountPoint->getStorageConfig()->getId();
194-
} elseif ($mountPoint instanceof CircleMount) {
195-
return "circle";
196-
}
197-
return get_class($mountPoint);
198-
}
199-
200-
private function formatShareType(IShare $share): ?string {
201-
switch ($share->getShareType()) {
202-
case IShare::TYPE_GROUP:
203-
return "group";
204-
case IShare::TYPE_CIRCLE:
205-
return "circle";
206-
case IShare::TYPE_DECK:
207-
return "deck";
208-
case IShare::TYPE_ROOM:
209-
return "room";
210-
case IShare::TYPE_USER:
211-
return null;
212-
default:
213-
return "Unknown (".$share->getShareType().")";
214-
}
215-
}
216-
21793
/**
21894
* @psalm-suppress UndefinedClass
21995
* @psalm-suppress UndefinedInterfaceMethod

0 commit comments

Comments
 (0)