362 lines
12 KiB
PHP
362 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Media;
|
|
|
|
use App\Domain\Catalog\Composition;
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\ContentElementVersion;
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Symfony\Component\Process\ExecutableFinder;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class ThumbnailDeriver
|
|
{
|
|
public function forElement(ContentElement $element): void
|
|
{
|
|
$element->refresh();
|
|
$chosen = $element->thumbnail_source;
|
|
|
|
if ($chosen === 'eigen' || (is_string($chosen) && str_starts_with($chosen, 'frame:'))) {
|
|
return;
|
|
}
|
|
|
|
$version = $element->versions()->orderByDesc('number')->first();
|
|
$file = is_array($version?->metadata) ? ($version->metadata['file'] ?? null) : null;
|
|
|
|
if (! $version instanceof ContentElementVersion || ! is_string($file) || $file === '') {
|
|
$this->fail($element, 'Für dieses Medium liegt noch keine Datei vor.');
|
|
|
|
return;
|
|
}
|
|
|
|
if (in_array($element->output_class, ['film', 'audio'], true)) {
|
|
$this->frame($element, $version, $file);
|
|
|
|
return;
|
|
}
|
|
|
|
$this->still($element, Storage::disk('media_derived'), $file, 'element-'.$element->id);
|
|
$this->rememberElement($element, 'datei');
|
|
}
|
|
|
|
public function forElementUpload(ContentElement $element, string $incoming): void
|
|
{
|
|
if (! str_starts_with($incoming, 'vorschau/') || str_contains($incoming, '..')) {
|
|
$this->fail($element, 'Das Vorschaubild wurde nicht übernommen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$this->still($element, Storage::disk('cms_incoming'), $incoming, 'element-'.$element->id);
|
|
$this->rememberElement($element, 'eigen');
|
|
}
|
|
|
|
public function forWebsiteImage(ContentElement $element, string $incoming): void
|
|
{
|
|
if ($element->output_class !== 'bild' || ! $this->incomingIsSafe($incoming)) {
|
|
$this->fail($element, 'Das Bild für die Website wurde nicht übernommen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$this->still($element, Storage::disk('cms_incoming'), $incoming, 'element-'.$element->id);
|
|
$this->rememberElement($element, 'eigen');
|
|
}
|
|
|
|
public function forElementFrame(ContentElement $element, ContentElementVersion $version, float $seconds): void
|
|
{
|
|
if ($version->content_element_id !== $element->id || $element->output_class !== 'film') {
|
|
$this->fail($element, 'Aus dieser Filmstelle ließ sich kein Vorschaubild erzeugen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$bytes = $this->jpegAtVersion($version, $seconds);
|
|
|
|
if (! is_string($bytes)) {
|
|
$this->fail($element, 'Aus dieser Filmstelle ließ sich kein Vorschaubild erzeugen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$name = 'element-'.$element->id;
|
|
$path = 'vorschau/'.$name.'.jpg';
|
|
Storage::disk('media_derived')->put($path, $bytes);
|
|
$this->still($element, Storage::disk('media_derived'), $path, $name);
|
|
$this->rememberElement($element, 'frame:'.$version->id.'@'.$this->secondText($seconds));
|
|
}
|
|
|
|
public function forCompositionUpload(Composition $composition, string $incoming): void
|
|
{
|
|
if (! str_starts_with($incoming, 'vorschau/') || str_contains($incoming, '..')) {
|
|
$this->failComposition($composition, 'Das Vorschaubild wurde nicht übernommen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$this->still($composition, Storage::disk('cms_incoming'), $incoming, 'komposition-'.$composition->id);
|
|
$this->rememberChoice($composition, 'eigen');
|
|
}
|
|
|
|
public function forCompositionFromElement(Composition $composition, ContentElement $element): void
|
|
{
|
|
$path = $element->thumbnail_path;
|
|
|
|
if (! is_string($path) || ! str_starts_with($path, 'vorschau/') || str_contains($path, '..')) {
|
|
$this->failComposition($composition, 'Für dieses Medium liegt noch kein Vorschaubild vor.');
|
|
|
|
return;
|
|
}
|
|
|
|
$this->still($composition, Storage::disk('media_derived'), $path, 'komposition-'.$composition->id);
|
|
$this->rememberChoice($composition, 'element:'.$element->id);
|
|
}
|
|
|
|
public function forCompositionFrame(Composition $composition, ContentElementVersion $version, float $seconds): void
|
|
{
|
|
$version->loadMissing('element');
|
|
|
|
if ($version->element?->output_class !== 'film') {
|
|
$this->failComposition($composition, 'Aus dieser Filmstelle ließ sich kein Titelbild erzeugen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$bytes = $this->jpegAtVersion($version, $seconds);
|
|
|
|
if (! is_string($bytes)) {
|
|
$this->failComposition($composition, 'Aus dieser Filmstelle ließ sich kein Titelbild erzeugen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$name = 'komposition-'.$composition->id;
|
|
$path = 'vorschau/'.$name.'.jpg';
|
|
Storage::disk('media_derived')->put($path, $bytes);
|
|
$this->still($composition, Storage::disk('media_derived'), $path, $name);
|
|
$this->rememberChoice($composition, 'frame:'.$version->id.'@'.$this->secondText($seconds));
|
|
}
|
|
|
|
public function frameInput(ContentElementVersion $version): ?string
|
|
{
|
|
$proxy = MediaRendition::query()
|
|
->where('content_element_version_id', $version->id)
|
|
->where('role', 'variant')
|
|
->where('label', '480')
|
|
->value('path');
|
|
|
|
if (is_string($proxy) && $this->readableDerived($proxy, 'ableitungen/')) {
|
|
return $proxy;
|
|
}
|
|
|
|
$file = is_array($version->metadata) ? ($version->metadata['file'] ?? null) : null;
|
|
|
|
return $this->readableDerived($file, 'inhalte/') ? $file : null;
|
|
}
|
|
|
|
public function jpegAtVersion(ContentElementVersion $version, float $seconds): ?string
|
|
{
|
|
if ($seconds < 0 || $seconds > 7200) {
|
|
return null;
|
|
}
|
|
|
|
$file = $this->frameInput($version);
|
|
|
|
if ($file === null) {
|
|
return null;
|
|
}
|
|
|
|
$disk = Storage::disk('media_derived');
|
|
$binary = (new ExecutableFinder)->find('ffmpeg');
|
|
|
|
if ($binary === null) {
|
|
return null;
|
|
}
|
|
|
|
$process = new Process([
|
|
$binary,
|
|
'-hide_banner',
|
|
'-loglevel',
|
|
'error',
|
|
'-ss',
|
|
$this->secondText($seconds),
|
|
'-i',
|
|
$disk->path($file),
|
|
'-frames:v',
|
|
'1',
|
|
'-vf',
|
|
'scale=640:-2',
|
|
'-f',
|
|
'image2pipe',
|
|
'-vcodec',
|
|
'mjpeg',
|
|
'pipe:1',
|
|
]);
|
|
$process->setTimeout(15);
|
|
$process->run();
|
|
$bytes = $process->getOutput();
|
|
|
|
if (! $process->isSuccessful() || ! str_starts_with($bytes, "\xFF\xD8")) {
|
|
return null;
|
|
}
|
|
|
|
return $bytes;
|
|
}
|
|
|
|
private function readableDerived(mixed $path, string $prefix): bool
|
|
{
|
|
if (! is_string($path) || $path === '' || str_contains($path, '..') || ! str_starts_with($path, $prefix)) {
|
|
return false;
|
|
}
|
|
|
|
return Storage::disk('media_derived')->exists($path);
|
|
}
|
|
|
|
private function incomingIsSafe(string $incoming): bool
|
|
{
|
|
return (str_starts_with($incoming, 'cms/') || str_starts_with($incoming, 'vorschau/'))
|
|
&& ! str_contains($incoming, '..')
|
|
&& preg_match('#^(cms|vorschau)/[A-Za-z0-9._\-/]+$#', $incoming) === 1
|
|
&& Storage::disk('cms_incoming')->exists($incoming);
|
|
}
|
|
|
|
private function secondText(float $seconds): string
|
|
{
|
|
$seconds = max(0, min(7200, $seconds));
|
|
$text = number_format($seconds, 3, '.', '');
|
|
|
|
return rtrim(rtrim($text, '0'), '.') ?: '0';
|
|
}
|
|
|
|
private function frame(ContentElement $element, ContentElementVersion $version, string $file): void
|
|
{
|
|
$binary = (new ExecutableFinder)->find('ffmpeg');
|
|
$disk = Storage::disk('media_derived');
|
|
|
|
if ($binary === null || ! $disk->exists($file)) {
|
|
$this->fail($element, 'Für dieses Medium liegt noch kein abgeleitetes Vorschaubild vor.');
|
|
|
|
return;
|
|
}
|
|
|
|
$target = 'vorschau/element-'.$element->id.'.jpg';
|
|
$disk->makeDirectory('vorschau');
|
|
$process = new Process([
|
|
$binary,
|
|
'-y',
|
|
'-ss',
|
|
'0',
|
|
'-i',
|
|
$disk->path($file),
|
|
'-frames:v',
|
|
'1',
|
|
$disk->path($target),
|
|
]);
|
|
$process->setTimeout(120);
|
|
$process->run();
|
|
|
|
if (! $process->isSuccessful() || ! $disk->exists($target)) {
|
|
$this->fail($element, 'Aus der Filmdatei ließ sich kein Vorschaubild erzeugen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$element->thumbnail_path = $target;
|
|
if (! filled($element->thumbnail_alt)) {
|
|
$element->thumbnail_alt = $element->working_title;
|
|
}
|
|
$element->thumbnail_error = null;
|
|
if ($element->output_class === 'film') {
|
|
$element->thumbnail_source = 'frame:'.$version->id.'@0';
|
|
}
|
|
$element->save();
|
|
}
|
|
|
|
private function still(ContentElement|Composition $subject, Filesystem $disk, string $file, string $name): void
|
|
{
|
|
if (! $disk->exists($file)) {
|
|
$this->failSubject($subject, 'Die Bilddatei wurde nicht gefunden.');
|
|
|
|
return;
|
|
}
|
|
|
|
$image = @imagecreatefromstring($disk->get($file));
|
|
|
|
if ($image === false) {
|
|
$this->failSubject($subject, 'Die Bilddatei ließ sich nicht lesen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$width = imagesx($image);
|
|
$height = imagesy($image);
|
|
$targetWidth = 640;
|
|
$targetHeight = (int) max(1, round($height * ($targetWidth / max(1, $width))));
|
|
$canvas = imagecreatetruecolor($targetWidth, $targetHeight);
|
|
imagecopyresampled($canvas, $image, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
|
|
ob_start();
|
|
imagejpeg($canvas, null, 82);
|
|
$bytes = ob_get_clean();
|
|
imagedestroy($image);
|
|
imagedestroy($canvas);
|
|
|
|
if (! is_string($bytes) || $bytes === '') {
|
|
$this->failSubject($subject, 'Das Vorschaubild ließ sich nicht schreiben.');
|
|
|
|
return;
|
|
}
|
|
|
|
$path = 'vorschau/'.$name.'.jpg';
|
|
Storage::disk('media_derived')->put($path, $bytes);
|
|
$subject->thumbnail_path = $path;
|
|
|
|
if (! filled($subject->thumbnail_alt)) {
|
|
$subject->thumbnail_alt = $subject instanceof Composition ? $subject->designation : $subject->working_title;
|
|
}
|
|
|
|
$subject->thumbnail_error = null;
|
|
$subject->save();
|
|
}
|
|
|
|
private function rememberElement(ContentElement $element, string $source): void
|
|
{
|
|
$element->refresh();
|
|
|
|
if ($element->thumbnail_error !== null || ! filled($element->thumbnail_path)) {
|
|
return;
|
|
}
|
|
|
|
$element->thumbnail_source = $source;
|
|
$element->save();
|
|
}
|
|
|
|
private function rememberChoice(Composition $composition, string $source): void
|
|
{
|
|
$composition->refresh();
|
|
|
|
if ($composition->thumbnail_error !== null || ! filled($composition->thumbnail_path)) {
|
|
return;
|
|
}
|
|
|
|
$composition->thumbnail_source = $source;
|
|
$composition->save();
|
|
}
|
|
|
|
private function fail(ContentElement $element, string $reason): void
|
|
{
|
|
$this->failSubject($element, $reason);
|
|
}
|
|
|
|
private function failComposition(Composition $composition, string $reason): void
|
|
{
|
|
$this->failSubject($composition, $reason);
|
|
}
|
|
|
|
private function failSubject(ContentElement|Composition $subject, string $reason): void
|
|
{
|
|
$subject->thumbnail_error = $reason;
|
|
$subject->save();
|
|
}
|
|
}
|