mirror of
https://github.com/nextcloud/server.git
synced 2026-04-15 22:11:17 -04:00
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>
43 lines
1.5 KiB
PHP
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();
|
|
}
|
|
}
|