Skip to content

A self-hosted media server and file management system

feat: basic config backend

aminnausin20ef9c73f9b4
diff --git a/app/Http/Controllers/Api/V1/Server/ServerConfigController.php b/app/Http/Controllers/Api/V1/Server/ServerConfigController.php
new file mode 100644
index 00000000..c5496471
--- /dev/null
+++ b/app/Http/Controllers/Api/V1/Server/ServerConfigController.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace App\Http\Controllers\Api\V1\Server;
+
+use App\Http\Controllers\Controller;
+use App\Http\Requests\Server\UpdateMediaConfigRequest;
+use App\Http\Requests\Server\UpdatePerformanceConfigRequest;
+use App\Http\Requests\Server\UpdateScannerConfigRequest;
+use App\Http\Requests\Server\UpdateStorageConfigRequest;
+use App\Http\Resources\Server\ServerConfigResource;
+use App\Models\ServerConfig;
+use App\Services\Server\QueueControlService;
+use App\Services\Server\ServerConfigService;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Support\Facades\Cache;
+
+class ServerConfigController extends Controller {
+ public function __construct(private ServerConfigService $config) {}
+
+ public function index(): JsonResponse {
+ Cache::forget('server_config:all');
+
+ return response()->json(
+ ServerConfigResource::collection(ServerConfig::all())->groupBy('group')->map(fn ($group) => $group->keyBy('key'))
+ );
+ }
+
+ public function updateScanner(UpdateScannerConfigRequest $request) {
+ $validated = $request->validated();
+
+ $this->config->setMany($validated);
+
+ return response()->noContent();
+ }
+
+ public function updatePerformance(UpdatePerformanceConfigRequest $request, QueueControlService $queueController) {
+ $validated = $request->validated();
+
+ $this->config->setMany($validated);
+ $queueController->restart();
+
+ return response()->noContent();
+ }
+
+ public function updateStorage(UpdateStorageConfigRequest $request) {
+ $validated = $request->validated();
+
+ $this->config->setMany($validated);
+
+ return response()->noContent();
+ }
+
+ public function updateMedia(UpdateMediaConfigRequest $request) {
+ $validated = $request->validated();
+
+ $this->config->set('media_formats', $validated);
+
+ return response()->noContent();
+ }
+}
diff --git a/app/Http/Requests/Server/UpdateMediaConfigRequest.php b/app/Http/Requests/Server/UpdateMediaConfigRequest.php
new file mode 100644
index 00000000..f4ea880d
--- /dev/null
+++ b/app/Http/Requests/Server/UpdateMediaConfigRequest.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Http\Requests\Server;
+
+use App\Rules\ValidMediaFormatMap;
+use Illuminate\Foundation\Http\FormRequest;
+
+class UpdateMediaConfigRequest extends FormRequest {
+ /**
+ * Determine if the user is authorized to make this request.
+ */
+ public function authorize(): bool {
+ return $this->user()?->isAdmin();
+ }
+
+ /**
+ * Get the validation rules that apply to the request.
+ * Gets category and potentially folder id from query strings
+ *
+ * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+ */
+ public function rules(): array {
+ return [
+ 'media_formats' => ['nullable', new ValidMediaFormatMap],
+ ];
+ }
+}
diff --git a/app/Http/Requests/Server/UpdatePerformanceConfigRequest.php b/app/Http/Requests/Server/UpdatePerformanceConfigRequest.php
new file mode 100644
index 00000000..3bd19c59
--- /dev/null
+++ b/app/Http/Requests/Server/UpdatePerformanceConfigRequest.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Http\Requests\Server;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class UpdatePerformanceConfigRequest extends FormRequest {
+ /**
+ * Determine if the user is authorized to make this request.
+ */
+ public function authorize(): bool {
+ return $this->user()?->isAdmin();
+ }
+
+ /**
+ * Get the validation rules that apply to the request.
+ * Gets category and potentially folder id from query strings
+ *
+ * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+ */
+ public function rules(): array {
+ return [
+ 'max_scan_workers' => 'integer|min:1|max:16|required',
+ 'max_event_workers' => 'integer|min:1|max:8|required',
+ ];
+ }
+}
diff --git a/app/Http/Requests/Server/UpdateScannerConfigRequest.php b/app/Http/Requests/Server/UpdateScannerConfigRequest.php
new file mode 100644
index 00000000..11563db6
--- /dev/null
+++ b/app/Http/Requests/Server/UpdateScannerConfigRequest.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Http\Requests\Server;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class UpdateScannerConfigRequest extends FormRequest {
+ /**
+ * Determine if the user is authorized to make this request.
+ */
+ public function authorize(): bool {
+ return $this->user()?->isAdmin();
+ }
+
+ /**
+ * Get the validation rules that apply to the request.
+ * Gets category and potentially folder id from query strings
+ *
+ * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+ */
+ public function rules(): array {
+ return [
+ 'uuid_embed' => 'boolean|required',
+ 'uuid_write_cache' => 'boolean|required',
+ 'attachments_extract' => 'boolean|required',
+ 'art_extract' => 'boolean|required',
+ 'thumbnails_generate' => 'boolean|required',
+ ];
+ }
+}
diff --git a/app/Http/Requests/Server/UpdateStorageConfigRequest.php b/app/Http/Requests/Server/UpdateStorageConfigRequest.php
new file mode 100644
index 00000000..84f9d951
--- /dev/null
+++ b/app/Http/Requests/Server/UpdateStorageConfigRequest.php
@@ -0,0 +1,43 @@
+<?php
+
+namespace App\Http\Requests\Server;
+
+use Closure;
+use Illuminate\Foundation\Http\FormRequest;
+
+class UpdateStorageConfigRequest extends FormRequest {
+ /**
+ * Determine if the user is authorized to make this request.
+ */
+ public function authorize(): bool {
+ return $this->user()?->isAdmin();
+ }
+
+ /**
+ * Get the validation rules that apply to the request.
+ * Gets category and potentially folder id from query strings
+ *
+ * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
+ */
+ public function rules(): array {
+ return [
+ 'cache_path' => ['nullable', 'string', function ($attr, $value, $fail) {
+ $this->validatePath($value, $fail);
+ }],
+ 'metadata_path' => ['nullable', 'string', function ($attr, $value, $fail) {
+ $this->validatePath($value, $fail);
+ }],
+ ];
+ }
+
+ /**
+ * Validates existance of a given path on the local (private) disk
+ *
+ * @return bool
+ */
+ private function validatePath(string $path, Closure $fail) {
+ if ($path && ! is_dir($path)) {
+ $fail("The path '$path' does not exist on the server.");
+ }
+ }
+}
diff --git a/app/Http/Resources/Server/ServerConfigResource.php b/app/Http/Resources/Server/ServerConfigResource.php
new file mode 100644
index 00000000..b4bc3fb9
--- /dev/null
+++ b/app/Http/Resources/Server/ServerConfigResource.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Http\Resources\Server;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class ServerConfigResource extends JsonResource {
+ /**
+ * Transform the resource into an array.
+ *
+ * @return array<string, mixed>
+ */
+ public function toArray(Request $request): array {
+ return [
+ 'key' => $this->key,
+ 'value' => json_decode($this->value, true),
+ 'default_value' => json_decode($this->default_value, true),
+ 'type' => $this->type,
+ 'group' => $this->group,
+ ];
+ }
+}
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index c2cbee78..a3c21b54 100755
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -3,6 +3,7 @@
namespace App\Providers;
use App\Models\User;
+use App\Services\Server\ServerConfigService;
use Dedoc\Scramble\Scramble;
use GuzzleHttp\Client;
use Illuminate\Auth\Notifications\ResetPassword;
@@ -26,6 +27,8 @@ class AppServiceProvider extends ServiceProvider {
});
}
+ $this->app->singleton(ServerConfigService::class);
+
Scramble::ignoreDefaultRoutes();
}
diff --git a/app/Rules/ValidMediaFormatMap.php b/app/Rules/ValidMediaFormatMap.php
new file mode 100644
index 00000000..147ba477
--- /dev/null
+++ b/app/Rules/ValidMediaFormatMap.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace App\Rules;
+
+use Closure;
+use Illuminate\Contracts\Validation\ValidationRule;
+
+class ValidMediaFormatMap implements ValidationRule {
+ /**
+ * Run the validation rule.
+ *
+ * @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
+ */
+ public function validate(string $attribute, mixed $value, Closure $fail): void {
+ if (! is_array($value)) {
+ $fail('The format map must be an array.');
+
+ return;
+ }
+
+ foreach ($value as $key => $container) {
+ if (! preg_match('/^[a-z0-9]{1,10}$/', $key)) {
+ $fail("Invalid extension '$key'. Must be lowercase alphanumeric, max 10 characters.");
+ }
+
+ if (! is_string($container) || empty($container)) {
+ $fail("Invalid container value for '$key'. Must be a non-empty string.");
+ }
+ }
+ }
+}
diff --git a/app/Services/Server/QueueControlService.php b/app/Services/Server/QueueControlService.php
new file mode 100644
index 00000000..9c6b52d8
--- /dev/null
+++ b/app/Services/Server/QueueControlService.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace App\Services\Server;
+
+use Illuminate\Support\Facades\Artisan;
+use Laravel\Horizon\Contracts\MasterSupervisorRepository;
+
+class QueueControlService {
+ public function restart(): void {
+ if (config('queue.default') === 'redis' && $this->horizonIsRunning()) {
+ Artisan::call('horizon:restart');
+ }
+ }
+
+ private function horizonIsRunning(): bool {
+ return count(app(MasterSupervisorRepository::class)->all()) > 0;
+ }
+}
diff --git a/app/Services/Server/ServerConfigService.php b/app/Services/Server/ServerConfigService.php
new file mode 100644
index 00000000..b7527a09
--- /dev/null
+++ b/app/Services/Server/ServerConfigService.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace App\Services\Server;
+
+use App\Models\ServerConfig;
+use Illuminate\Support\Collection;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\DB;
+
+class ServerConfigService {
+ public function all(): Collection {
+ return Cache::rememberForever('server_config:all', function () {
+ return ServerConfig::all()->keyBy('key')->map(fn ($record) => [
+ 'value' => json_decode($record->value, true) ?? $record->value,
+ 'default_value' => json_decode($record->default_value, true) ?? $record->default_value,
+ 'type' => $record->type,
+ 'group' => $record->group,
+ ]);
+ });
+ }
+
+ public function get(string $key, mixed $default = null): mixed {
+ return $this->all()->get($key)['value'] ?? $default ?? $this->getDefault($key);
+ }
+
+ public function getDefault(string $key): mixed {
+ return $this->all()->get($key)['default_value'] ?? null;
+ }
+
+ public function set(string $key, mixed $value): void {
+ ServerConfig::update(['key' => $key], ['value' => json_encode($value ?? $this->getDefault($key))]);
+ Cache::forget('server_config:all');
+ }
+
+ public function setMany(array $values): void {
+ DB::transaction(function () use ($values) {
+ foreach ($values as $key => $value) {
+ ServerConfig::where('key', $key)->update(['value' => json_encode($value ?? $this->getDefault($key))]);
+ }
+ });
+
+ Cache::forget('server_config:all');
+ }
+}
diff --git a/routes/api.php b/routes/api.php
index 833efb74..194837c1 100755
--- a/routes/api.php
+++ b/routes/api.php
@@ -18,6 +18,7 @@ use App\Http\Controllers\Api\V1\PlaybackProgressController;
use App\Http\Controllers\Api\V1\ProfileController;
use App\Http\Controllers\Api\V1\RecordController;
use App\Http\Controllers\Api\V1\SeriesController;
+use App\Http\Controllers\Api\V1\Server\ServerConfigController;
use App\Http\Controllers\Api\V1\Server\SetupController;
use App\Http\Controllers\Api\V1\SessionController;
use App\Http\Controllers\Api\V1\SubTaskController;
@@ -59,6 +60,15 @@ Route::group(['middleware' => ['auth:sanctum']], function () {
Route::put('/email', [EmailController::class, 'update'])->name('email.update');
});
+ Route::prefix('config')->group(function () {
+ // Account Settings
+ Route::get('/', [ServerConfigController::class, 'index']);
+ Route::patch('/scanner', [ServerConfigController::class, 'updateScanner']);
+ Route::patch('/storage', [ServerConfigController::class, 'updateStorage']);
+ Route::patch('/performance', [ServerConfigController::class, 'updatePerformance']);
+ Route::patch('/media', [ServerConfigController::class, 'updateMedia']);
+ });
+
// Dashboard
Route::get('/active-sessions', [UserController::class, 'sessionCount']);