|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl> |
| 6 | + * |
| 7 | + * @license GNU AGPL version 3 or any later version |
| 8 | + * |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +namespace OC\Core\Command\Info; |
| 25 | + |
| 26 | + |
| 27 | +use OC\Files\SetupManager; |
| 28 | +use OCA\Circles\MountManager\CircleMount; |
| 29 | +use OCA\Files_External\Config\ExternalMountPoint; |
| 30 | +use OCA\Files_Sharing\SharedMount; |
| 31 | +use OCA\GroupFolders\Mount\GroupMountPoint; |
| 32 | +use OCP\Constants; |
| 33 | +use OCP\Files\Config\IUserMountCache; |
| 34 | +use OCP\Files\FileInfo; |
| 35 | +use OCP\Files\IHomeStorage; |
| 36 | +use OCP\Files\IRootFolder; |
| 37 | +use OCP\Files\Mount\IMountManager; |
| 38 | +use OCP\Files\Mount\IMountPoint; |
| 39 | +use OCP\Files\Node; |
| 40 | +use OCP\IUser; |
| 41 | +use OCP\Share\IShare; |
| 42 | +use OCP\Util; |
| 43 | +use Symfony\Component\Console\Output\OutputInterface; |
| 44 | +use OCP\Files\Folder; |
| 45 | + |
| 46 | +class FileUtils { |
| 47 | + private IRootFolder $rootFolder; |
| 48 | + private IUserMountCache $userMountCache; |
| 49 | + private IMountManager $mountManager; |
| 50 | + private SetupManager $setupManager; |
| 51 | + |
| 52 | + public function __construct( |
| 53 | + IRootFolder $rootFolder, |
| 54 | + IUserMountCache $userMountCache, |
| 55 | + IMountManager $mountManager, |
| 56 | + SetupManager $setupManager |
| 57 | + ) { |
| 58 | + $this->rootFolder = $rootFolder; |
| 59 | + $this->userMountCache = $userMountCache; |
| 60 | + $this->mountManager = $mountManager; |
| 61 | + $this->setupManager = $setupManager; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param FileInfo $file |
| 66 | + * @return array<string, Node[]> |
| 67 | + * @throws \OCP\Files\NotPermittedException |
| 68 | + * @throws \OC\User\NoUserException |
| 69 | + */ |
| 70 | + public function getFilesByUser(FileInfo $file): array { |
| 71 | + $id = $file->getId(); |
| 72 | + if (!$id) { |
| 73 | + return []; |
| 74 | + } |
| 75 | + |
| 76 | + $mounts = $this->userMountCache->getMountsForFileId($id); |
| 77 | + $result = []; |
| 78 | + foreach ($mounts as $mount) { |
| 79 | + if (isset($result[$mount->getUser()->getUID()])) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + $userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID()); |
| 84 | + $result[$mount->getUser()->getUID()] = $userFolder->getById($id); |
| 85 | + } |
| 86 | + |
| 87 | + return $result; |
| 88 | + } |
| 89 | + |
| 90 | + public function formatPermissions(string $type, int $permissions): string { |
| 91 | + if ($permissions == Constants::PERMISSION_ALL || ($type === 'file' && $permissions == (Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE))) { |
| 92 | + return "full permissions"; |
| 93 | + } |
| 94 | + |
| 95 | + $perms = []; |
| 96 | + $allPerms = [Constants::PERMISSION_READ => "read", Constants::PERMISSION_UPDATE => "update", Constants::PERMISSION_CREATE => "create", Constants::PERMISSION_DELETE => "delete", Constants::PERMISSION_SHARE => "share"]; |
| 97 | + foreach ($allPerms as $perm => $name) { |
| 98 | + if (($permissions & $perm) === $perm) { |
| 99 | + $perms[] = $name; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return implode(", ", $perms); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @psalm-suppress UndefinedClass |
| 108 | + * @psalm-suppress UndefinedInterfaceMethod |
| 109 | + */ |
| 110 | + public function formatMountType(IMountPoint $mountPoint): string { |
| 111 | + $storage = $mountPoint->getStorage(); |
| 112 | + if ($storage && $storage->instanceOfStorage(IHomeStorage::class)) { |
| 113 | + return "home storage"; |
| 114 | + } elseif ($mountPoint instanceof SharedMount) { |
| 115 | + $share = $mountPoint->getShare(); |
| 116 | + $shares = $mountPoint->getGroupedShares(); |
| 117 | + $sharedBy = array_map(function (IShare $share) { |
| 118 | + $shareType = $this->formatShareType($share); |
| 119 | + if ($shareType) { |
| 120 | + return $share->getSharedBy() . " (via " . $shareType . " " . $share->getSharedWith() . ")"; |
| 121 | + } else { |
| 122 | + return $share->getSharedBy(); |
| 123 | + } |
| 124 | + }, $shares); |
| 125 | + $description = "shared by " . implode(', ', $sharedBy); |
| 126 | + if ($share->getSharedBy() !== $share->getShareOwner()) { |
| 127 | + $description .= " owned by " . $share->getShareOwner(); |
| 128 | + } |
| 129 | + return $description; |
| 130 | + } elseif ($mountPoint instanceof GroupMountPoint) { |
| 131 | + return "groupfolder " . $mountPoint->getFolderId(); |
| 132 | + } elseif ($mountPoint instanceof ExternalMountPoint) { |
| 133 | + return "external storage " . $mountPoint->getStorageConfig()->getId(); |
| 134 | + } elseif ($mountPoint instanceof CircleMount) { |
| 135 | + return "circle"; |
| 136 | + } |
| 137 | + return get_class($mountPoint); |
| 138 | + } |
| 139 | + |
| 140 | + public function formatShareType(IShare $share): ?string { |
| 141 | + switch ($share->getShareType()) { |
| 142 | + case IShare::TYPE_GROUP: |
| 143 | + return "group"; |
| 144 | + case IShare::TYPE_CIRCLE: |
| 145 | + return "circle"; |
| 146 | + case IShare::TYPE_DECK: |
| 147 | + return "deck"; |
| 148 | + case IShare::TYPE_ROOM: |
| 149 | + return "room"; |
| 150 | + case IShare::TYPE_USER: |
| 151 | + return null; |
| 152 | + default: |
| 153 | + return "Unknown (" . $share->getShareType() . ")"; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @param IUser $user |
| 159 | + * @return IMountPoint[] |
| 160 | + */ |
| 161 | + public function getMountsForUser(IUser $user): array { |
| 162 | + $this->setupManager->setupForUser($user); |
| 163 | + $prefix = "/" . $user->getUID(); |
| 164 | + return array_filter($this->mountManager->getAll(), function (IMountPoint $mount) use ($prefix) { |
| 165 | + return str_starts_with($mount->getMountPoint(), $prefix); |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Print out the largest count($sizeLimits) files in the directory tree |
| 171 | + * |
| 172 | + * @param OutputInterface $output |
| 173 | + * @param Folder $node |
| 174 | + * @param string $prefix |
| 175 | + * @param array $sizeLimits largest items that are still in the queue to be printed, ordered ascending |
| 176 | + * @return void |
| 177 | + */ |
| 178 | + public function outputLargeFilesTree( |
| 179 | + OutputInterface $output, |
| 180 | + Folder $node, |
| 181 | + string $prefix, |
| 182 | + array &$sizeLimits |
| 183 | + ): int { |
| 184 | + $count = 0; |
| 185 | + $children = $node->getDirectoryListing(); |
| 186 | + usort($children, function (Node $a, Node $b) { |
| 187 | + return $b->getSize() <=> $a->getSize(); |
| 188 | + }); |
| 189 | + foreach ($children as $i => $child) { |
| 190 | + if (count($sizeLimits) === 0 || $child->getSize() < $sizeLimits[0]) { |
| 191 | + return $count; |
| 192 | + } |
| 193 | + array_shift($sizeLimits); |
| 194 | + $count += 1; |
| 195 | + |
| 196 | +// var_dump(implode(", ", $sizeLimits)); |
| 197 | + /** @var Node $child */ |
| 198 | + $output->writeln("$prefix- " . $child->getName() . ": " . Util::humanFileSize($child->getSize())); |
| 199 | + if ($child instanceof Folder) { |
| 200 | + $recurseSizeLimits = $sizeLimits; |
| 201 | + for ($j = 0; $j < count($recurseSizeLimits); $j++) { |
| 202 | + $nextChildSize = (int)$children[$i + $j]?->getSize(); |
| 203 | + if ($nextChildSize > $recurseSizeLimits[0]) { |
| 204 | + array_shift($recurseSizeLimits); |
| 205 | + $recurseSizeLimits[] = $nextChildSize; |
| 206 | + } |
| 207 | + } |
| 208 | + sort($recurseSizeLimits); |
| 209 | + $recurseCount = $this->outputLargeFilesTree($output, $child, $prefix . " ", $recurseSizeLimits); |
| 210 | + $sizeLimits = array_slice($sizeLimits, $recurseCount); |
| 211 | + $count += $recurseCount; |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments