FileUtils.php 8.0 KB

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