2025-08-21 10:42:34 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2025-09-16 05:34:41 -04:00
|
|
|
|
2025-10-09 09:12:54 -04:00
|
|
|
/**
|
2025-09-16 05:34:41 -04:00
|
|
|
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH
|
|
|
|
|
* SPDX-FileContributor: Carl Schwan
|
2025-08-21 10:42:34 -04:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
*/
|
|
|
|
|
|
2025-10-09 09:12:54 -04:00
|
|
|
namespace OC\Preview;
|
2025-08-21 10:42:34 -04:00
|
|
|
|
2025-09-10 04:51:41 -04:00
|
|
|
use OC\Files\SimpleFS\SimpleFile;
|
2025-08-21 10:42:34 -04:00
|
|
|
use OC\Preview\Db\Preview;
|
|
|
|
|
use OC\Preview\Db\PreviewMapper;
|
|
|
|
|
use OC\Preview\Storage\StorageFactory;
|
|
|
|
|
use OCP\DB\Exception;
|
|
|
|
|
use OCP\Files\AppData\IAppDataFactory;
|
|
|
|
|
use OCP\Files\IAppData;
|
2025-09-30 07:44:34 -04:00
|
|
|
use OCP\Files\IMimeTypeDetector;
|
|
|
|
|
use OCP\Files\IMimeTypeLoader;
|
2025-09-16 05:34:41 -04:00
|
|
|
use OCP\Files\IRootFolder;
|
2025-10-09 09:12:54 -04:00
|
|
|
use OCP\Files\NotFoundException;
|
2025-09-30 09:56:31 -04:00
|
|
|
use OCP\IConfig;
|
2025-08-21 10:42:34 -04:00
|
|
|
use OCP\IDBConnection;
|
2025-09-30 07:44:34 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2025-08-21 10:42:34 -04:00
|
|
|
|
2025-10-09 09:12:54 -04:00
|
|
|
class PreviewMigrationService {
|
2025-08-21 10:42:34 -04:00
|
|
|
private IAppData $appData;
|
2025-09-30 09:56:31 -04:00
|
|
|
private string $previewRootPath;
|
2025-08-21 10:42:34 -04:00
|
|
|
|
|
|
|
|
public function __construct(
|
2025-09-30 09:56:31 -04:00
|
|
|
private readonly IConfig $config,
|
2025-09-16 05:34:41 -04:00
|
|
|
private readonly IRootFolder $rootFolder,
|
2025-10-09 09:12:54 -04:00
|
|
|
private readonly LoggerInterface $logger,
|
2025-09-30 07:44:34 -04:00
|
|
|
private readonly IMimeTypeDetector $mimeTypeDetector,
|
|
|
|
|
private readonly IMimeTypeLoader $mimeTypeLoader,
|
2025-10-09 09:12:54 -04:00
|
|
|
private readonly IDBConnection $connection,
|
|
|
|
|
private readonly PreviewMapper $previewMapper,
|
|
|
|
|
private readonly StorageFactory $storageFactory,
|
2025-08-21 10:42:34 -04:00
|
|
|
IAppDataFactory $appDataFactory,
|
|
|
|
|
) {
|
|
|
|
|
$this->appData = $appDataFactory->get('preview');
|
2025-09-30 09:56:31 -04:00
|
|
|
$this->previewRootPath = 'appdata_' . $this->config->getSystemValueString('instanceid') . '/preview/';
|
2025-08-21 10:42:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-11 08:52:34 -04:00
|
|
|
* @param array<string|int, string[]> $previewFolders
|
2025-10-09 09:12:54 -04:00
|
|
|
* @return Preview[]
|
2025-08-21 10:42:34 -04:00
|
|
|
*/
|
2025-10-09 09:12:54 -04:00
|
|
|
public function migrateFileId(int $fileId, bool $flatPath): array {
|
|
|
|
|
$previews = [];
|
2025-09-30 09:56:31 -04:00
|
|
|
$internalPath = $this->getInternalFolder((string)$fileId, $flatPath);
|
2025-10-09 09:12:54 -04:00
|
|
|
try {
|
|
|
|
|
$folder = $this->appData->getFolder($internalPath);
|
|
|
|
|
} catch (NotFoundException) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2025-09-16 05:34:41 -04:00
|
|
|
|
|
|
|
|
/**
|
2025-09-25 08:25:47 -04:00
|
|
|
* @var list<array{file: SimpleFile, preview: Preview}> $previewFiles
|
2025-09-16 05:34:41 -04:00
|
|
|
*/
|
|
|
|
|
$previewFiles = [];
|
|
|
|
|
|
|
|
|
|
foreach ($folder->getDirectoryListing() as $previewFile) {
|
2025-09-30 07:44:34 -04:00
|
|
|
$path = $fileId . '/' . $previewFile->getName();
|
2025-09-16 05:34:41 -04:00
|
|
|
/** @var SimpleFile $previewFile */
|
2025-09-30 09:56:31 -04:00
|
|
|
$preview = Preview::fromPath($path, $this->mimeTypeDetector);
|
2026-01-05 15:37:23 -05:00
|
|
|
$preview->generateId();
|
2025-09-30 07:44:34 -04:00
|
|
|
if (!$preview) {
|
|
|
|
|
$this->logger->error('Unable to import old preview at path.');
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-09-25 08:25:47 -04:00
|
|
|
$preview->setSize($previewFile->getSize());
|
|
|
|
|
$preview->setMtime($previewFile->getMtime());
|
|
|
|
|
$preview->setOldFileId($previewFile->getId());
|
|
|
|
|
$preview->setEncrypted(false);
|
2025-08-21 10:42:34 -04:00
|
|
|
|
2025-09-16 05:34:41 -04:00
|
|
|
$previewFiles[] = [
|
|
|
|
|
'file' => $previewFile,
|
2025-09-25 08:25:47 -04:00
|
|
|
'preview' => $preview,
|
2025-09-16 05:34:41 -04:00
|
|
|
];
|
|
|
|
|
}
|
2025-08-21 10:42:34 -04:00
|
|
|
|
2025-10-09 09:12:54 -04:00
|
|
|
if (empty($previewFiles)) {
|
|
|
|
|
return $previews;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 05:34:41 -04:00
|
|
|
$qb = $this->connection->getQueryBuilder();
|
2025-09-26 06:26:43 -04:00
|
|
|
$qb->select('storage', 'etag', 'mimetype')
|
2025-09-16 05:34:41 -04:00
|
|
|
->from('filecache')
|
|
|
|
|
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($fileId)))
|
|
|
|
|
->setMaxResults(1);
|
|
|
|
|
|
|
|
|
|
$result = $qb->executeQuery();
|
2025-11-17 06:20:11 -05:00
|
|
|
$result = $result->fetchAllAssociative();
|
2025-09-16 05:34:41 -04:00
|
|
|
|
|
|
|
|
if (count($result) > 0) {
|
|
|
|
|
foreach ($previewFiles as $previewFile) {
|
2025-09-30 09:56:31 -04:00
|
|
|
/** @var Preview $preview */
|
2025-09-25 08:25:47 -04:00
|
|
|
$preview = $previewFile['preview'];
|
2025-09-16 05:34:41 -04:00
|
|
|
/** @var SimpleFile $file */
|
|
|
|
|
$file = $previewFile['file'];
|
|
|
|
|
$preview->setStorageId($result[0]['storage']);
|
|
|
|
|
$preview->setEtag($result[0]['etag']);
|
2025-09-30 09:56:31 -04:00
|
|
|
$preview->setSourceMimeType($this->mimeTypeLoader->getMimetypeById((int)$result[0]['mimetype']));
|
2026-01-05 15:37:23 -05:00
|
|
|
$preview->generateId();
|
2025-09-16 05:34:41 -04:00
|
|
|
try {
|
|
|
|
|
$preview = $this->previewMapper->insert($preview);
|
2025-09-30 09:56:31 -04:00
|
|
|
} catch (Exception) {
|
2025-09-16 05:34:41 -04:00
|
|
|
// We already have this preview in the preview table, skip
|
2025-09-30 09:56:31 -04:00
|
|
|
$qb->delete('filecache')
|
|
|
|
|
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($file->getId())))
|
|
|
|
|
->hintShardKey('storage', $this->rootFolder->getMountPoint()->getNumericStorageId())
|
|
|
|
|
->executeStatement();
|
2025-09-16 05:34:41 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2025-08-21 10:42:34 -04:00
|
|
|
|
2025-09-16 05:34:41 -04:00
|
|
|
try {
|
|
|
|
|
$this->storageFactory->migratePreview($preview, $file);
|
2025-09-30 09:56:31 -04:00
|
|
|
$qb = $this->connection->getQueryBuilder();
|
2025-09-16 05:34:41 -04:00
|
|
|
$qb->delete('filecache')
|
|
|
|
|
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($file->getId())))
|
2025-09-30 09:56:31 -04:00
|
|
|
->hintShardKey('storage', $this->rootFolder->getMountPoint()->getNumericStorageId())
|
2025-09-16 05:34:41 -04:00
|
|
|
->executeStatement();
|
|
|
|
|
// Do not call $file->delete() as this will also delete the file from the file system
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->previewMapper->delete($preview);
|
|
|
|
|
throw $e;
|
2025-08-21 10:42:34 -04:00
|
|
|
}
|
2025-10-09 09:12:54 -04:00
|
|
|
|
|
|
|
|
$previews[] = $preview;
|
2025-08-21 10:42:34 -04:00
|
|
|
}
|
2025-09-30 09:56:31 -04:00
|
|
|
} else {
|
|
|
|
|
// No matching fileId, delete preview
|
|
|
|
|
try {
|
|
|
|
|
$this->connection->beginTransaction();
|
|
|
|
|
foreach ($previewFiles as $previewFile) {
|
|
|
|
|
/** @var SimpleFile $file */
|
|
|
|
|
$file = $previewFile['file'];
|
|
|
|
|
$file->delete();
|
|
|
|
|
}
|
|
|
|
|
$this->connection->commit();
|
|
|
|
|
} catch (Exception) {
|
|
|
|
|
$this->connection->rollback();
|
|
|
|
|
}
|
2025-08-21 10:42:34 -04:00
|
|
|
}
|
2025-09-16 05:34:41 -04:00
|
|
|
|
2025-09-30 09:56:31 -04:00
|
|
|
$this->deleteFolder($internalPath);
|
2025-10-09 09:12:54 -04:00
|
|
|
|
|
|
|
|
return $previews;
|
2025-08-21 10:42:34 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-09 09:12:54 -04:00
|
|
|
private static function getInternalFolder(string $name, bool $flatPath): string {
|
2025-09-30 09:56:31 -04:00
|
|
|
if ($flatPath) {
|
|
|
|
|
return $name;
|
2025-08-21 10:42:34 -04:00
|
|
|
}
|
|
|
|
|
return implode('/', str_split(substr(md5($name), 0, 7))) . '/' . $name;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-30 09:56:31 -04:00
|
|
|
private function deleteFolder(string $path): void {
|
2025-08-21 10:42:34 -04:00
|
|
|
$current = $path;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
2025-09-30 09:56:31 -04:00
|
|
|
$appDataPath = $this->previewRootPath . $current;
|
|
|
|
|
$qb = $this->connection->getQueryBuilder();
|
|
|
|
|
$qb->delete('filecache')
|
|
|
|
|
->where($qb->expr()->eq('path_hash', $qb->createNamedParameter(md5($appDataPath))))
|
|
|
|
|
->hintShardKey('storage', $this->rootFolder->getMountPoint()->getNumericStorageId())
|
|
|
|
|
->executeStatement();
|
|
|
|
|
|
2025-08-21 10:42:34 -04:00
|
|
|
$current = dirname($current);
|
|
|
|
|
if ($current === '/' || $current === '.' || $current === '') {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$folder = $this->appData->getFolder($current);
|
|
|
|
|
if (count($folder->getDirectoryListing()) !== 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|