nextcloud/lib/private/Preview/Storage/ObjectStorePreviewStorage.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

50 lines
1.3 KiB
PHP

<?php
namespace OC\Preview\Storage;
use Icewind\Streams\CountWrapper;
use OC\Preview\Db\Preview;
use OCP\Files\ObjectStore\IObjectStore;
class ObjectStorePreviewStorage implements IPreviewStorage {
private string $objectPrefix = 'urn:oid:preview:';
/**
* @param array{objectPrefix: ?string} $parameters
*/
public function __construct(private IObjectStore $objectStore, array $parameters) {
if (isset($parameters['objectPrefix'])) {
$this->objectPrefix = $parameters['objectPrefix'] . 'preview:';
}
}
public function writePreview(Preview $preview, $stream): false|int {
if (!is_resource($stream)) {
$fh = fopen('php://temp', 'w+');
fwrite($fh, $stream);
rewind($fh);
$stream = $fh;
}
$size = 0;
$countStream = CountWrapper::wrap($stream, function (int $writtenSize) use (&$size): void {
$size = $writtenSize;
});
$this->objectStore->writeObject($this->constructUrn($preview), $countStream);
return $size;
}
public function readPreview(Preview $preview) {
return $this->objectStore->readObject($this->constructUrn($preview));
}
public function deletePreview(Preview $preview) {
return $this->objectStore->deleteObject($this->constructUrn($preview));
}
private function constructUrn(Preview $preview): string {
return $this->objectPrefix . $preview->getId();
}
}