Skip to content

A self-hosted media server and file management system

feat: update models for thumbnails

aminnausinf6a6bc479ac3
diff --git a/app/Enums/ImageSource.php b/app/Enums/ImageSource.php
index 5d35c759..a70e2959 100644
--- a/app/Enums/ImageSource.php
+++ b/app/Enums/ImageSource.php
@@ -8,6 +8,7 @@ enum ImageSource: int {
case API = 2;
case URL = 3; // external url, not downloaded
case DOWNLOADED = 4;
+ case EMBEDDED = 5;
public function label(): string {
return strtolower($this->name);
diff --git a/app/Enums/ImageVariant.php b/app/Enums/ImageVariant.php
new file mode 100644
index 00000000..78888901
--- /dev/null
+++ b/app/Enums/ImageVariant.php
@@ -0,0 +1,7 @@
+<?php
+
+namespace App\Enums;
+
+enum ImageVariant: string {
+ case Original = 'original';
+}
diff --git a/app/Models/Image.php b/app/Models/Image.php
index 7799161b..1b7aa1c9 100644
--- a/app/Models/Image.php
+++ b/app/Models/Image.php
@@ -4,32 +4,70 @@ namespace App\Models;
use App\Enums\ImageSource;
use App\Enums\ImageType;
+use App\Enums\ImageVariant;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\MorphTo;
class Image extends Model {
/**
* id -> int8 (pk)
- * metadata_uuid -> int8 (fk) (index) (onDelete=cascade)
+ *
+ * imageable_type -> varchar(255)
+ * imageable_id -> uuid (fk) (index) (composite index with imageable_type)
*
* user_id -> int8 (fk) (nullable) (onDelete=setNull)
*
- * image_type -> int2 (enum) (composite index with metadata_uuid, is_primary)
- * source -> int2 (enum)
- * is_primary -> boolean (default=false)
+ * image_type -> int2 (enum) (composite index with imageable_type, imageable_id, replaced_at)
+ * image_source -> int2 (enum)
+ * image_variant -> string (enum) (nullable)
*
* width -> int4 (nullable)
* height -> int4 (nullable)
- * blurhash -> varchar(64)
+ * size -> uint8 (default=0)
+ * blur_hash -> varchar(64) (nullable)
*
+ * format -> varchar(10) (nullable)
* path -> text (nullable)
* source_url -> text (nullable)
*
+ * replaced_at -> timestamptz (nullable)
+ *
* created_at -> timestamptz (nullable)
* updated_at -> timestamptz (nullable)
*/
protected $casts = [
'image_type' => ImageType::class,
- 'source' => ImageSource::class,
- 'is_primary' => 'boolean',
+ 'image_source' => ImageSource::class,
+ 'image_variant' => ImageVariant::class,
+ ];
+
+ protected $fillable = [
+ 'imageable_type',
+ 'imageable_id',
+
+ 'image_type',
+ 'image_source',
+ 'image_variant',
+ 'width',
+ 'height',
+ 'size',
+ 'blur_hash',
+
+ 'format',
+ 'path',
+ 'source_url',
+
+ 'replaced_at',
];
+
+ public function imageable(): MorphTo {
+ return $this->morphTo();
+ }
+
+ public function filename(): string {
+ $variant = $this->image_variant ? "_{$this->image_variant->value}" : '';
+ $ext = $this->format ?? 'webp';
+
+ return $this->image_type->label() . "{$variant}.{$ext}";
+ }
}
diff --git a/app/Models/Metadata.php b/app/Models/Metadata.php
index 1c88ea39..fe49d076 100644
--- a/app/Models/Metadata.php
+++ b/app/Models/Metadata.php
@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
+use Illuminate\Database\Eloquent\Relations\MorphMany;
class Metadata extends Model {
use HasEditableFields, HasFactory;
@@ -162,8 +163,12 @@ class Metadata extends Model {
return $this->hasMany(Subtitle::class, 'metadata_uuid', 'uuid')->orderBy('track_id');
}
- public function images(): HasMany {
- return $this->hasMany(Image::class, 'metadata_uuid', 'uuid')->orderBy('track_id');
+ public function images(): MorphMany {
+ return $this->morphMany(Image::class, 'imageable');
+ }
+
+ public function primaryPoster(): BelongsTo {
+ return $this->belongsTo(Image::class, 'primary_poster_id');
}
public function storyboard(): HasOne {
@@ -189,4 +194,37 @@ class Metadata extends Model {
'artist',
];
}
+
+ /**
+ * Builds the ideal directory for a given metadata item
+ *
+ * @param Metadata $metadata Metadata row
+ * @return string The ideal directory in the format {shard}/{uuid}
+ */
+ public static function buildMetadataDirectory(Metadata $metadata): string {
+ $uuid = $metadata->uuid;
+ if (! $uuid || ! uuid_is_valid($uuid)) {
+ throw new \RuntimeException('Metadata has no uuid');
+ }
+
+ $shard = substr($uuid, 0, 2);
+
+ return "{$shard}/{$uuid}";
+ }
+
+ /**
+ * Builds the ideal directory for a given metadata item
+ *
+ * @return string The ideal directory in the format /{shard}/{uuid}
+ */
+ public function metadataDirectory(): string {
+ $uuid = $this->uuid;
+ if (! $uuid || ! uuid_is_valid($uuid)) {
+ throw new \RuntimeException('Metadata has no uuid');
+ }
+
+ $shard = substr($uuid, 0, 2);
+
+ return "{$shard}/{$uuid}";
+ }
}
diff --git a/app/Models/Series.php b/app/Models/Series.php
index e0a02e81..9184c840 100644
--- a/app/Models/Series.php
+++ b/app/Models/Series.php
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\MorphMany;
class Series extends Model {
use HasEditableFields, HasFactory;
@@ -92,6 +93,14 @@ class Series extends Model {
return $this->hasMany(SeriesSizeHistory::class);
}
+ public function images(): MorphMany {
+ return $this->morphMany(Image::class, 'imageable');
+ }
+
+ public function primaryPoster(): BelongsTo {
+ return $this->belongsTo(Image::class, 'primary_poster_id');
+ }
+
// This is only for demo reset so it is not super important, only include fields that may have bad content
protected function getEditableFields(): array {
return [
diff --git a/app/Models/Video.php b/app/Models/Video.php
index 7a5a05ae..5c74b401 100755
--- a/app/Models/Video.php
+++ b/app/Models/Video.php
@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
+use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class Video extends Model {
@@ -59,4 +60,13 @@ class Video extends Model {
public function getCompositeIdAttribute(): string {
return Str::after($this->path, config('media.storage.prefix', 'storage/media/'));
}
+
+ public function getAbsoluteMediaPath() {
+ $path = $this->path;
+ $formattedPath = str_starts_with($path, 'storage/')
+ ? substr($path, 8)
+ : $path;
+
+ return str_replace('\\', '/', Storage::disk('public')->path($formattedPath));
+ }
}
diff --git a/database/migrations/2026_05_29_042123_add_poster_to_series.php b/database/migrations/2026_05_29_042123_add_poster_to_series.php
new file mode 100644
index 00000000..6ae78cae
--- /dev/null
+++ b/database/migrations/2026_05_29_042123_add_poster_to_series.php
@@ -0,0 +1,26 @@
+<?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->uuid('primary_poster_id')->nullable()->constrained('images')->nullOnDelete();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void {
+ Schema::table('series', function (Blueprint $table) {
+ $table->dropForeign(['primary_poster_id']);
+ $table->dropColumn('primary_poster_id');
+ });
+ }
+};