Skip to content

A self-hosted media server and file management system

chore(refactoring): Add comments and a bit of structure idea for refactoring verify files/folders jobs

aminnausina9e3d4cda609
diff --git a/app/Jobs/VerifyFiles.php b/app/Jobs/VerifyFiles.php
index bdf8ce3f..04042032 100755
--- a/app/Jobs/VerifyFiles.php
+++ b/app/Jobs/VerifyFiles.php
@@ -200,7 +200,10 @@ class VerifyFiles implements ShouldQueue {
}
// if file is of type audio one of the following is true: description and episode is null, codec is null, bitrate is null => generate description from audio tags
$audioMetadata = ((is_null($metadata->description) && is_null($metadata->episode)) || is_null($metadata->bitrate) || is_null($metadata->codec)) && $is_audio ? $this->getAudioDescription($filePath, $this->fileMetaData ?? null) : [];
- if ((is_null($metadata->poster_url) || filemtime($filePath)) && $mime_type && $is_audio) {
+
+ // TODO: if no poster_url is set or file was modified since last update and the file is of type audio, extract image
+ // TODO: if poster_url is set and it is not a local url, download and save as local image
+ if ($is_audio && (is_null($metadata->poster_url) || filemtime($filePath) > $metadata->updated_at)) {
$relativePath = $video->folder->path . '/' . $metadata->id;
$coverArtPath = "posters/audio/$relativePath-$uuid.png";
diff --git a/app/Services/Image/Downloader.php b/app/Services/Image/Downloader.php
new file mode 100644
index 00000000..b7d1df97
--- /dev/null
+++ b/app/Services/Image/Downloader.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Services\Image;
+
+use App\Jobs\VerifyFiles;
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
+
+class Downloader {
+ public function __construct() {
+ }
+
+ // TODO: Eventually use this service with DI in verifyFiles and verifyFolders to download thumbnail urls ... currently using static function in verifyFolders
+ public function importExternalImageAsURL($url, $compositePath) {
+ try {
+ $response = Http::get($url);
+ if ($response->successful()) {
+ dump('Getting thumbnail');
+ $imageContent = $response->body();
+ $path = 'thumbnails/' . $compositePath . '.webp';
+ Storage::disk('public')->put($path, $imageContent);
+
+ return VerifyFiles::getPathUrl($path);
+ }
+ } catch (\Throwable $th) {
+ // throw $th;
+ Log::warning('Unable to download thumbnail image from ' . $url . ' : ' . $th->getMessage());
+ }
+
+ return false;
+ }
+}
diff --git a/app/Support/UrlValidator.php b/app/Support/UrlValidator.php
new file mode 100644
index 00000000..6706d271
--- /dev/null
+++ b/app/Support/UrlValidator.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Support;
+
+use Illuminate\Support\Str;
+
+class UrlValidator {
+ public function isExternalUrl(string $url): bool {
+ // ! strpos($metadata->poster_url, str_replace('http://', '', str_replace('https://', '', config('api.app_url')))
+ return $url && !Str::startsWith($url, config('app.scheme') . "://" . config('app.host'));
+ }
+}