Skip to content

A self-hosted media server and file management system

feat: image service

aminnausinbc3c3c2a7ab1
diff --git a/app/Jobs/VerifyFiles.php b/app/Jobs/VerifyFiles.php
index 38d1660c..71325266 100755
--- a/app/Jobs/VerifyFiles.php
+++ b/app/Jobs/VerifyFiles.php
@@ -11,6 +11,7 @@ use App\Models\Metadata;
use App\Models\Record;
use App\Models\SubTask;
use App\Models\Video;
+use App\Services\Images\ImageService;
use App\Services\Server\ServerConfigService;
use App\Services\TaskService;
use Carbon\Carbon;
@@ -45,13 +46,13 @@ class VerifyFiles extends ManagedSubTask {
$this->subTaskId = $subTask->id;
}
- public function handle(TaskService $taskService, ServerConfigService $config): void {
+ public function handle(TaskService $taskService, ServerConfigService $config, ImageService $imageService): void {
if (! $this->beginSubTask($taskService)) {
return;
}
try {
- $summary = $this->verifyFiles($taskService, $config);
+ $summary = $this->verifyFiles($taskService, $config, $imageService);
foreach ($this->scannedDirectories as $folderPath => $dirData) {
if (empty($dirData['targets'])) {
@@ -93,7 +94,7 @@ class VerifyFiles extends ManagedSubTask {
}
}
- private function verifyFiles(TaskService $taskService, ServerConfigService $config) {
+ private function verifyFiles(TaskService $taskService, ServerConfigService $config, ImageService $imageService) {
$metadataTransactions = [];
$error = false;
diff --git a/app/Jobs/VerifyFolders.php b/app/Jobs/VerifyFolders.php
index b0249d52..8147a414 100644
--- a/app/Jobs/VerifyFolders.php
+++ b/app/Jobs/VerifyFolders.php
@@ -8,6 +8,7 @@ use App\Models\Series;
use App\Models\SeriesSizeHistory;
use App\Models\SubTask;
use App\Services\TaskService;
+use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
@@ -18,7 +19,7 @@ class VerifyFolders extends ManagedSubTask {
/**
* Create a new job instance.
*/
- public function __construct(public $folders, $taskId) {
+ public function __construct(public Collection $folders, int $taskId) {
$subTask = SubTask::create(['task_id' => $taskId, 'status' => TaskStatus::PENDING, 'name' => 'Verify ' . count($folders) . ' Folders']); //
$this->taskId = $taskId;
$this->subTaskId = $subTask->id;
diff --git a/app/Services/Images/ImageService.php b/app/Services/Images/ImageService.php
new file mode 100644
index 00000000..0789c375
--- /dev/null
+++ b/app/Services/Images/ImageService.php
@@ -0,0 +1,208 @@
+<?php
+
+namespace App\Services\Images;
+
+use App\Enums\ImageSource;
+use App\Enums\ImageType;
+use App\Enums\ImageVariant;
+use App\Models\Image;
+use App\Models\Metadata;
+use App\Models\Series;
+use Bepsvpt\Blurhash\Facades\BlurHash;
+use Illuminate\Contracts\Filesystem\FileNotFoundException;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
+use Symfony\Component\Process\Exception\ProcessFailedException;
+use Symfony\Component\Process\Process;
+
+readonly class ImageData {
+ public function __construct(
+ public string $absolutePath,
+ public string $relativePath,
+ public ImageType $type,
+ public ImageSource $source,
+ public string $format,
+ public ?ImageVariant $variant = null,
+ public ?int $userId = null,
+ ) {}
+}
+
+class ImageService {
+ /**
+ * Extracts poster from a video file and saves to metadata directory
+ *
+ * @param string $filePath Absolute path to the input file
+ * @return ?Image returns Image row
+ */
+ public function generateVideoPoster(string $filePath, Metadata $metadata, ?int $userId = null): ?Image {
+ $offset = max(30, (int) ($metadata->duration * 0.1)); // 10% in or 30s min
+ $fmt = 'webp';
+
+ [$relativePath, $absolutePath] = $this->resolvePath($metadata, ImageType::POSTER, $fmt);
+
+ try {
+ $process = new Process([
+ 'ffmpeg',
+ '-ss',
+ $offset,
+ '-i',
+ $filePath,
+ '-frames:v',
+ '1',
+ '-c:v',
+ 'libwebp',
+ '-quality',
+ '85',
+ '-y',
+ $absolutePath,
+ ]);
+ $process->mustRun();
+
+ return $this->persistImage($metadata, new ImageData(absolutePath: $absolutePath, relativePath: $relativePath, type: ImageType::POSTER, source: ImageSource::GENERATED, format: $fmt, userId: $userId));
+ } catch (\Throwable $th) {
+ Log::error('Video poster generation failed', ['file' => $filePath, 'error' => $th->getMessage()]);
+
+ return null;
+ }
+ }
+
+ /**
+ * Extracts album art from a file and saves to metadata directory
+ *
+ * @param string $filePath Absolute path to the input file
+ * @return ?Image returns Image row
+ */
+ public function extractAlbumArt(string $filePath, Metadata $metadata, bool $overwrite = false, ?int $userId = null): ?Image {
+ $fmt = 'webp'; // Audio supports png only ?
+
+ [$relativePath, $absolutePath] = $this->resolvePath($metadata, ImageType::POSTER, $fmt);
+
+ try {
+ $process = new Process([
+ 'ffmpeg',
+ '-i',
+ $filePath,
+ '-an',
+ '-vcodec',
+ 'copy',
+ '-f',
+ 'image2',
+ '-update',
+ '1',
+ '-y',
+ $absolutePath,
+ ]);
+ $process->run();
+
+ if (! $process->isSuccessful()) {
+ // Checks if error is caused by missing album art (never set so dont log)
+ if (str_contains($process->getErrorOutput(), 'Output file does not contain any stream')) {
+ return null;
+ }
+ throw new ProcessFailedException($process);
+ }
+
+ return $this->persistImage($metadata, new ImageData(absolutePath: $absolutePath, relativePath: $relativePath, type: ImageType::POSTER, source: ImageSource::EMBEDDED, format: $fmt, userId: $userId));
+ } catch (\Throwable $th) {
+ Log::error('Album art extraction failed', ['file' => $filePath, 'error' => $th->getMessage()]);
+
+ return null;
+ }
+ }
+
+ public function downloadFromUrl(string $url, Model $owner, ImageType $imageType, ?int $userId = null): ?Image {
+ try {
+ $fmt = 'webp';
+
+ [$relativePath, $absolutePath] = $this->resolvePath($owner, $imageType, $fmt);
+
+ $response = Http::get($url);
+ if (! $response->successful()) {
+ return null;
+ }
+
+ Storage::disk('public')->put($relativePath, $response->body());
+
+ return $this->persistImage($owner, new ImageData(absolutePath: $absolutePath, relativePath: $relativePath, type: $imageType, source: ImageSource::DOWNLOADED, format: $fmt, userId: $userId));
+ } catch (\Throwable $th) {
+ Log::warning('Failed to download image', ['url' => $url, 'error' => $th->getMessage()]);
+
+ return null;
+ }
+ }
+
+ private function generateBlurHash(string $absolutePath): ?string {
+ try {
+ return BlurHash::encode($absolutePath);
+ } catch (\Throwable $th) {
+ Log::error('Blur hash generation failed', ['file' => $absolutePath, 'error' => $th->getMessage(), 'trace' => $th->getTraceAsString()]);
+
+ return null;
+ }
+ }
+
+ private function resolvePath(Model $owner, ImageType $type, string $format, ?ImageVariant $variant = null): array {
+ $uuid = $owner->uuid ?? $owner->getKey();
+ $prefix = match (true) {
+ $owner instanceof Metadata => 'metadata',
+ $owner instanceof Series => 'series',
+ default => 'assets',
+ };
+
+ $disk = Storage::disk('public');
+ $relativeDir = "images/{$prefix}/" . substr($uuid, 0, 2) . "/{$uuid}";
+ $disk->makeDirectory($relativeDir);
+
+ $filename = $type->label() . ($variant ? "_{$variant->value}" : '') . ".{$format}";
+
+ $relativePath = "{$relativeDir}/{$filename}";
+ $absolutePath = str_replace('\\', '/', $disk->path($relativePath));
+
+ return [$relativePath, $absolutePath];
+ }
+
+ private function persistImage(Model $owner, ImageData $data): Image {
+ if (! file_exists($data->absolutePath) || filesize($data->absolutePath) === 0) {
+ throw new FileNotFoundException("Image file not created: {$data->absolutePath}");
+ }
+
+ $uuid = $owner->uuid ?? throw new \InvalidArgumentException('Owner must have a UUID');
+
+ Image::where([
+ 'imageable_id' => $uuid,
+ 'imageable_type' => $owner::class,
+ 'image_type' => $data->type,
+ 'replaced_at' => null,
+ ])->update(['replaced_at' => now()]);
+
+ [$width, $height] = getimagesize($data->absolutePath);
+
+ return Image::create([
+ 'imageable_id' => $uuid,
+ 'imageable_type' => $owner::class,
+ 'user_id' => $data->userId,
+
+ 'image_type' => $data->type,
+ 'image_source' => $data->source,
+ 'image_variant' => $data->variant,
+
+ 'width' => $width,
+ 'height' => $height,
+ 'size' => filesize($data->absolutePath),
+
+ 'path' => $data->relativePath,
+ 'format' => $data->format,
+
+ 'blur_hash' => $this->generateBlurhash($data->absolutePath),
+ ]);
+ }
+
+ public static function getImageUrl(string $relativePath, string $disk = 'public') {
+ /**
+ * @disregard P1013 Undefined method but it actually exists
+ */
+ return Storage::disk($disk)->url($relativePath);
+ }
+}