Skip to content

A self-hosted media server and file management system

feat: simple folder size history (no ui)

aminnausin627e0dd5cfe0
diff --git a/app/Jobs/VerifyFolders.php b/app/Jobs/VerifyFolders.php
index 34cb4f87..b0249d52 100644
--- a/app/Jobs/VerifyFolders.php
+++ b/app/Jobs/VerifyFolders.php
@@ -5,8 +5,10 @@ namespace App\Jobs;
use App\Enums\TaskStatus;
use App\Exceptions\DataLostException;
use App\Models\Series;
+use App\Models\SeriesSizeHistory;
use App\Models\SubTask;
use App\Services\TaskService;
+use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
@@ -45,6 +47,8 @@ class VerifyFolders extends ManagedSubTask {
}
$transactions = [];
+ $sizeHistoryTransactions = [];
+
$error = false;
$index = 0;
@@ -57,14 +61,20 @@ class VerifyFolders extends ManagedSubTask {
try {
$stored = [];
$changes = [];
+ $sizeHistoryChanges = [];
$series = Series::firstOrCreate(['composite_id' => $folder->path], ['folder_id' => $folder->id]);
$stored = $series->toArray();
- $episodeCount = $folder->videos->count();
- if ($stored['episodes'] !== $episodeCount) {
- $changes['episodes'] = $episodeCount;
+ $relatedFileCount = $folder->videos->count();
+ if (! isset($stored['episodes'])) { // Only set episode count initially, since it is a user editable field
+ $changes['episodes'] = $relatedFileCount;
+ }
+
+ if ($relatedFileCount !== $stored['file_count']) {
+ $changes['file_count'] = $relatedFileCount;
+ $sizeHistoryChanges['file_count'] = $relatedFileCount;
}
$primary_media_type = $folder->primary_media_type;
@@ -75,6 +85,7 @@ class VerifyFolders extends ManagedSubTask {
$totalSize = $folder->total_size;
if ($stored['total_size'] !== $totalSize) {
$changes['total_size'] = $totalSize;
+ $sizeHistoryChanges['total_bytes'] = $totalSize;
}
if (is_null($series->title)) {
@@ -108,7 +119,7 @@ class VerifyFolders extends ManagedSubTask {
if (! empty($changes)) {
$changes['updated_at'] = now();
- array_push($transactions, [...$stored, ...$changes]);
+ $transactions[] = [...$stored, ...$changes];
/**
* DEBUG
*
@@ -117,6 +128,9 @@ class VerifyFolders extends ManagedSubTask {
* dump($folder->name);
*/
}
+ if (! empty($sizeHistoryChanges)) {
+ $sizeHistoryTransactions[] = ['series_id' => $series->id, 'total_bytes' => $totalSize, 'file_count' => $relatedFileCount, 'recorded_at' => now()];
+ }
$index += 1;
$taskService->updateSubTask($this->subTaskId, ['progress' => (int) (($index / count($this->folders)) * 100)]);
@@ -128,11 +142,18 @@ class VerifyFolders extends ManagedSubTask {
}
try {
- if (empty($transactions) || $error) {
+ if ($error || empty($transactions)) { // size history transactions rely on there being folder changes because they are only created on changes
return 'No Changes Found';
}
- Series::upsert($transactions, 'id', ['folder_id', 'title', 'episodes', 'thumbnail_url', 'raw_thumbnail_url', 'total_size', 'primary_media_type', 'updated_at']);
+ DB::beginTransaction();
+
+ Series::upsert($transactions, 'id', ['folder_id', 'title', 'episodes', 'thumbnail_url', 'raw_thumbnail_url', 'total_size', 'file_count', 'primary_media_type', 'updated_at']);
+
+ if (! empty($sizeHistoryTransactions)) {
+ SeriesSizeHistory::insert($sizeHistoryTransactions);
+ }
+ DB::commit();
return 'Updated ' . count($transactions) . ' folders from id ' . ($transactions[0]['folder_id']) . ' to ' . ($transactions[count($transactions) - 1]['folder_id']);
} catch (\Throwable $th) {
diff --git a/app/Models/Series.php b/app/Models/Series.php
index bba0ca41..1bed570b 100644
--- a/app/Models/Series.php
+++ b/app/Models/Series.php
@@ -37,6 +37,8 @@ class Series extends Model {
* edited_at -> timestamp (nullable)
* started_at -> date (nullable)
* ended_at -> date (nullable)
+ *
+ * file_count -> uint4 (default=0)
*/
protected $fillable = [
'folder_id',
@@ -51,6 +53,7 @@ class Series extends Model {
'films',
'started_at',
'ended_at',
+ 'file_count',
'thumbnail_url',
'edited_at',
];
@@ -71,6 +74,10 @@ class Series extends Model {
return $this->hasMany(FolderTag::class);
}
+ public function sizeHistory(): HasMany {
+ return $this->hasMany(SeriesSizeHistory::class);
+ }
+
protected function getEditableFields(): array {
return [
'editor_id',
diff --git a/app/Models/SeriesSizeHistory.php b/app/Models/SeriesSizeHistory.php
new file mode 100644
index 00000000..60877908
--- /dev/null
+++ b/app/Models/SeriesSizeHistory.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+
+class SeriesSizeHistory extends Model {
+ protected $fillable = [
+ 'series_id',
+ 'total_bytes',
+ 'file_count',
+ 'recorded_at',
+ ];
+
+ protected $casts = [
+ 'recorded_at' => 'datetime',
+ ];
+
+ public function series(): BelongsTo {
+ return $this->belongsTo(Series::class);
+ }
+}
diff --git a/database/migrations/2026_02_01_222526_create_series_size_history_table.php b/database/migrations/2026_02_01_222526_create_series_size_history_table.php
new file mode 100644
index 00000000..edfbabcd
--- /dev/null
+++ b/database/migrations/2026_02_01_222526_create_series_size_history_table.php
@@ -0,0 +1,41 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration {
+ /**
+ * Run the migrations.
+ */
+ public function up(): void {
+ Schema::create('series_size_histories', function (Blueprint $table) {
+ $table->id();
+ $table->foreignId('series_id')->constrained()->cascadeOnDelete();
+ $table->unsignedBigInteger('total_bytes');
+ $table->unsignedInteger('file_count');
+ $table->timestampTz('recorded_at');
+ $table->timestamptz('created_at')->default(now());
+ });
+
+ DB::statement('
+ INSERT INTO series_size_histories (series_id, total_bytes, file_count, recorded_at)
+ SELECT
+ id,
+ total_size,
+ GREATEST(COALESCE(file_count, 0), COALESCE(episodes, 0)),
+ NOW()
+ FROM series
+ WHERE total_size IS NOT NULL
+ AND folder_id IS NOT NULL;
+ ');
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void {
+ Schema::dropIfExists('series_size_histories');
+ }
+};