nextcloud/lib/private/Preview/Storage/LocalPreviewStorage.php
Carl Schwan 18fbacdd8d perf(preview): Split preview data to new table
The new oc_previews table is optimized for storing previews and should
decrease significantly the space taken by previews in the filecache
table.

This attend to reuse the IObjectStore abstraction over S3/Swift/Azure
but currently only support one single bucket configuration.

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

43 lines
1.5 KiB
PHP

<?php
namespace OC\Preview\Storage;
use Icewind\Streams\CountWrapper;
use OC\Files\Storage\Local;
use OC\Preview\Db\Preview;
use OCP\Files\Storage\IStorage;
use OCP\IPreview;
class LocalPreviewStorage implements IPreviewStorage {
private const PREVIEW_DIRECTORY = "__preview";
public function __construct(private readonly string $rootFolder) {
}
public function writePreview(Preview $preview, $stream): false|int {
$previewPath = $this->constructPath($preview);
['basename' => $basename, 'dirname' => $dirname] = pathinfo($previewPath);
$currentDir = $this->rootFolder . DIRECTORY_SEPARATOR . self::PREVIEW_DIRECTORY;
mkdir($currentDir);
foreach (explode('/', $dirname) as $suffix) {
$currentDir .= "/$suffix";
mkdir($currentDir);
}
$file = @fopen($this->rootFolder . DIRECTORY_SEPARATOR . self::PREVIEW_DIRECTORY . DIRECTORY_SEPARATOR . $previewPath, "w");
return fwrite($file, $stream);
}
public function readPreview(Preview $preview) {
$previewPath = $this->constructPath($preview);
return @fopen($this->rootFolder . DIRECTORY_SEPARATOR . self::PREVIEW_DIRECTORY . DIRECTORY_SEPARATOR . $previewPath, "r");
}
public function deletePreview(Preview $preview) {
$previewPath = $this->constructPath($preview);
@unlink($this->rootFolder . DIRECTORY_SEPARATOR . self::PREVIEW_DIRECTORY . DIRECTORY_SEPARATOR . $previewPath);
}
private function constructPath(Preview $preview): string {
return implode('/', str_split(substr(md5((string)$preview->getFileId()), 0, 7))) . '/' . $preview->getFileId() . '/' . $preview->getName();
}
}