b2in/app/Services/DisplayMediaService.php
Kevin Adametz 193d37b1ce Display CMS: Upload-Fehler abfangen und kaputte Bilder im Player überspringen.
Mediathek-Uploads legen keinen Datensatz mehr an, wenn der Datei-Write
fehlschlägt (verhindert 404-Einträge auf den Displays). Der Player
überspringt Bilder mit Ladefehler nach 2s statt Schwarzbild zu zeigen.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-07 08:57:31 +00:00

245 lines
7.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\DisplayMedia;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RuntimeException;
use Symfony\Component\Process\Process;
class DisplayMediaService
{
/**
* Store an uploaded file and create a DisplayMedia record.
*
* @throws RuntimeException When the file cannot be written to storage.
*/
public function storeUpload(UploadedFile $file, ?string $collection = null): DisplayMedia
{
$filename = $file->getClientOriginalName();
$extension = strtolower($file->getClientOriginalExtension());
$storageName = Str::uuid().'.'.$extension;
$datePath = now()->format('Y/m');
$relativePath = "display-media/{$datePath}/{$storageName}";
$storedPath = Storage::disk('public')->putFileAs("display-media/{$datePath}", $file, $storageName);
if ($storedPath === false || ! Storage::disk('public')->exists($relativePath)) {
throw new RuntimeException("Die Datei \"{$filename}\" konnte nicht im Storage gespeichert werden. Bitte Schreibrechte und Speicherplatz prüfen.");
}
$type = in_array($extension, ['mp4', 'webm', 'mov']) ? 'video' : 'image';
$metadata = [];
if ($type === 'image') {
$dimensions = @getimagesize($file->getRealPath());
if ($dimensions) {
$metadata['width'] = $dimensions[0];
$metadata['height'] = $dimensions[1];
}
} else {
$dimensions = $this->readVideoDimensions($file->getRealPath());
if ($dimensions !== null) {
$metadata['width'] = $dimensions['width'];
$metadata['height'] = $dimensions['height'];
}
}
$media = DisplayMedia::create([
'filename' => $filename,
'disk' => 'public',
'path' => $relativePath,
'source_type' => 'upload',
'type' => $type,
'mime_type' => $file->getMimeType(),
'file_size' => $file->getSize(),
'collection' => $collection,
'metadata' => ! empty($metadata) ? $metadata : null,
]);
if ($type === 'video') {
$thumbnailPath = $this->generateVideoThumbnail($media);
if ($thumbnailPath !== null) {
$media->update(['thumbnail_path' => $thumbnailPath]);
}
}
return $media;
}
/**
* Generate a poster frame for an uploaded video using ffmpeg.
*
* Returns the relative thumbnail path on the media's disk, or null when
* generation is not possible (e.g. ffmpeg missing or an unreadable file).
*/
public function generateVideoThumbnail(DisplayMedia $media): ?string
{
if (! $media->isVideo() || ! $media->isUpload() || ! $media->path) {
return null;
}
$disk = Storage::disk($media->disk);
$videoPath = $disk->path($media->path);
if (! is_file($videoPath)) {
return null;
}
$thumbnailRelativePath = preg_replace('/\.[^.\/]+$/', '', $media->path).'-poster.jpg';
$thumbnailPath = $disk->path($thumbnailRelativePath);
if (! is_dir(dirname($thumbnailPath))) {
@mkdir(dirname($thumbnailPath), 0755, true);
}
// Try a frame ~1s in first (avoids black intro frames); fall back to
// the very first frame for clips shorter than one second.
foreach (['1', '0'] as $seekSeconds) {
if ($this->extractFrame($videoPath, $thumbnailPath, $seekSeconds) && filesize($thumbnailPath) > 0) {
return $thumbnailRelativePath;
}
}
if (is_file($thumbnailPath)) {
@unlink($thumbnailPath);
}
return null;
}
private function extractFrame(string $videoPath, string $thumbnailPath, string $seekSeconds): bool
{
$process = new Process([
'ffmpeg',
'-y',
'-ss', $seekSeconds,
'-i', $videoPath,
'-frames:v', '1',
'-vf', 'scale=640:-2',
'-q:v', '3',
$thumbnailPath,
]);
$process->setTimeout(60);
try {
$process->run();
} catch (\Throwable) {
return false;
}
return $process->isSuccessful() && is_file($thumbnailPath);
}
/**
* @return array{width: int, height: int}|null
*/
private function readVideoDimensions(string $videoPath): ?array
{
$process = new Process([
'ffprobe',
'-v', 'error',
'-select_streams', 'v:0',
'-show_entries', 'stream=width,height',
'-of', 'csv=s=x:p=0',
$videoPath,
]);
$process->setTimeout(30);
try {
$process->run();
} catch (\Throwable) {
return null;
}
if (! $process->isSuccessful()) {
return null;
}
$dimensions = trim($process->getOutput());
if (! preg_match('/^(?<width>\d+)x(?<height>\d+)$/', $dimensions, $matches)) {
return null;
}
return [
'width' => (int) $matches['width'],
'height' => (int) $matches['height'],
];
}
/**
* Create a DisplayMedia record from an external URL.
*/
public function createFromUrl(string $url, string $type = 'video', ?string $title = null, ?string $collection = null): DisplayMedia
{
$filename = $title ?: $this->extractFilenameFromUrl($url);
return DisplayMedia::create([
'filename' => $filename,
'disk' => 'public',
'path' => null,
'external_url' => $url,
'source_type' => 'external',
'type' => $type,
'mime_type' => null,
'file_size' => 0,
'title' => $title,
'collection' => $collection,
]);
}
/**
* Validate that an external URL is accessible.
*/
public function validateExternalUrl(string $url): bool
{
try {
$response = Http::timeout(10)
->withOptions(['allow_redirects' => true])
->head($url);
return $response->successful() || $response->status() === 302 || $response->status() === 301;
} catch (\Throwable) {
// Some services block HEAD requests, try GET with stream
try {
$response = Http::timeout(10)
->withOptions(['allow_redirects' => true, 'stream' => true])
->get($url);
return $response->successful();
} catch (\Throwable) {
return false;
}
}
}
/**
* Delete a DisplayMedia record and its associated files.
*/
public function delete(DisplayMedia $media): void
{
if ($media->isUpload() && $media->path) {
Storage::disk($media->disk)->delete($media->path);
}
if ($media->thumbnail_path) {
Storage::disk($media->disk)->delete($media->thumbnail_path);
}
$media->delete();
}
private function extractFilenameFromUrl(string $url): string
{
$parsed = parse_url($url, PHP_URL_PATH);
$basename = $parsed ? basename($parsed) : 'external-media';
return Str::limit($basename, 100);
}
}