-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathLocalPreviewStorage.php
More file actions
396 lines (347 loc) · 12.3 KB
/
LocalPreviewStorage.php
File metadata and controls
396 lines (347 loc) · 12.3 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileContributor: Carl Schwan
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Preview\Storage;
use LogicException;
use OC;
use OC\Files\SimpleFS\SimpleFile;
use OC\Preview\Db\Preview;
use OC\Preview\Db\PreviewMapper;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDBConnection;
use Override;
use Psr\Log\LoggerInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class LocalPreviewStorage implements IPreviewStorage {
private const SCAN_BATCH_SIZE = 1000;
public function __construct(
private readonly IConfig $config,
private readonly PreviewMapper $previewMapper,
private readonly IAppConfig $appConfig,
private readonly IDBConnection $connection,
private readonly IMimeTypeDetector $mimeTypeDetector,
private readonly LoggerInterface $logger,
private readonly IMimeTypeLoader $mimeTypeLoader,
private readonly IRootFolder $rootFolder,
) {
}
#[Override]
public function writePreview(Preview $preview, mixed $stream): int {
$previewPath = $this->constructPath($preview);
$this->createParentFiles($previewPath);
return file_put_contents($previewPath, $stream);
}
#[Override]
public function readPreview(Preview $preview): mixed {
$previewPath = $this->constructPath($preview);
$resource = @fopen($previewPath, 'r');
if ($resource === false) {
throw new NotFoundException('Unable to open preview stream at ' . $previewPath);
}
return $resource;
}
#[Override]
public function deletePreview(Preview $preview): void {
$previewPath = $this->constructPath($preview);
if (!@unlink($previewPath) && is_file($previewPath)) {
throw new NotPermittedException('Unable to delete preview at ' . $previewPath);
}
}
public function getRootFolder(): string {
return $this->config->getSystemValueString('datadirectory', OC::$SERVERROOT . '/data');
}
public function getPreviewRootFolder(): string {
return $this->getRootFolder() . '/' . $this->rootFolder->getAppDataDirectoryName() . '/preview/';
}
private function constructPath(Preview $preview): string {
return $this->getPreviewRootFolder() . implode('/', str_split(substr(md5((string)$preview->getFileId()), 0, 7))) . '/' . $preview->getFileId() . '/' . $preview->getName();
}
private function createParentFiles(string $path): void {
$dirname = dirname($path);
if (!is_dir($dirname)) {
mkdir($dirname, recursive: true);
}
if (!is_dir($dirname)) {
throw new NotPermittedException("Unable to create directory '$dirname'");
}
}
#[Override]
public function migratePreview(Preview $preview, SimpleFile $file): void {
// legacy flat directory
$sourcePath = $this->getPreviewRootFolder() . $preview->getFileId() . '/' . $preview->getName();
if (!file_exists($sourcePath)) {
return;
}
$destinationPath = $this->constructPath($preview);
if (file_exists($destinationPath)) {
@unlink($sourcePath); // We already have a new preview, just delete the old one
return;
}
$this->createParentFiles($destinationPath);
$ok = rename($sourcePath, $destinationPath);
if (!$ok) {
throw new LogicException('Failed to move ' . $sourcePath . ' to ' . $destinationPath);
}
}
#[Override]
public function scan(): int {
$checkForFileCache = !$this->appConfig->getValueBool('core', 'previewMovedDone');
if (!file_exists($this->getPreviewRootFolder())) {
return 0;
}
$scanner = new RecursiveDirectoryIterator($this->getPreviewRootFolder());
$previewsFound = 0;
/**
* Use an associative array keyed by path for O(1) lookup instead of
* the O(n) in_array() the original code used.
*
* @var array<string, true> $skipPaths
*/
$skipPaths = [];
/**
* Pending previews grouped by fileId. A single original file can have
* many preview variants (different sizes/formats), so we group them to
* issue one filecache lookup per original file rather than one per
* preview variant.
*
* @var array<int, list<array{preview: Preview, filePath: string, realPath: string}>> $pendingByFileId
*/
$pendingByFileId = [];
/**
* path_hash => realPath for legacy filecache entries that need to be
* cleaned up. Only populated when $checkForFileCache is true.
*
* @var array<string, string> $pendingPathHashes
*/
$pendingPathHashes = [];
$pendingCount = 0;
foreach (new RecursiveIteratorIterator($scanner) as $file) {
if (!$file->isFile()) {
continue;
}
$filePath = $file->getPathname();
if (isset($skipPaths[$filePath])) {
continue;
}
$preview = Preview::fromPath($filePath, $this->mimeTypeDetector);
if ($preview === false) {
$this->logger->error('Unable to parse preview information for ' . $file->getRealPath());
continue;
}
$preview->setSize($file->getSize());
$preview->setMtime($file->getMtime());
$preview->setEncrypted(false);
$realPath = $file->getRealPath();
$pendingByFileId[$preview->getFileId()][] = [
'preview' => $preview,
'filePath' => $filePath,
'realPath' => $realPath,
];
$pendingCount++;
if ($checkForFileCache) {
$relativePath = str_replace($this->getRootFolder() . '/', '', $realPath);
$pendingPathHashes[md5($relativePath)] = $realPath;
}
if ($pendingCount >= self::SCAN_BATCH_SIZE) {
$this->connection->beginTransaction();
try {
$previewsFound += $this->processScanBatch($pendingByFileId, $pendingPathHashes, $checkForFileCache, $skipPaths);
$this->connection->commit();
} catch (\Exception $e) {
$this->connection->rollBack();
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw $e;
}
$pendingByFileId = [];
$pendingPathHashes = [];
$pendingCount = 0;
}
}
if ($pendingCount > 0) {
$this->connection->beginTransaction();
try {
$previewsFound += $this->processScanBatch($pendingByFileId, $pendingPathHashes, $checkForFileCache, $skipPaths);
$this->connection->commit();
} catch (\Exception $e) {
$this->connection->rollBack();
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw $e;
}
}
return $previewsFound;
}
/**
* Process one batch of preview files collected during scan().
*
* @param array<int, list<array{preview: Preview, filePath: string, realPath: string}>> $pendingByFileId
* @param array<string, string> $pendingPathHashes path_hash => realPath
* @param array<string, true> $skipPaths Modified in place: newly-moved paths are added so the outer iterator skips them.
*/
private function processScanBatch(
array $pendingByFileId,
array $pendingPathHashes,
bool $checkForFileCache,
array &$skipPaths,
): int {
$filecacheByFileId = $this->fetchFilecacheByFileIds(array_keys($pendingByFileId));
$legacyByPathHash = [];
if ($checkForFileCache && $pendingPathHashes !== []) {
$legacyByPathHash = $this->fetchFilecacheByPathHashes(array_keys($pendingPathHashes));
}
$previewsFound = 0;
foreach ($pendingByFileId as $fileId => $items) {
if (!isset($filecacheByFileId[$fileId])) {
// Original file has been deleted – clean up all its previews.
foreach ($items as $item) {
$this->logger->warning('Original file ' . $fileId . ' was not found. Deleting preview at ' . $item['realPath']);
@unlink($item['realPath']);
}
continue;
}
$filecacheRow = $filecacheByFileId[$fileId];
foreach ($items as $item) {
$preview = $item['preview'];
if ($checkForFileCache) {
$relativePath = str_replace($this->getRootFolder() . '/', '', $item['realPath']);
$pathHash = md5($relativePath);
if (isset($legacyByPathHash[$pathHash])) {
$legacyRow = $legacyByPathHash[$pathHash];
$qb = $this->connection->getQueryBuilder();
$qb->delete('filecache')
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($legacyRow['fileid'])))
->andWhere($qb->expr()->eq('storage', $qb->createNamedParameter($legacyRow['storage'])))
->executeStatement();
$this->deleteParentsFromFileCache((int)$legacyRow['parent'], (int)$legacyRow['storage']);
}
}
$preview->setStorageId((int)$filecacheRow['storage']);
$preview->setEtag($filecacheRow['etag']);
$preview->setSourceMimetype($this->mimeTypeLoader->getMimetypeById((int)$filecacheRow['mimetype']));
$preview->generateId();
$this->connection->beginTransaction();
try {
$this->previewMapper->insert($preview);
$this->connection->commit();
} catch (Exception $e) {
$this->connection->rollBack();
if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
throw $e;
}
}
// Move old flat preview to new nested directory format.
$dirName = str_replace($this->getPreviewRootFolder(), '', $item['filePath']);
if (preg_match('/[0-9a-e]\/[0-9a-e]\/[0-9a-e]\/[0-9a-e]\/[0-9a-e]\/[0-9a-e]\/[0-9a-e]\/[0-9]+/', $dirName) !== 1) {
$previewPath = $this->constructPath($preview);
$this->createParentFiles($previewPath);
$ok = rename($item['realPath'], $previewPath);
if (!$ok) {
throw new LogicException('Failed to move ' . $item['realPath'] . ' to ' . $previewPath);
}
// Mark the destination so the outer iterator skips it if it encounters the path later.
$skipPaths[$previewPath] = true;
}
$previewsFound++;
}
}
return $previewsFound;
}
/**
* Bulk-fetch filecache rows for a set of fileIds.
*
* @param int[] $fileIds
*/
private function fetchFilecacheByFileIds(array $fileIds): array {
if (empty($fileIds)) {
return [];
}
$result = [];
foreach (array_chunk($fileIds, 1000) as $chunk) {
$qb = $this->connection->getQueryBuilder();
$qb->select('fileid', 'storage', 'etag', 'mimetype')
->from('filecache')
->where($qb->expr()->in('fileid', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
$rows = $qb->runAcrossAllShards()
->executeQuery();
while ($row = $rows->fetchAssociative()) {
$result[(int)$row['fileid']] = $row;
}
$rows->closeCursor();
}
return $result;
}
/**
* Bulk-fetch filecache rows for a set of path_hashes (legacy migration).
*
* @param string[] $pathHashes
*/
private function fetchFilecacheByPathHashes(array $pathHashes): array {
if (empty($pathHashes)) {
return [];
}
$result = [];
foreach (array_chunk($pathHashes, 1000) as $chunk) {
$qb = $this->connection->getQueryBuilder();
$qb->select('fileid', 'storage', 'etag', 'mimetype', 'parent', 'path_hash')
->from('filecache')
->where($qb->expr()->in('path_hash', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY)));
$rows = $qb->runAcrossAllShards()
->executeQuery();
while ($row = $rows->fetchAssociative()) {
$result[$row['path_hash']] = $row;
}
$rows->closeCursor();
}
return $result;
}
/**
* Recursive method that deletes the folder and its parent folders if it's not
* empty.
*/
private function deleteParentsFromFileCache(int $folderId, int $storageId): void {
$qb = $this->connection->getQueryBuilder();
$result = $qb->select('fileid', 'path', 'storage', 'parent')
->from('filecache')
->where($qb->expr()->eq('parent', $qb->createNamedParameter($folderId)))
->setMaxResults(1)
->runAcrossAllShards()
->executeQuery();
$row = $result->fetchAssociative();
$result->closeCursor();
if ($row !== false) {
// there are other files in the directory, don't delete yet
return;
}
// Get new parent
$qb = $this->connection->getQueryBuilder();
$result = $qb->select('fileid', 'path', 'parent')
->from('filecache')
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($folderId)))
->andWhere($qb->expr()->eq('storage', $qb->createNamedParameter($storageId)))
->setMaxResults(1)
->executeQuery();
$row = $result->fetchAssociative();
$result->closeCursor();
if ($row !== false) {
$parentFolderId = (int)$row['parent'];
$qb = $this->connection->getQueryBuilder();
$qb->delete('filecache')
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($folderId)))
->andWhere($qb->expr()->eq('storage', $qb->createNamedParameter($storageId)))
->executeStatement();
$this->deleteParentsFromFileCache($parentFolderId, $storageId);
}
}
}