A self-hosted media server and file management system
Vue ffmpegfile-serverjellyfinlaravelmediamedia-playermedia-servermediabrowsermediaservermusicmusic-librarymusic-playermusicplayerself-hostedvuejs
feat: update verify folders to use image service to download new images
aminnausin4c14b1e1f5f4
diff --git a/app/Jobs/VerifyFolders.php b/app/Jobs/VerifyFolders.phpindex 8147a414..9300bdc3 100644--- a/app/Jobs/VerifyFolders.php+++ b/app/Jobs/VerifyFolders.php@@ -2,18 +2,18 @@namespace App\Jobs;+use App\Enums\ImageType;use App\Enums\TaskStatus;use App\Exceptions\DataLostException;use App\Models\Series;use App\Models\SeriesSizeHistory;use App\Models\SubTask;+use App\Models\Task;+use App\Services\Images\ImageService;use App\Services\TaskService;use Illuminate\Support\Collection;use Illuminate\Support\Facades\DB;-use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;-use Illuminate\Support\Facades\Storage;-use Illuminate\Support\Str;class VerifyFolders extends ManagedSubTask {/**@@ -28,13 +28,13 @@ class VerifyFolders extends ManagedSubTask {/*** Execute the job.*/- public function handle(TaskService $taskService): void {+ public function handle(TaskService $taskService, ImageService $imageService): void {if (! $this->beginSubTask($taskService)) {return;}try {- $summary = $this->verifyFolders($taskService);+ $summary = $this->verifyFolders($taskService, $imageService);$this->completeSubTask($taskService, $summary);} catch (\Throwable $th) {$this->failSubTask($taskService, $th);@@ -42,7 +42,7 @@ class VerifyFolders extends ManagedSubTask {}}- private function verifyFolders(TaskService $taskService) {+ private function verifyFolders(TaskService $taskService, ImageService $imageService) {if (count($this->folders) == 0) {throw new DataLostException('Folder Data Lost');}@@ -53,8 +53,10 @@ class VerifyFolders extends ManagedSubTask {$error = false;$index = 0;+ $taskUserId = Task::find($this->taskId)->user_id;+$this->folders->load([- 'series',+ 'series.primaryPoster','videos.metadata',]);@@ -93,31 +95,38 @@ class VerifyFolders extends ManagedSubTask {$changes['title'] = $folder->name;}- $thumbnailPath = explode('/', $series->composite_id ?? 'unsorted/unsorted')[0] . '/' . basename($series->id);- $thumbnailIsInternal = strpos($series->thumbnail_url, str_replace('http://', '', str_replace('https://', '', config('api.app_url'))));-- if (isset($series->thumbnail_url) && ! $thumbnailIsInternal && strlen(trim($series->thumbnail_url)) > 0) {- $thumbnailResult = $this->getThumbnailAsFile($series->thumbnail_url, $thumbnailPath);- if ($thumbnailResult) {- $changes['raw_thumbnail_url'] = $series->thumbnail_url;- $changes['thumbnail_url'] = $thumbnailResult;- Log::info("Downloaded external thumbnail for {$series->id}.", [+ // region Poster++ /**+ * Only checks for external posters, and downloads if not already downloaded+ *+ * But it should only check if it was updated because the url stays in the db row for user to see?+ * otherwise its doing this poster check every time+ *+ * theres also if it was replaced with upload or something then it will always overwrite? idk+ *+ * Eventually can pull poster from 3rd party API+ */+ $externalPosterDownloaded = $series->primaryPoster && $series->primaryPoster->source_url === $series->thumbnail_url;+ $posterIsExternal = filter_var($series->thumbnail_url, FILTER_VALIDATE_URL) && ! str_contains($series->thumbnail_url, config('api.app_url'));++ if ($posterIsExternal && ! $externalPosterDownloaded) {+ $image = $imageService->downloadFromUrl($series->thumbnail_url, $series, ImageType::POSTER, $taskUserId);++ if ($image) {+ $changes['primary_poster_id'] = $image->id;+ $changes['poster_updated_at'] = now();+ $changes['thumbnail_url'] = config('app.url') . "/storage/{$image->path}";+ Log::info("Downloaded external thumbnail for {$series->composite_id}.", ['series' => $series->id,- 'src' => $changes['raw_thumbnail_url'],- 'dst' => $thumbnailResult,+ 'src' => $series->thumbnail_url,+ 'dst' => $image->path,]);}- } elseif (isset($series->thumbnail_url) && $thumbnailIsInternal && ! Storage::disk('public')->exists("thumbnails/$thumbnailPath.webp")) {- // This means the thumbnail is set with another internal image url (for example cover art from a song was used as a thumbnail for some folder)- Log::warning(- "Local thumbnail is set but does not exist for $series->composite_id at " . Storage::disk('public')->path("thumbnails/$thumbnailPath.webp"),- [- 'path' => Str::after(urldecode($series->thumbnail_url), '/storage/'),- 'exists' => Storage::disk('public')->exists(Str::after(urldecode($series->thumbnail_url), '/storage/')),- ]- );}+ // endregion+if (! empty($changes)) {$changes['updated_at'] = now();$transactions[] = [...$stored, ...$changes];@@ -149,7 +158,7 @@ class VerifyFolders extends ManagedSubTask {DB::beginTransaction();- Series::upsert($transactions, 'id', ['folder_id', 'title', 'episodes', 'thumbnail_url', 'raw_thumbnail_url', 'total_size', 'file_count', 'primary_media_type', 'updated_at']);+ Series::upsert($transactions, 'id', ['folder_id', 'title', 'episodes', 'thumbnail_url', 'raw_thumbnail_url', 'total_size', 'file_count', 'primary_media_type', 'updated_at', 'primary_poster_id', 'poster_updated_at']);if (! empty($sizeHistoryTransactions)) {SeriesSizeHistory::insert($sizeHistoryTransactions);@@ -163,21 +172,18 @@ class VerifyFolders extends ManagedSubTask {$this->handleError('Error inserting verified folder series data', $th, $ids, count($transactions));}}-- private function getThumbnailAsFile($url, $compositePath) {- try {- $response = Http::get($url);- if ($response->successful()) {- $imageContent = $response->body();- $path = 'thumbnails/' . $compositePath . '.webp';- Storage::disk('public')->put($path, $imageContent);-- return VerifyFiles::getPathUrl($path);- }- } catch (\Throwable $th) {- Log::warning("Unable to download thumbnail image from $url for $compositePath: " . $th->getMessage());- }-- return false;- }}++// $thumbnailPath = explode('/', $series->composite_id ?? 'unsorted/unsorted')[0] . '/' . basename($series->id);+// $thumbnailIsInternal = strpos($series->thumbnail_url, str_replace('http://', '', str_replace('https://', '', config('api.app_url'))));++// if (isset($series->thumbnail_url) && $thumbnailIsInternal && ! Storage::disk('public')->exists("thumbnails/$thumbnailPath.webp") && !$series->primaryPoster) {+// // This means the thumbnail is set with another internal image url (for example cover art from a song was used as a thumbnail for some folder)+// Log::warning(+// "Local thumbnail is set but does not exist for $series->composite_id at " . Storage::disk('public')->path("thumbnails/$thumbnailPath.webp"),+// [+// 'path' => Str::after(urldecode($series->thumbnail_url), '/storage/'),+// 'exists' => Storage::disk('public')->exists(Str::after(urldecode($series->thumbnail_url), '/storage/')),+// ]+// );+// }diff --git a/app/Models/Series.php b/app/Models/Series.phpindex b8d0fd09..e89a6e83 100644--- a/app/Models/Series.php+++ b/app/Models/Series.php@@ -43,6 +43,9 @@ class Series extends Model {* file_count -> uint4 (default=0)** avg_intro_duration -> float(2) (default=90)+ *+ * primary_poster_id -> int8 (fk) (nullable)+ * poster_updated_at -> timestampTz (nullable)*/protected $fillable = ['uuid',@@ -63,6 +66,9 @@ class Series extends Model {'thumbnail_url','edited_at','downloads_enabled',++ 'primary_poster_id',+ 'poster_updated_at',];protected $casts = [@@ -74,6 +80,8 @@ class Series extends Model {'edited_at' => 'datetime',+ 'poster_updated_at' => 'datetime',+'downloads_enabled' => 'boolean',];diff --git a/app/Services/Images/ImageService.php b/app/Services/Images/ImageService.phpindex e4096d40..948cf159 100644--- a/app/Services/Images/ImageService.php+++ b/app/Services/Images/ImageService.php@@ -169,6 +169,38 @@ class ImageService {}}+ // Untested+ // Dangerous+ public function migrateImage(string $filePath, Model $owner, ImageType $imageType, ?int $userId = null): ?Image {+ try {+ $fmt = pathinfo($filePath, PATHINFO_EXTENSION);++ [$relativeOutputPath, $absoluteOutputPath] = $this->resolvePath($owner, $imageType, $fmt);++ if (! file_exists($filePath) || filesize($filePath) === 0) {+ throw new \InvalidArgumentException('File provided for migration is invalid');+ }++ rename($filePath, $absoluteOutputPath);++ return $this->persistImage(+ $owner,+ new ImageData(+ absolutePath: $absoluteOutputPath,+ relativePath: $relativeOutputPath,+ type: $imageType,+ source: ImageSource::LEGACY,+ format: $fmt,+ userId: $userId,+ )+ );+ } catch (\Throwable $th) {+ Log::warning('Failed to migrate image', ['path' => $filePath, 'owner' => $owner->class, 'error' => $th->getMessage()]);++ return null;+ }+ }+private function generateBlurHash(string $absolutePath): ?string {try {return BlurHash::encode($absolutePath);@@ -179,16 +211,22 @@ class ImageService {}}+ /**+ * Resolves relative and absolute path for an image type, format and variant for an imageable model+ *+ * Path: metadata/{imageable}/{shard}/{uuid}/{type->label}.{format}+ * Disk: public+ */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',+ default => throw new \UnexpectedValueException('Unknown imageable: ' . $owner::class),};$disk = Storage::disk('public');- $relativeDir = "images/{$prefix}/" . substr($uuid, 0, 2) . "/{$uuid}";+ $relativeDir = "metadata/{$prefix}/" . substr($uuid, 0, 2) . "/{$uuid}";$disk->makeDirectory($relativeDir);$filename = $type->label() . ($variant ? "_{$variant->value}" : '') . ".{$format}";@@ -230,6 +268,7 @@ class ImageService {'path' => $data->relativePath,'format' => $data->format,+ 'source_url' => $data->sourceUrl,'blur_hash' => $this->generateBlurhash($data->absolutePath),]);diff --git a/database/migrations/2026_05_29_221445_add_poster_changed_date_to_series.php b/database/migrations/2026_05_29_221445_add_poster_changed_date_to_series.phpnew file mode 100644index 00000000..b793c7fe--- /dev/null+++ b/database/migrations/2026_05_29_221445_add_poster_changed_date_to_series.php@@ -0,0 +1,25 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration {+ /**+ * Run the migrations.+ */+ public function up(): void {+ Schema::table('series', function (Blueprint $table) {+ $table->timestampTz('poster_updated_at')->nullable();+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void {+ Schema::table('series', function (Blueprint $table) {+ $table->dropColumn(['poster_updated_at']);+ });+ }+};diff --git a/database/migrations/2026_05_29_231506_change_thumbnail_url_to_text_on_series.php b/database/migrations/2026_05_29_231506_change_thumbnail_url_to_text_on_series.phpnew file mode 100644index 00000000..339297ed--- /dev/null+++ b/database/migrations/2026_05_29_231506_change_thumbnail_url_to_text_on_series.php@@ -0,0 +1,25 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration {+ /**+ * Run the migrations.+ */+ public function up(): void {+ Schema::table('series', function (Blueprint $table) {+ $table->text('thumbnail_url')->nullable()->change();+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void {+ Schema::table('text', function (Blueprint $table) {+ $table->string('thumbnail_url')->nullable()->change();+ });+ }+};