Skip to content

Commit 66f50bd

Browse files
committed
refactor(preview): Use same mimetype ids as filecache
Signed-off-by: Carl Schwan <[email protected]>
1 parent bd001c9 commit 66f50bd

File tree

15 files changed

+184
-180
lines changed

15 files changed

+184
-180
lines changed

core/BackgroundJobs/MovePreviewJob.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
use OCP\DB\Exception;
2020
use OCP\Files\AppData\IAppDataFactory;
2121
use OCP\Files\IAppData;
22+
use OCP\Files\IMimeTypeDetector;
23+
use OCP\Files\IMimeTypeLoader;
2224
use OCP\Files\IRootFolder;
2325
use OCP\Files\NotFoundException;
2426
use OCP\Files\SimpleFS\ISimpleFolder;
2527
use OCP\IAppConfig;
2628
use OCP\IDBConnection;
2729
use Override;
30+
use Psr\Log\LoggerInterface;
2831

2932
class MovePreviewJob extends TimedJob {
3033
private IAppData $appData;
@@ -36,6 +39,9 @@ public function __construct(
3639
private readonly StorageFactory $storageFactory,
3740
private readonly IDBConnection $connection,
3841
private readonly IRootFolder $rootFolder,
42+
private readonly IMimeTypeDetector $mimeTypeDetector,
43+
private readonly IMimeTypeLoader $mimeTypeLoader,
44+
private readonly LoggerInterface $logger,
3945
IAppDataFactory $appDataFactory,
4046
) {
4147
parent::__construct($time);
@@ -125,8 +131,13 @@ private function processPreviews(int|string $fileId, bool $simplePaths): void {
125131
$previewFiles = [];
126132

127133
foreach ($folder->getDirectoryListing() as $previewFile) {
134+
$path = $fileId . '/' . $previewFile->getName();
128135
/** @var SimpleFile $previewFile */
129-
$preview = Preview::fromPath($fileId . '/' . $previewFile->getName());
136+
$preview = Preview::fromPath($path, $this->mimeTypeDetector, $this->mimeTypeLoader);
137+
if (!$preview) {
138+
$this->logger->error('Unable to import old preview at path.');
139+
continue;
140+
}
130141
$preview->setSize($previewFile->getSize());
131142
$preview->setMtime($previewFile->getMtime());
132143
$preview->setOldFileId($previewFile->getId());

core/Command/Preview/ResetRenderedTexts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private function deletePreviews(OutputInterface $output, bool $dryMode): void {
9393
$previewsToDeleteCount = 0;
9494

9595
foreach ($this->getPreviewsToDelete() as $preview) {
96-
$output->writeln('Deleting preview ' . $preview->getName() . ' for fileId ' . $preview->getFileId(), OutputInterface::VERBOSITY_VERBOSE);
96+
$output->writeln('Deleting preview ' . $preview->getName($this->mimeTypeLoader) . ' for fileId ' . $preview->getFileId(), OutputInterface::VERBOSITY_VERBOSE);
9797

9898
$previewsToDeleteCount++;
9999

lib/private/Files/Cache/LocalRootScanner.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@
88
*/
99
namespace OC\Files\Cache;
1010

11+
use OCP\IConfig;
12+
use OCP\Server;
13+
1114
class LocalRootScanner extends Scanner {
15+
private string $previewFolder;
16+
17+
public function __construct(\OC\Files\Storage\Storage $storage) {
18+
parent::__construct($storage);
19+
$config = Server::get(IConfig::class);
20+
$this->previewFolder = 'appdata_' . $config->getSystemValueString('instanceid', '') . '/preview';
21+
}
22+
1223
public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true, $data = null) {
1324
if ($this->shouldScanPath($file)) {
1425
return parent::scanFile($file, $reuseExisting, $parentId, $cacheData, $lock, $data);
@@ -27,6 +38,9 @@ public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $loc
2738

2839
private function shouldScanPath(string $path): bool {
2940
$path = trim($path, '/');
41+
if (str_starts_with($path, $this->previewFolder)) {
42+
return false;
43+
}
3044
return $path === '' || str_starts_with($path, 'appdata_') || str_starts_with($path, '__groupfolders');
3145
}
3246
}

lib/private/Files/Cache/Scanner.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
use OC\Files\Storage\Wrapper\Encryption;
1212
use OC\Files\Storage\Wrapper\Jail;
1313
use OC\Hooks\BasicEmitter;
14-
use OC\SystemConfig;
1514
use OCP\Files\Cache\IScanner;
1615
use OCP\Files\ForbiddenException;
1716
use OCP\Files\NotFoundException;
1817
use OCP\Files\Storage\ILockingStorage;
1918
use OCP\Files\Storage\IReliableEtagStorage;
19+
use OCP\IConfig;
2020
use OCP\IDBConnection;
2121
use OCP\Lock\ILockingProvider;
22+
use OCP\Server;
2223
use Psr\Log\LoggerInterface;
2324

2425
/**
@@ -65,19 +66,15 @@ class Scanner extends BasicEmitter implements IScanner {
6566

6667
protected IDBConnection $connection;
6768

68-
private string $previewFolder;
69-
7069
public function __construct(\OC\Files\Storage\Storage $storage) {
7170
$this->storage = $storage;
7271
$this->storageId = $this->storage->getId();
7372
$this->cache = $storage->getCache();
74-
/** @var SystemConfig $config */
75-
$config = \OC::$server->get(SystemConfig::class);
76-
$this->cacheActive = !$config->getValue('filesystem_cache_readonly', false);
77-
$this->useTransactions = !$config->getValue('filescanner_no_transactions', false);
78-
$this->lockingProvider = \OC::$server->get(ILockingProvider::class);
79-
$this->connection = \OC::$server->get(IDBConnection::class);
80-
$this->previewFolder = 'appdata_' . $config->getValue('instanceid', '') . '/preview';
73+
$config = Server::get(IConfig::class);
74+
$this->cacheActive = !$config->getSystemValueBool('filesystem_cache_readonly', false);
75+
$this->useTransactions = !$config->getSystemValueBool('filescanner_no_transactions', false);
76+
$this->lockingProvider = Server::get(ILockingProvider::class);
77+
$this->connection = Server::get(IDBConnection::class);
8178
}
8279

8380
/**
@@ -415,11 +412,6 @@ protected function scanChildren(string $path, $recursive, int $reuse, int $folde
415412
$size = 0;
416413
$childQueue = $this->handleChildren($path, $recursive, $reuse, $folderId, $lock, $size, $etagChanged);
417414

418-
if (str_starts_with($path, $this->previewFolder)) {
419-
// Preview scanning is handled in LocalPreviewStorage
420-
return 0;
421-
}
422-
423415
foreach ($childQueue as $child => [$childId, $childSize]) {
424416
// "etag changed" propagates up, but not down, so we pass `false` to the children even if we already know that the etag of the current folder changed
425417
$childEtagChanged = false;

lib/private/Preview/Db/Preview.php

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
use OCP\AppFramework\Db\Entity;
1414
use OCP\DB\Types;
15-
use OCP\IPreview;
15+
use OCP\Files\IMimeTypeDetector;
16+
use OCP\Files\IMimeTypeLoader;
17+
use OCP\Server;
1618

1719
/**
1820
* Preview entity mapped to the oc_previews and oc_preview_locations table.
@@ -91,42 +93,39 @@ public function __construct() {
9193
$this->addType('version', Types::BIGINT);
9294
}
9395

94-
public static function fromPath(string $path): Preview {
96+
public static function fromPath(string $path, IMimeTypeDetector $mimeTypeDetector, IMimeTypeLoader $mimeTypeLoader): Preview|false {
9597
$preview = new self();
9698
$preview->setFileId((int)basename(dirname($path)));
9799

98100
$fileName = pathinfo($path, PATHINFO_FILENAME) . '.' . pathinfo($path, PATHINFO_EXTENSION);
101+
$ok = preg_match('/(([0-9]+)-)?([0-9]+)-([0-9]+)(-(max))?(-(crop))?\.([a-z]{3,4})/', $fileName, $matches);
99102

100-
[0 => $baseName, 1 => $extension] = explode('.', $fileName);
101-
$preview->setMimetype(match ($extension) {
102-
'jpg' | 'jpeg' => IPreview::MIMETYPE_JPEG,
103-
'png' => IPreview::MIMETYPE_PNG,
104-
'gif' => IPreview::MIMETYPE_GIF,
105-
'webp' => IPreview::MIMETYPE_WEBP,
106-
default => IPreview::MIMETYPE_JPEG,
107-
});
108-
$nameSplit = explode('-', $baseName);
109-
110-
$offset = 0;
111-
$preview->setVersion(null);
112-
if (count($nameSplit) === 4 || (count($nameSplit) === 3 && is_numeric($nameSplit[2]))) {
113-
$offset = 1;
114-
$preview->setVersion((int)$nameSplit[0]);
103+
if ($ok !== 1) {
104+
return false;
115105
}
116106

117-
$preview->setWidth((int)$nameSplit[$offset + 0]);
118-
$preview->setHeight((int)$nameSplit[$offset + 1]);
107+
[
108+
2 => $version,
109+
3 => $width,
110+
4 => $height,
111+
6 => $crop,
112+
8 => $max,
113+
] = $matches;
119114

120-
$preview->setCropped(false);
121-
$preview->setMax(false);
122-
if (isset($nameSplit[$offset + 2])) {
123-
$preview->setCropped($nameSplit[$offset + 2] === 'crop');
124-
$preview->setMax($nameSplit[$offset + 2] === 'max');
115+
$preview->setMimetype($mimeTypeLoader->getId($mimeTypeDetector->detectPath($fileName)));
116+
117+
$preview->setWidth((int)$width);
118+
$preview->setHeight((int)$height);
119+
$preview->setCropped($crop === 'crop');
120+
$preview->setMax($max === 'max');
121+
122+
if (!empty($version)) {
123+
$preview->setVersion((int)$version);
125124
}
126125
return $preview;
127126
}
128127

129-
public function getName(): string {
128+
public function getName(IMimeTypeLoader $mimeTypeLoader): string {
130129
$path = ($this->getVersion() > -1 ? $this->getVersion() . '-' : '') . $this->getWidth() . '-' . $this->getHeight();
131130
if ($this->isCropped()) {
132131
$path .= '-crop';
@@ -135,27 +134,23 @@ public function getName(): string {
135134
$path .= '-max';
136135
}
137136

138-
$ext = $this->getExtension();
137+
$ext = $this->getExtension($mimeTypeLoader);
139138
$path .= '.' . $ext;
140139
return $path;
141140
}
142141

143-
public function getMimetypeValue(): string {
144-
return match ($this->mimetype) {
145-
IPreview::MIMETYPE_JPEG => 'image/jpeg',
146-
IPreview::MIMETYPE_PNG => 'image/png',
147-
IPreview::MIMETYPE_WEBP => 'image/webp',
148-
IPreview::MIMETYPE_GIF => 'image/gif',
142+
public function getExtension(IMimeTypeLoader $mimeTypeLoader): string {
143+
return match ($this->getMimetypeValue($mimeTypeLoader)) {
144+
'image/png' => 'png',
145+
'image/gif' => 'gif',
146+
'image/jpeg' => 'jpg',
147+
'image/webp' => 'webp',
148+
default => 'png',
149149
};
150150
}
151151

152-
public function getExtension(): string {
153-
return match ($this->mimetype) {
154-
IPreview::MIMETYPE_JPEG => 'jpg',
155-
IPreview::MIMETYPE_PNG => 'png',
156-
IPreview::MIMETYPE_WEBP => 'webp',
157-
IPreview::MIMETYPE_GIF => 'gif',
158-
};
152+
public function getMimetypeValue(IMimeTypeLoader $mimeTypeLoader): string {
153+
return $mimeTypeLoader->getMimetypeById($this->mimetype) ?? 'image/jpeg';
159154
}
160155

161156
public function setBucketName(string $bucketName): void {

lib/private/Preview/Db/PreviewMapper.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCP\AppFramework\Db\QBMapper;
1414
use OCP\DB\Exception;
1515
use OCP\DB\QueryBuilder\IQueryBuilder;
16+
use OCP\Files\IMimeTypeLoader;
1617
use OCP\IDBConnection;
1718
use OCP\IPreview;
1819

@@ -24,7 +25,9 @@ class PreviewMapper extends QBMapper {
2425
private const TABLE_NAME = 'previews';
2526
private const LOCATION_TABLE_NAME = 'preview_locations';
2627

27-
public function __construct(IDBConnection $db) {
28+
public function __construct(
29+
IDBConnection $db,
30+
) {
2831
parent::__construct($db, self::TABLE_NAME, Preview::class);
2932
}
3033

@@ -57,23 +60,6 @@ public function getAvailablePreviews(array $fileIds): array {
5760
return $previews;
5861
}
5962

60-
public function getPreview(int $fileId, int $width, int $height, string $mode, int $mimetype = IPreview::MIMETYPE_JPEG): ?Preview {
61-
$selectQb = $this->db->getQueryBuilder();
62-
$this->joinLocation($selectQb)
63-
->where(
64-
$selectQb->expr()->eq('file_id', $selectQb->createNamedParameter($fileId)),
65-
$selectQb->expr()->eq('width', $selectQb->createNamedParameter($width)),
66-
$selectQb->expr()->eq('height', $selectQb->createNamedParameter($height)),
67-
$selectQb->expr()->eq('mode', $selectQb->createNamedParameter($mode)),
68-
$selectQb->expr()->eq('mimetype', $selectQb->createNamedParameter($mimetype)),
69-
);
70-
try {
71-
return $this->findEntity($selectQb);
72-
} catch (DoesNotExistException) {
73-
return null;
74-
}
75-
}
76-
7763
/**
7864
* @return \Generator<Preview>
7965
*/

0 commit comments

Comments
 (0)