Skip to content

A self-hosted media server and file management system

further optimise folders

aminnausinef40ce95547d
diff --git a/app/Http/Controllers/Api/V1/CategoryController.php b/app/Http/Controllers/Api/V1/CategoryController.php
index 0d4dc36e..80158ef1 100755
--- a/app/Http/Controllers/Api/V1/CategoryController.php
+++ b/app/Http/Controllers/Api/V1/CategoryController.php
@@ -29,7 +29,7 @@ class CategoryController extends Controller {
return $this->success(
CategoryResource::collection(
- $categories->withCount(['folders', 'videos'])->with(['folders.series'])->get()
+ $categories->with(['folders.series'])->get()
)
);
} catch (\Throwable $th) {
@@ -46,7 +46,7 @@ class CategoryController extends Controller {
*/
public function show(Category $category) {
$category->load(['folders.series']);
- return new CategoryResource($category->withCount(['folders', 'videos']));
+ return new CategoryResource($category);
}
/**
diff --git a/app/Http/Resources/CategoryResource.php b/app/Http/Resources/CategoryResource.php
index 133ffa21..3c6795c7 100755
--- a/app/Http/Resources/CategoryResource.php
+++ b/app/Http/Resources/CategoryResource.php
@@ -4,6 +4,7 @@ namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
+use Illuminate\Support\Facades\Log;
class CategoryResource extends JsonResource {
/**
@@ -12,13 +13,20 @@ class CategoryResource extends JsonResource {
* @return array<string, mixed>
*/
public function toArray(Request $request): array {
+
+ $folders = $this->whenLoaded('folders');
+
+ $videosCount = $folders->sum(function ($folder) {
+ return $folder->video_count ?? $folder->series->episodes;
+ });
+
return [
'id' => (string) $this->id,
'name' => $this->name,
'default_folder_id' => $this->default_folder_id,
- 'folders_count' => $this->folders_count ?? 0,
- 'folders' => FolderResource::collection($this->folders),
- 'videos_count' => $this->videos_count ?? 0,
+ 'folders_count' => count($folders) ?? 0,
+ 'folders' => FolderResource::collection($folders),
+ 'videos_count' => $videosCount ?? 0,
'created_at' => $this->created_at,
'last_scan' => $this->last_scan,
];
diff --git a/app/Http/Resources/FolderResource.php b/app/Http/Resources/FolderResource.php
index 84cd275b..3f343138 100755
--- a/app/Http/Resources/FolderResource.php
+++ b/app/Http/Resources/FolderResource.php
@@ -11,12 +11,13 @@ class FolderResource extends JsonResource {
*
* @return array<string, mixed>
*/
+
public function toArray(Request $request): array {
return [
'id' => $this->id,
'name' => $this->name,
'path' => $this->path,
- 'file_count' => $this->videos_count ?? 0, // $videos->count(),
+ 'file_count' => $this->videos_count ?? $this->series->episodes ?? 0, // $videos->count(),
'total_size' => $this->series->total_size,
'category_id' => $this->category_id,
'videos' => $this->when($request->videos, function () {
@@ -27,29 +28,3 @@ class FolderResource extends JsonResource {
];
}
}
-
-/*
-Mega Yikes AI code
-protected static $categoryCache = [];
-
-$videos = $request->videos ? $this->videos : [];
-
-$totalFileSize = $this->videos->sum(function ($video) {
-return $video->metadata ? $video->metadata->file_size : 0;
-});
-$self = Folder::with('videos.metadata')->find($this->id);
-
-$categoryId = $this->category_id; // Assuming editor_id is the foreign key
-$category = null;
-
-if (isset(self::$categoryCache[$categoryId])) {
- $category = self::$categoryCache[$categoryId];
-} else {
- $category = $this->category;
- self::$categoryCache[$categoryId] = $category;
-}
-
-'total_size' => $this->videos->sum(function ($video) {
- return $video->metadata ? $video->metadata->file_size : 0;
-}),
-*/
diff --git a/app/Models/Folder.php b/app/Models/Folder.php
index e45ff578..de5da3f7 100755
--- a/app/Models/Folder.php
+++ b/app/Models/Folder.php
@@ -31,8 +31,8 @@ class Folder extends Model {
protected static function boot() {
parent::boot(); // Automatic withCount
- static::addGlobalScope('videosCount', function ($builder) {
- $builder->withCount('videos');
- });
+ // static::addGlobalScope('videosCount', function ($builder) {
+ // $builder->withCount('videos');
+ // });
}
}
diff --git a/database/migrations/2025_02_04_204438_create_indexes_on_sorted_columns.php b/database/migrations/2025_02_04_204438_create_indexes_on_sorted_columns.php
new file mode 100644
index 00000000..3ef2e2ab
--- /dev/null
+++ b/database/migrations/2025_02_04_204438_create_indexes_on_sorted_columns.php
@@ -0,0 +1,46 @@
+<?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('categories', function (Blueprint $table) {
+ $table->index('is_private'); // Index for filtering
+ });
+ Schema::table('playbacks', function (Blueprint $table) {
+ $table->index('progress'); // Index for sorting
+ });
+ Schema::table('tasks', function (Blueprint $table) {
+ $table->index('status'); // Index for filtering
+ });
+ Schema::table('sub_tasks', function (Blueprint $table) {
+ $table->index('status'); // Index for filtering
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void {
+ Schema::table('categories', function (Blueprint $table) {
+ $table->dropIndex(['is_private']);
+ });
+
+ Schema::table('playbacks', function (Blueprint $table) {
+ $table->dropIndex(['progress']);
+ });
+
+ Schema::table('tasks', function (Blueprint $table) {
+ $table->dropIndex(['status']);
+ });
+
+ Schema::table('sub_tasks', function (Blueprint $table) {
+ $table->dropIndex(['status']);
+ });
+ }
+};