Skip to content

A self-hosted media server and file management system

new verify folders untested lol

aminnausin9d4aa4260844
diff --git a/app/Http/Controllers/DirectoryController.php b/app/Http/Controllers/DirectoryController.php
index aa9a9e74..04c5aa95 100755
--- a/app/Http/Controllers/DirectoryController.php
+++ b/app/Http/Controllers/DirectoryController.php
@@ -4,12 +4,14 @@ namespace App\Http\Controllers;
use App\Http\Requests\VideoCollectionRequest;
use App\Http\Resources\FolderResource;
+use App\Http\Resources\SeriesResource;
use App\Http\Resources\VideoResource;
use App\Jobs\CleanFolderPaths;
use App\Jobs\CleanVideoPaths;
use App\Jobs\IndexFiles;
use App\Jobs\SyncFiles;
use App\Jobs\VerifyFiles;
+use App\Jobs\VerifyFolders;
use App\Models\Category;
use App\Models\Folder;
use App\Models\Video;
@@ -81,7 +83,7 @@ class DirectoryController extends Controller
}
$videoList = VideoResource::collection( Video::where('folder_id', $folderRaw->id)->get());
- $data['folder'] = array('id'=>$folderRaw->id, 'name'=>$folderRaw->name, 'videos'=>$videoList);
+ $data['folder'] = array('id'=>$folderRaw->id, 'name'=>$folderRaw->name, 'series'=>(isset($folderRaw->series) ? new SeriesResource($folderRaw->series) : null), 'videos'=>$videoList);
return $this->success($data, '', 200);
@@ -170,11 +172,53 @@ class DirectoryController extends Controller
}
Bus::batch($jobs)->dispatch();
- dump('This job has no web output. Check queue listener console for updates.');
+ dump('verifyFiles : This job has no web output. Check queue listener console for updates.');
} catch (\Throwable $th) {
dump('Error cannot verify file metadata');
dump($th);
}
+
+ try {
+ $jobs = [];
+ $chunks = [];
+
+ Folder::orderBy('id')->chunk(20, function($folders) use (&$chunks) {
+ $chunks[] = $folders;
+ });
+
+ foreach($chunks as $chunk){
+ $jobs[] = new VerifyFolders($chunk);
+ // break;
+ }
+
+ Bus::batch($jobs)->dispatch();
+ dump('verifyFolders : This job has no web output. Check queue listener console for updates.');
+ } catch (\Throwable $th) {
+ dump('Error cannot verify folder series data');
+ dump($th);
+ }
+ }
+
+ public function verifyFolders(Request $request){
+ try {
+ $jobs = [];
+ $chunks = [];
+
+ Folder::orderBy('id')->chunk(20, function($folders) use (&$chunks) {
+ $chunks[] = $folders;
+ });
+
+ foreach($chunks as $chunk){
+ $jobs[] = new VerifyFolders($chunk);
+ // break;
+ }
+
+ Bus::batch($jobs)->dispatch();
+ dump('verifyFolders : This job has no web output. Check queue listener console for updates.');
+ } catch (\Throwable $th) {
+ dump('Error cannot verify folder series data');
+ dump($th);
+ }
}
public function cleanPaths() {
diff --git a/app/Jobs/VerifyFiles.php b/app/Jobs/VerifyFiles.php
index 27f1c69f..6679902e 100755
--- a/app/Jobs/VerifyFiles.php
+++ b/app/Jobs/VerifyFiles.php
@@ -2,6 +2,7 @@
namespace App\Jobs;
+use App\Models\Metadata;
use App\Models\Record;
use App\Models\Video;
use Illuminate\Bus\Batchable;
@@ -13,6 +14,8 @@ use Illuminate\Queue\SerializesModels;
use FFMpeg\FFProbe as FFMpegFFProbe;
use Illuminate\Support\Facades\Storage;
+use function PHPUnit\Framework\isNull;
+
class VerifyFiles implements ShouldQueue
{
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@@ -47,34 +50,36 @@ class VerifyFiles implements ShouldQueue
$stored = array();
$changes = array();
- $stored = $video->toArray();
+ $metadata = Metadata::firstOrCreate(['composite_id' => $video->folder->path . "/" . basename($video->path)], ['video_id' => $video->id]);
- preg_match('![sS][0-9]+!', $stored['name'], $seasonRaw);
- preg_match('![eE][0-9]+!', $stored['name'], $episodeRaw);
+ $stored = $metadata->toArray();
+ preg_match('![sS][0-9]+!', $video->name, $seasonRaw);
+ preg_match('![eE][0-9]+!', $video->name, $episodeRaw);
preg_match('![0-9]+!', $seasonRaw[0] ?? '', $season);
preg_match('![0-9]+!', $episodeRaw[0] ?? '', $episode);
- if(is_null($video->duration)){
- // dump(Storage::path('') . 'public//' . substr($video->path, 8));
+ if(is_null($metadata->duration)){
+ // dump(str_replace('\\', '/', Storage::path('')) . 'public/' . substr($video->path, 8));
$ffprobe = FFMpegFFProbe::create();
$duration = floor($ffprobe
- ->format(Storage::path('') . 'public/' . substr($video->path, 8)) // extracts file informations
+ ->format(str_replace('\\', '/', Storage::path('')) . 'public/' . substr($video->path, 8)) // extracts file information
->get('duration'));
$changes['duration'] = $duration;
}
- if(is_null($video->episode) && count($episode) == 1) $changes['episode'] = (int)$episode[0];
- if(is_null($video->season) && count($season) == 1) $changes['season'] = (int)$season[0];
+ if(is_null($metadata->episode) && count($episode) == 1) $changes['episode'] = (int)$episode[0];
+ if(is_null($metadata->season) && count($season) == 1) $changes['season'] = (int)$season[0];
- if(is_null($video->title)){
+ if(is_null($metadata->title)){
$newTitle = count($season) == 1 ? 'S' . $season[0] : '';
$newTitle .= count($episode) == 1 ? 'E' . $episode[0] : '';
if($newTitle != '') $changes['title'] = $newTitle;
+ else $changes['title'] = $video->name;
}
- is_null($video->view_count) ? $changes['view_count'] = Record::where('video_id', $video->id)->count() : $stored['view_count'] = $video->view_count;
+ is_null($metadata->view_count) ? $changes['view_count'] = Record::where('video_id', $video->id)->count() + ($metadata->id ? Record::where('metadata_id', $metadata->id)->count() : 0) : $stored['view_count'] = $metadata->view_count;
if(count($changes) > 0){
array_push($transactions, [...$stored, ...$changes]);
@@ -82,6 +87,7 @@ class VerifyFiles implements ShouldQueue
// dump($changes);
// dump($video->name);
}
+ // dump($metadata->toArray());
} catch (\Throwable $th) {
//throw $th;
@@ -91,9 +97,14 @@ class VerifyFiles implements ShouldQueue
}
}
- if(count($transactions) == 0 || $error == true) return;
- Video::upsert($transactions, 'id', ['title','duration','season','episode','view_count']);
- dump('Updated ' . count($transactions) . ' videos from id ' . ($transactions[0]['id']) . ' to ' . ($transactions[count($transactions) - 1]['id']));
+ try {
+ if(count($transactions) == 0 || $error == true) return;
+ Metadata::upsert($transactions, 'id' , ['video_id','title','duration','season','episode','view_count']);
+ // Video::upsert($transactions, 'id', ['title','duration','season','episode','view_count']);
+ dump('Updated ' . count($transactions) . ' videos from id ' . ($transactions[0]['video_id']) . ' to ' . ($transactions[count($transactions) - 1]['video_id']));
+ } catch (\Throwable $th) {
+ dump('Error cannot insert verified file metadata ' . $th->getMessage() . ' Cancelling ' . count($transactions) . ' updates');
+ }
}
}
diff --git a/app/Jobs/VerifyFolders.php b/app/Jobs/VerifyFolders.php
new file mode 100644
index 00000000..6e55b069
--- /dev/null
+++ b/app/Jobs/VerifyFolders.php
@@ -0,0 +1,84 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\Series;
+use App\Models\Video;
+use Illuminate\Bus\Batchable;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class VerifyFolders implements ShouldQueue
+{
+ use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+ /**
+ * Create a new job instance.
+ */
+ public function __construct(public $folders)
+ {
+ //
+ }
+
+ /**
+ * Execute the job.
+ */
+ public function handle(): void
+ {
+ if ($this->batch()->cancelled()) {
+ // Determine if the batch has been cancelled...
+ return;
+ }
+
+ if(count($this->folders) == 0){
+ dump('Folder Data Lost');
+ return;
+ }
+
+ $transactions = array();
+ $error = false;
+ foreach ($this->folders as $folder) {
+ try {
+ $stored = array();
+ $changes = array();
+
+ $series = Series::firstOrCreate(['composite_id' => $folder->path], ['folder_id' => $folder->id]);
+
+ $stored = $series->toArray();
+
+ if(is_null($series->episodes)) $changes['episodes'] = Video::where('folder_id', $folder->id)->count();
+
+ if(is_null($series->title)){
+ $changes['title'] = $folder->name;
+ }
+
+ if(count($changes) > 0){
+ array_push($transactions, [...$stored, ...$changes]);
+ // dump([...$stored, ...$changes]);
+ // dump($changes);
+ // dump($folder->name);
+ }
+ // dump($series->toArray());
+
+ } catch (\Throwable $th) {
+ //throw $th;
+ dump('Error cannot verify folder series data ' . $th->getMessage() . ' Cancelling ' . count($transactions) . ' updates');
+ $error = true;
+ break;
+ }
+ }
+
+ try {
+ if(count($transactions) == 0 || $error == true) return;
+ Series::upsert($transactions, 'id' , ['folder_id','title','episodes']);
+ // Video::upsert($transactions, 'id', ['title','duration','season','episode','view_count']);
+ dump('Updated ' . count($transactions) . ' folders from id ' . ($transactions[0]['folder_id']) . ' to ' . ($transactions[count($transactions) - 1]['folder_id']));
+ } catch (\Throwable $th) {
+ dump('Error cannot insert verified folder series data ' . $th->getMessage() . ' Cancelling ' . count($transactions) . ' updates');
+ }
+ }
+}
+