Add featured thumbnail config and ArtworkFeaturedImagePath helper

This commit is contained in:
2026-05-06 18:54:18 +02:00
parent a3cfc6c17f
commit ee24111d59
2 changed files with 117 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace App\Support;
use App\Models\Artwork;
final class ArtworkFeaturedImagePath
{
/**
* @return array<string, array<string, int|string>>
*/
public function variants(): array
{
/** @var array<string, array<string, int|string>> $variants */
$variants = config('uploads.featured_variants', []);
return $variants;
}
/**
* @return list<string>
*/
public function variantNames(): array
{
return array_keys($this->variants());
}
public function defaultVariant(): string
{
return 'desktop';
}
public function featuredPrefix(): string
{
return trim((string) config('uploads.featured_prefix', 'artworks/featured'), '/');
}
public function objectPath(Artwork $artwork, string $variant): string
{
$hash = $this->normalizedHash($artwork);
$variantName = $this->normalizeVariant($variant);
return sprintf(
'%s/%s/%s/%s/%s.webp',
$this->featuredPrefix(),
$variantName,
substr($hash, 0, 2),
substr($hash, 2, 2),
$hash,
);
}
public function url(Artwork $artwork, string $variant): string
{
return rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/').'/'.$this->objectPath($artwork, $variant);
}
/**
* @return list<string>
*/
public function preferredVariantOrder(string $variant): array
{
$variantName = $this->normalizeVariant($variant);
$orders = [
'xs' => ['xs', 'mobile_sm', 'mobile', 'tablet', 'desktop', 'desktop_xl'],
'mobile_sm' => ['mobile_sm', 'mobile', 'tablet', 'desktop', 'desktop_xl'],
'mobile' => ['mobile', 'mobile_sm', 'xs', 'tablet', 'desktop', 'desktop_xl'],
'tablet' => ['tablet', 'desktop', 'desktop_xl', 'mobile', 'mobile_sm', 'xs'],
'desktop' => ['desktop', 'desktop_xl', 'tablet', 'mobile', 'mobile_sm', 'xs'],
'desktop_xl' => ['desktop_xl', 'desktop', 'tablet', 'mobile', 'mobile_sm', 'xs'],
];
return $orders[$variantName] ?? [$this->defaultVariant()];
}
/**
* @return array<string, int|string>|null
*/
public function variantConfig(string $variant): ?array
{
return $this->variants()[$this->normalizeVariant($variant)] ?? null;
}
public function normalizeVariant(string $variant): string
{
return array_key_exists($variant, $this->variants()) ? $variant : $this->defaultVariant();
}
private function normalizedHash(Artwork $artwork): string
{
$hash = trim((string) $artwork->hash);
if ($hash === '') {
throw new \InvalidArgumentException('Artwork hash is required for featured thumbnail paths.');
}
return $hash;
}
}