Skip to content

A self-hosted media server and file management system

feat: support toggle of embed jobs

aminnausind67d2cb4434c
diff --git a/app/Jobs/EmbedUidInMetadata.php b/app/Jobs/EmbedUidInMetadata.php
index 6dc76e1e..b92e1f26 100644
--- a/app/Jobs/EmbedUidInMetadata.php
+++ b/app/Jobs/EmbedUidInMetadata.php
@@ -5,6 +5,7 @@ namespace App\Jobs;
use App\Enums\TaskStatus;
use App\Models\SubTask;
use App\Models\Video;
+use App\Services\Server\ServerConfigService;
use App\Services\TaskService;
use Illuminate\Queue\Attributes\DeleteWhenMissingModels;
use Illuminate\Support\Facades\Log;
@@ -13,11 +14,11 @@ use Symfony\Component\Process\Process;
#[DeleteWhenMissingModels]
class EmbedUidInMetadata extends ManagedSubTask {
- protected $filePath;
+ protected string $filePath;
- protected $uuid;
+ protected string $uuid;
- protected $videoId;
+ protected int $videoId;
public $timeout = 86400;
@@ -26,7 +27,7 @@ class EmbedUidInMetadata extends ManagedSubTask {
*
* @return void
*/
- public function __construct($filePath, $uuid, $taskId, $videoId = null) {
+ public function __construct(string $filePath, string $uuid, int $taskId, $videoId = null) {
$this->filePath = $filePath;
$this->uuid = $uuid;
$this->videoId = $videoId;
@@ -40,11 +41,11 @@ class EmbedUidInMetadata extends ManagedSubTask {
/**
* Execute the job.
*/
- public function handle(TaskService $taskService): void {
+ public function handle(TaskService $taskService, ServerConfigService $config): void {
$this->beginSubTask($taskService, "Adding uuid to $this->filePath");
try {
- $summary = $this->handleEmbed();
+ $summary = $this->handleEmbed($config);
$task = $this->completeSubTask($taskService, $summary);
if ($this->videoId) {
@@ -84,7 +85,11 @@ class EmbedUidInMetadata extends ManagedSubTask {
}
}
- private function handleEmbed() {
+ private function handleEmbed(ServerConfigService $config) {
+ if (! $config->get('uuid_embed')) {
+ return 'UUID embed jobs are not enabled';
+ }
+
$ext = pathinfo($this->filePath, PATHINFO_EXTENSION);
dump("Adding uuid $this->uuid to $this->filePath with extension $ext");
diff --git a/app/Jobs/IndexFiles.php b/app/Jobs/IndexFiles.php
index b24d20a9..40b56ec3 100755
--- a/app/Jobs/IndexFiles.php
+++ b/app/Jobs/IndexFiles.php
@@ -253,7 +253,7 @@ class IndexFiles extends ManagedSubTask {
$data = Storage::json('categories.json') ?? ['next_ID' => 1, 'categoryStructure' => []]; // array("anime"=>1,"tv"=>2,"yogscast"=>3); // read from json
$scanned = array_filter(
scandir($path),
- fn($item) => $item !== '.' &&
+ fn ($item) => $item !== '.' &&
$item !== '..' &&
is_dir($path . DIRECTORY_SEPARATOR . $item)
); // read folder structure
@@ -554,7 +554,7 @@ class IndexFiles extends ManagedSubTask {
->orderBy('updated_at', 'desc')
->get(['uuid', 'video_id', 'composite_id', 'logical_composite_id', 'updated_at'])
->groupBy('logical_composite_id')
- ->map(fn($group) => $group->first())
+ ->map(fn ($group) => $group->first())
->all();
// Creates insert and upsert transactions for videos and metadata
@@ -580,7 +580,9 @@ class IndexFiles extends ManagedSubTask {
['uuid' => $uuid, 'willEmbedUuid' => $willEmbedUuid, 'willReplaceMissing' => $willReplaceMissing] = $this->resolveExistingUuid($embeddedUuid, $compositeId, $logicalCompositeId, $deletedVideoIds, $metadataByUuid, $metadataByComposite);
if ($willEmbedUuid) {
- $this->embedChain[] = new EmbedUidInMetadata($absolutePath, $uuid, $this->taskId, $currentID); // TODO: Make tagging user configurable, probably by library but always use a uuid
+ if ($this->config->get('uuid_embed', true)) {
+ $this->embedChain[] = new EmbedUidInMetadata($absolutePath, $uuid, $this->taskId, $currentID); // TODO: Make tagging user configurable, probably by library but always use a uuid
+ }
$rawMetadata['tags']['uuid'] = $uuid; // Backfill uuid into cached metadata if it did not exist at time of probe
}
@@ -636,7 +638,7 @@ class IndexFiles extends ManagedSubTask {
private function resolveExistingUuid(?string $embeddedUuid, string $compositeId, string $logicalCompositeId, array $deletedIds, array $metadataByUuid, array $metadataByComposite): array {
// sanitise input
$embeddedUuid = $embeddedUuid && Uuid::isValid($embeddedUuid) ? $embeddedUuid : null;
- $hasExistingVideo = fn($metadata) => $metadata->video_id !== null && ! in_array($metadata->video_id, $deletedIds, true);
+ $hasExistingVideo = fn ($metadata) => $metadata->video_id !== null && ! in_array($metadata->video_id, $deletedIds, true);
/**
* 1: has uuid, and matches metadata by uuid
@@ -735,5 +737,4 @@ class IndexFiles extends ManagedSubTask {
return true;
}
}
-class BatchCancelledException extends \Exception {
-}
+class BatchCancelledException extends \Exception {}
diff --git a/app/Jobs/VerifyFiles.php b/app/Jobs/VerifyFiles.php
index ee7bcd41..dc46f10c 100755
--- a/app/Jobs/VerifyFiles.php
+++ b/app/Jobs/VerifyFiles.php
@@ -9,8 +9,11 @@ use App\Jobs\Utility\Subtitles\ScanSubtitles;
use App\Models\Metadata;
use App\Models\Record;
use App\Models\SubTask;
+use App\Models\Video;
+use App\Services\Server\ServerConfigService;
use App\Services\TaskService;
use Carbon\Carbon;
+use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
@@ -29,21 +32,23 @@ class VerifyFiles extends ManagedSubTask {
/**
* Create a new job instance.
+ *
+ * @param Collection<int, Video> $videos
*/
- public function __construct(public $videos, int $taskId) {
+ public function __construct(public Collection $videos, int $taskId) {
$subTask = SubTask::create(['task_id' => $taskId, 'status' => TaskStatus::PENDING, 'name' => 'Verify ' . count($videos) . ' Files']);
$this->taskId = $taskId;
$this->subTaskId = $subTask->id;
}
- public function handle(TaskService $taskService): void {
+ public function handle(TaskService $taskService, ServerConfigService $config): void {
if (! $this->beginSubTask($taskService)) {
return;
}
try {
- $summary = $this->verifyFiles($taskService);
+ $summary = $this->verifyFiles($taskService, $config);
foreach ($this->scannedDirectories as $folderPath => $dirData) {
if (empty($dirData['targets'])) {
@@ -85,7 +90,7 @@ class VerifyFiles extends ManagedSubTask {
}
}
- private function verifyFiles(TaskService $taskService) {
+ private function verifyFiles(TaskService $taskService, ServerConfigService $config) {
$metadataTransactions = [];
$error = false;
@@ -118,7 +123,7 @@ class VerifyFiles extends ManagedSubTask {
// handle missing or invalid Uuid
if (! Uuid::isValid($uuid ?? '')) {
- $uuid = $this->resolveMediaUuid($video, $filePath); // Calls confirm metadata and will always lead to an ffprobe call
+ $uuid = $this->resolveMediaUuid($config, $video, $filePath); // Calls confirm metadata and will always lead to an ffprobe call
}
/**
@@ -576,7 +581,7 @@ class VerifyFiles extends ManagedSubTask {
return $result;
}
- protected function extractMimeType($filePath) {
+ protected function extractMimeType(string $filePath) {
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->file($filePath);
@@ -597,7 +602,7 @@ class VerifyFiles extends ManagedSubTask {
return $mimeType;
}
- public static function getAbsoluteMediaPath($media) {
+ public static function getAbsoluteMediaPath(Video $media) {
$path = str_starts_with($media->path, 'storage/')
? substr($media->path, 8)
: $media->path;
@@ -605,17 +610,27 @@ class VerifyFiles extends ManagedSubTask {
return str_replace('\\', '/', Storage::disk('public')->path($path));
}
- protected function resolveMediaUuid($media, $filePath) {
+ protected function resolveMediaUuid(ServerConfigService $config, Video $media, string $filePath): string {
// if the media in db or file does not have a valid uuid, it will add it in both the db and on the file.
$this->confirmMetadata($filePath, 'resolve uuid');
- if (! isset($this->fileMetaData['tags']['uuid'])) {
- $uuid = Str::uuid()->toString();
- $this->fileMetaData['tags']['uuid'] = $uuid;
- $this->embedChain[] = new EmbedUidInMetadata($filePath, $uuid, $this->taskId, $media->id);
- } else {
+
+ if (isset($this->fileMetaData['tags']['uuid'])) {
$uuid = $this->fileMetaData['tags']['uuid'];
+
+ // If embedding, uuid is added to the media row in the job, but if found on the file, it is manually added here
+ // This means it was already embedded at some point but was not in the Videos table
+ $media->update(['uuid' => $uuid]);
+
dump("Found UUID on file {$uuid}");
- $media->update(['uuid' => $uuid]); // If embedding, media file is updated in the embed job
+
+ return $uuid;
+ }
+
+ $uuid = Str::uuid()->toString();
+ $this->fileMetaData['tags']['uuid'] = $uuid;
+
+ if ($config->get('uuid_embed', true)) {
+ $this->embedChain[] = new EmbedUidInMetadata($filePath, $uuid, $this->taskId, $media->id);
}
return $uuid;