feat: implement preview manager
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXF\Preview;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class PreviewException extends RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace KTXF\Preview;
|
||||
|
||||
final class PreviewGenerationException extends PreviewException
|
||||
use RuntimeException;
|
||||
|
||||
final class PreviewGenerationException extends RuntimeException
|
||||
{
|
||||
}
|
||||
|
||||
@@ -11,47 +11,22 @@ final readonly class PreviewRequest
|
||||
public string $preferredMimeType;
|
||||
|
||||
public function __construct(
|
||||
public string $variant,
|
||||
public int $maxWidth,
|
||||
public int $maxHeight,
|
||||
string $preferredMimeType,
|
||||
public int $quality,
|
||||
public ?int $page = null,
|
||||
public ?float $timestamp = null,
|
||||
public int $maxSourceSize = 26214400,
|
||||
) {
|
||||
if (preg_match('/^[a-z][a-z0-9-]*$/', $this->variant) !== 1) {
|
||||
throw new InvalidArgumentException('Preview variant must use lowercase letters, numbers, and hyphens');
|
||||
}
|
||||
if ($this->maxWidth < 1 || $this->maxHeight < 1) {
|
||||
throw new InvalidArgumentException('Preview dimensions must be positive');
|
||||
}
|
||||
if ($this->quality < 1 || $this->quality > 100) {
|
||||
throw new InvalidArgumentException('Preview quality must be between 1 and 100');
|
||||
}
|
||||
if ($this->page !== null && $this->page < 1) {
|
||||
throw new InvalidArgumentException('Preview page numbers start at 1');
|
||||
}
|
||||
if ($this->timestamp !== null && $this->timestamp < 0) {
|
||||
throw new InvalidArgumentException('Preview timestamps cannot be negative');
|
||||
if ($this->maxSourceSize < 1) {
|
||||
throw new InvalidArgumentException('Preview maximum source size must be positive');
|
||||
}
|
||||
$this->preferredMimeType = MimeType::normalize($preferredMimeType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fixed-order scalar representation for cache-key generation.
|
||||
*
|
||||
* @return array{variant:string,maxWidth:int,maxHeight:int,preferredMimeType:string,quality:int,page:?int,timestamp:?float}
|
||||
*/
|
||||
public function normalized(): array
|
||||
{
|
||||
return [
|
||||
'variant' => $this->variant,
|
||||
'maxWidth' => $this->maxWidth,
|
||||
'maxHeight' => $this->maxHeight,
|
||||
'preferredMimeType' => $this->preferredMimeType,
|
||||
'quality' => $this->quality,
|
||||
'page' => $this->page,
|
||||
'timestamp' => $this->timestamp,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,17 +22,16 @@ final readonly class PreviewSource
|
||||
string $mimeType,
|
||||
public ?int $size,
|
||||
public Closure $resourceFactory,
|
||||
public ?string $selector = null,
|
||||
) {
|
||||
if ($this->sourceType === '' || $this->identity === '' || $this->signature === '') {
|
||||
throw new InvalidArgumentException('Preview sources require a type, identity, and content signature');
|
||||
if (preg_match('/^[a-z][a-z0-9-]*$/', $this->sourceType) !== 1) {
|
||||
throw new InvalidArgumentException('Preview source type must use lowercase letters, numbers, and hyphens');
|
||||
}
|
||||
if ($this->identity === '' || $this->signature === '') {
|
||||
throw new InvalidArgumentException('Preview sources require an identity and content signature');
|
||||
}
|
||||
if ($this->size !== null && $this->size < 0) {
|
||||
throw new InvalidArgumentException('Preview source size cannot be negative');
|
||||
}
|
||||
if ($this->selector === '') {
|
||||
throw new InvalidArgumentException('Preview source selectors must be null or non-empty');
|
||||
}
|
||||
$this->mimeType = MimeType::normalize($mimeType);
|
||||
}
|
||||
|
||||
@@ -40,7 +39,7 @@ final readonly class PreviewSource
|
||||
{
|
||||
$resource = ($this->resourceFactory)();
|
||||
if (!$resource instanceof BinaryResource) {
|
||||
throw new PreviewException('Preview source factory did not return a BinaryResource');
|
||||
throw new PreviewGenerationException('Preview source factory did not return a BinaryResource');
|
||||
}
|
||||
return $resource;
|
||||
}
|
||||
|
||||
@@ -11,17 +11,9 @@ use KTXF\Resource\Provider\ProviderInterface;
|
||||
|
||||
interface PreviewProviderInterface extends ProviderInterface
|
||||
{
|
||||
/** @return list<string> Exact MIME types or type wildcards such as image/*. */
|
||||
public function supportedMimeTypes(): array;
|
||||
|
||||
/** @return list<string> */
|
||||
public function outputMimeTypes(): array;
|
||||
|
||||
public function supports(string $sourceMimeType, PreviewRequest $request): bool;
|
||||
|
||||
public function generate(PreviewSource $source, PreviewRequest $request): PreviewResult;
|
||||
|
||||
public function generatorVersion(): string;
|
||||
|
||||
public function priority(): int;
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXF\Preview;
|
||||
|
||||
final class UnsupportedPreviewException extends PreviewException
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user