Skip to content

A self-hosted media server and file management system

feat: add media type to series and optimise generating it

aminnausina5a638d4862d
diff --git a/app/Enums/MediaType.php b/app/Enums/MediaType.php
new file mode 100644
index 00000000..bca3742f
--- /dev/null
+++ b/app/Enums/MediaType.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Enums;
+
+enum MediaType: int {
+ case VIDEO = 0;
+ case AUDIO = 1;
+
+ public function isVideo(): bool {
+ return $this === self::VIDEO;
+ }
+
+ public function isAudio(): bool {
+ return $this === self::AUDIO;
+ }
+
+ /**
+ * Check if a given value is a valid enum case.
+ */
+ public static function isValid(int $type): bool {
+ foreach (self::cases() as $case) {
+ if ($case->value == $type) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public static function getValues(): array {
+ return array_column(self::cases(), 'value');
+ }
+
+ public static function getLabel(TaskStatus $type): string {
+ return match ($type) {
+ self::VIDEO => 'video',
+ self::AUDIO => 'audio',
+ };
+ }
+}
diff --git a/app/Http/Controllers/DirectoryController.php b/app/Http/Controllers/DirectoryController.php
index 4de74209..b0af481f 100755
--- a/app/Http/Controllers/DirectoryController.php
+++ b/app/Http/Controllers/DirectoryController.php
@@ -3,8 +3,6 @@
namespace App\Http\Controllers;
use App\Http\Resources\FolderResource;
-use App\Http\Resources\SeriesResource;
-use App\Http\Resources\VideoResource;
use App\Models\Category;
use App\Models\Folder;
use App\Services\PathResolverService;
@@ -97,12 +95,8 @@ class DirectoryController extends Controller {
private function loadFolderData(array $data, FolderResource $folder): array {
$folder->load(['videos.metadata.videoTags.tag', 'series.folderTags.tag']);
- $data['folder'] = [
- 'id' => $folder->id,
- 'name' => $folder->name,
- 'videos' => VideoResource::collection($folder->videos),
- 'series' => new SeriesResource($folder->series),
- ];
+ $request = Request::create('', 'GET', ['videos' => true]);
+ $data['folder'] = $folder->toArray($request);
return $data;
}
diff --git a/app/Http/Resources/FolderResource.php b/app/Http/Resources/FolderResource.php
index 42fef4d7..f966bb8b 100755
--- a/app/Http/Resources/FolderResource.php
+++ b/app/Http/Resources/FolderResource.php
@@ -18,6 +18,7 @@ class FolderResource extends JsonResource {
'path' => $this->path,
'file_count' => $this->videos_count ?? $this->series->episodes ?? 0, // $videos->count(),
'total_size' => $this->series->total_size,
+ 'is_majority_audio' => $this->series->primary_media_type ? true : false,
'category_id' => $this->category_id,
'videos' => $this->when($request->videos, function () {
return VideoResource::collection($this->videos);
diff --git a/app/Jobs/VerifyFolders.php b/app/Jobs/VerifyFolders.php
index a7d38073..5278e136 100644
--- a/app/Jobs/VerifyFolders.php
+++ b/app/Jobs/VerifyFolders.php
@@ -5,7 +5,6 @@ namespace App\Jobs;
use App\Enums\TaskStatus;
use App\Models\Series;
use App\Models\SubTask;
-use App\Models\Video;
use App\Services\TaskService;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
@@ -84,6 +83,11 @@ class VerifyFolders implements ShouldQueue {
$error = false;
$index = 0;
+ $this->folders->load([
+ 'series',
+ 'videos.metadata',
+ ]);
+
foreach ($this->folders as $folder) {
try {
$stored = [];
@@ -94,9 +98,11 @@ class VerifyFolders implements ShouldQueue {
$stored = $series->toArray();
// if (is_null($series->episodes)) {
- $changes['episodes'] = Video::where('folder_id', $folder->id)->count();
+ $changes['episodes'] = $folder->videos->count();
// }
+ $changes['primary_media_type'] = $folder->primary_media_type;
+
if (is_null($series->title)) {
$changes['title'] = $folder->name;
}
@@ -104,12 +110,13 @@ class VerifyFolders implements ShouldQueue {
if (isset($series->thumbnail_url) && ! strpos($series->thumbnail_url, str_replace('http://', '', str_replace('https://', '', config('api.app_url'))))) {
$thumbnailResult = $this->getThumbnailAsFile($series->thumbnail_url, explode('/', $series->composite_id ?? 'unsorted/unsorted')[0] . '/' . basename($series->id));
if ($thumbnailResult) {
+ $changes['raw_thumbnail_url'] = $series->thumbnail_url;
$changes['thumbnail_url'] = $thumbnailResult;
- dump('got thumbnail for ' . $series->id . ' at ' . $thumbnailResult);
+ dump('got thumbnail for ' . $series->id . ' at ' . $thumbnailResult . ' from ' . $changes['raw_thumbnail_url']);
}
}
- $totalSize = $series->folder->total_size;
+ $totalSize = $folder->total_size;
if ($stored['total_size'] !== $totalSize) {
$changes['total_size'] = $totalSize;
}
@@ -140,7 +147,7 @@ class VerifyFolders implements ShouldQueue {
return 'No Changes Found';
}
- Series::upsert($transactions, 'id', ['folder_id', 'title', 'episodes', 'thumbnail_url', 'total_size']);
+ Series::upsert($transactions, 'id', ['folder_id', 'title', 'episodes', 'thumbnail_url', 'raw_thumbnail_url', 'total_size', 'primary_media_type']);
$summary = 'Updated ' . count($transactions) . ' folders from id ' . ($transactions[0]['folder_id']) . ' to ' . ($transactions[count($transactions) - 1]['folder_id']);
dump($summary);
diff --git a/app/Models/Folder.php b/app/Models/Folder.php
index c1423393..6d5ea533 100755
--- a/app/Models/Folder.php
+++ b/app/Models/Folder.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\HasMany;
+use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
class Folder extends Model {
@@ -25,34 +26,25 @@ class Folder extends Model {
return $this->belongsTo(Category::class);
}
- public function folderTags(): HasMany {
- return $this->hasMany(FolderTag::class);
+ public function folderTags(): HasManyThrough {
+ return $this->hasManyThrough(FolderTag::class, Series::class);
}
public function getTotalSizeAttribute() {
return $this->videos()->join('metadata', 'videos.id', '=', 'metadata.video_id')->sum('metadata.file_size');
}
- public function isMajorityAudio(): bool {
- $totalVideos = $this->videos()->count();
-
- if ($totalVideos === 0) {
- return false;
- }
+ public function getPrimaryMediaTypeAttribute() {
+ return $this->isMajorityAudio() ? 1 : 0;
+ }
- $audioVideos = $this->videos()
- ->whereHas('metadata', function ($query) {
- $query->where('mime_type', 'like', 'audio%');
- })
- ->count();
+ public function isMajorityAudio(): bool {
+ $counts = $this->videos()
+ ->selectRaw('COUNT(*) as total')
+ ->selectRaw("SUM(CASE WHEN metadata.mime_type LIKE 'audio%' THEN 1 ELSE 0 END) as audio")
+ ->join('metadata', 'videos.id', '=', 'metadata.video_id')
+ ->first();
- return $audioVideos >= ($totalVideos / 2);
+ return $counts->total > 0 && $counts->audio >= ($counts->total / 2);
}
-
- // protected static function boot() {
- // parent::boot(); // Automatic withCount
- // static::addGlobalScope('videosCount', function ($builder) {
- // $builder->withCount('videos');
- // });
- // }
}
diff --git a/app/Services/PreviewGeneratorService.php b/app/Services/PreviewGeneratorService.php
index 47cf81b6..2ecbd3b9 100644
--- a/app/Services/PreviewGeneratorService.php
+++ b/app/Services/PreviewGeneratorService.php
@@ -94,7 +94,7 @@ class PreviewGeneratorService {
$folderResource = $this->getDecodedResource(new FolderResource($folder));
$thumbnail = $folder->series->thumbnail_url ?: $this->defaultThumbnail;
- $isAudio = $folder->isMajorityAudio();
+ $isAudio = $folder->series->primary_media_type === 1;
$fileCount = $folderResource->file_count ?? 0;
$fileType = ($isAudio ? 'Track' : 'Episode') . ($fileCount === 1 ? '' : 's');
$contentString = ($folderResource->series->date_start ? $this->getMediaReleaseSeason($folderResource->series->date_start) . ' • ' : '') . "$fileCount $fileType";
@@ -109,6 +109,7 @@ class PreviewGeneratorService {
'thumbnail_url' => $thumbnail,
'upload_date' => $this->formatDate($folderResource->series->date_created),
'content_string' => $contentString,
+ 'rating' => $folderResource->series->rating,
'tags' => $folderResource->series->folder_tags ? array_map(fn ($tag) => $tag->name, $folderResource->series->folder_tags) : null,
'url' => $request->fullUrl(),
];
diff --git a/database/migrations/2025_05_22_161441_add_media_type_to_series.php b/database/migrations/2025_05_22_161441_add_media_type_to_series.php
new file mode 100644
index 00000000..f8216efb
--- /dev/null
+++ b/database/migrations/2025_05_22_161441_add_media_type_to_series.php
@@ -0,0 +1,26 @@
+<?php
+
+use App\Enums\MediaType;
+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->tinyInteger('primary_media_type')->default(MediaType::VIDEO);
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void {
+ Schema::table('series', function (Blueprint $table) {
+ $table->dropColumn('primary_media_type');
+ });
+ }
+};
diff --git a/resources/js/types/resources.ts b/resources/js/types/resources.ts
index 07679280..e1b90c47 100644
--- a/resources/js/types/resources.ts
+++ b/resources/js/types/resources.ts
@@ -28,6 +28,7 @@ export interface FolderResource {
path: string;
file_count: number;
total_size: number;
+ is_majority_audio: boolean;
category_id: number;
videos?: VideoResource[];
series?: SeriesResource;