aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Info/FileUtils.php
blob: bc07535a289aface42de798a21061ecedad247c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

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;
use OCP\Files\IHomeStorage;
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 NotPermittedException
	 * @throws NoUserException
	 */
	public function getFilesByUser(FileInfo $file): array {
		$id = $file->getId();
		if (!$id) {
			return [];
		}

		$mounts = $this->userMountCache->getMountsForFileId($id);
		$result = [];
		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;
	}

	/**
	 * Get file by either id of path
	 *
	 * @param string $fileInput
	 * @return Node|null
	 */
	public function getNode(string $fileInput): ?Node {
		if (is_numeric($fileInput)) {
			$mounts = $this->userMountCache->getMountsForFileId((int)$fileInput);
			if (!$mounts) {
				return null;
			}
			$mount = reset($mounts);
			$userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID());
			return $userFolder->getFirstNodeById((int)$fileInput);
		} else {
			try {
				return $this->rootFolder->get($fileInput);
			} catch (NotFoundException $e) {
				return null;
			}
		}
	}

	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';
		}

		$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
	 */
	public 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);
	}

	public 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() . ')';
		}
	}

	/**
	 * Print out the largest count($sizeLimits) files in the directory tree
	 *
	 * @param OutputInterface $output
	 * @param Folder $node
	 * @param string $prefix
	 * @param array $sizeLimits largest items that are still in the queue to be printed, ordered ascending
	 * @return int how many items we've printed
	 */
	public function outputLargeFilesTree(
		OutputInterface $output,
		Folder $node,
		string $prefix,
		array &$sizeLimits,
		bool $all,
	): int {
		/**
		 * Algorithm to print the N largest items in a folder without requiring to query or sort the entire three
		 *
		 * This is done by keeping a list ($sizeLimits) of size N that contain the largest items outside of this
		 * folders that are could be printed if there aren't enough items in this folder that are larger.
		 *
		 * We loop over the items in this folder by size descending until the size of the item falls before the smallest
		 * size in $sizeLimits (at that point there are enough items outside this folder to complete the N items).
		 *
		 * When encountering a folder, we create an updated $sizeLimits with the largest items in the current folder still
		 * remaining which we pass into the recursion. (We don't update the current $sizeLimits because that should only
		 * hold items *outside* of the current folder.)
		 *
		 * For every item printed we remove the first item of $sizeLimits are there is no longer room in the output to print
		 * items that small.
		 */

		$count = 0;
		$children = $node->getDirectoryListing();
		usort($children, function (Node $a, Node $b) {
			return $b->getSize() <=> $a->getSize();
		});
		foreach ($children as $i => $child) {
			if (!$all) {
				if (count($sizeLimits) === 0 || $child->getSize() < $sizeLimits[0]) {
					return $count;
				}
				array_shift($sizeLimits);
			}
			$count += 1;

			/** @var Node $child */
			$output->writeln("$prefix- " . $child->getName() . ': <info>' . Util::humanFileSize($child->getSize()) . '</info>');
			if ($child instanceof Folder) {
				$recurseSizeLimits = $sizeLimits;
				if (!$all) {
					for ($j = 0; $j < count($recurseSizeLimits); $j++) {
						if (isset($children[$i + $j + 1])) {
							$nextChildSize = $children[$i + $j + 1]->getSize();
							if ($nextChildSize > $recurseSizeLimits[0]) {
								array_shift($recurseSizeLimits);
								$recurseSizeLimits[] = $nextChildSize;
							}
						}
					}
					sort($recurseSizeLimits);
				}
				$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);
		}
	}
}