nextcloud/lib/private/Preview/Storage/StorageFactory.php
Carl Schwan bfc7d5dd9f feat(preview): Implement scanning for previews
This work similarly to the move preview job to migrate the previews to
the new DB table and also reuse some code.

So when we are finding files in appdata/preview, try adding them to the
oc_previews table and delete them from the oc_filecache table.

Signed-off-by: Carl Schwan <carl.schwan@nextcloud.com>
2025-10-06 13:37:15 +02:00

63 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
/*
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH
* SPDX-FileContributor: Carl Schwan
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Preview\Storage;
use OC\Files\ObjectStore\PrimaryObjectStoreConfig;
use OC\Files\SimpleFS\SimpleFile;
use OC\Preview\Db\Preview;
use OC\Preview\Db\PreviewMapper;
use OCP\IConfig;
use OCP\Server;
class StorageFactory implements IPreviewStorage {
private ?IPreviewStorage $backend = null;
public function __construct(
private readonly PrimaryObjectStoreConfig $objectStoreConfig,
private readonly IConfig $config,
private readonly PreviewMapper $previewMapper,
) {
}
public function writePreview(Preview $preview, mixed $stream): false|int {
return $this->getBackend()->writePreview($preview, $stream);
}
public function readPreview(Preview $preview): mixed {
return $this->getBackend()->readPreview($preview);
}
public function deletePreview(Preview $preview): void {
$this->getBackend()->deletePreview($preview);
}
private function getBackend(): IPreviewStorage {
if ($this->backend) {
return $this->backend;
}
if ($this->objectStoreConfig->hasObjectStore()) {
$this->backend = new ObjectStorePreviewStorage($this->objectStoreConfig, $this->config, $this->previewMapper);
} else {
$this->backend = Server::get(LocalPreviewStorage::class);
}
return $this->backend;
}
public function migratePreview(Preview $preview, SimpleFile $file): void {
$this->getBackend()->migratePreview($preview, $file);
}
public function scan(): int {
return $this->getBackend()->scan();
}
}