From d921da99f49681bf23298889cf40f0ad87fe139d Mon Sep 17 00:00:00 2001 From: marcoc Date: Tue, 11 Feb 2025 17:06:15 +0100 Subject: [PATCH] feat(file management): added all CRUD coverage and index with filters Here the filters guide: https://laravel.io/articles/filter-eloquent-models-with-multiple-optional-filters --- app/Filters/FileFilters.php | 33 ++++++++++ app/Filters/GenericFilter.php | 12 ++++ app/Http/Controllers/FileController.php | 80 +++++++++++++++++++------ app/Models/File.php | 4 ++ 4 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 app/Filters/FileFilters.php create mode 100644 app/Filters/GenericFilter.php diff --git a/app/Filters/FileFilters.php b/app/Filters/FileFilters.php new file mode 100644 index 0000000..a25f4dc --- /dev/null +++ b/app/Filters/FileFilters.php @@ -0,0 +1,33 @@ + GenericFilter::class, + 'artist_name' => GenericFilter::class, + 'genre' => GenericFilter::class, + 'label' => GenericFilter::class, + 'composer' => GenericFilter::class, + 'per_page' => 20 + ]; + + public function apply($query) + { + foreach ($this->receivedFilters() as $name => $value) { + if ($name != 'per_page') { + $filterInstance = new $this->filters[$name]; + $query = $filterInstance($query, $name, $value); + } + } + + return $query; + } + + + public function receivedFilters() + { + return request()->only(array_keys($this->filters)); + } +} \ No newline at end of file diff --git a/app/Filters/GenericFilter.php b/app/Filters/GenericFilter.php new file mode 100644 index 0000000..782ca93 --- /dev/null +++ b/app/Filters/GenericFilter.php @@ -0,0 +1,12 @@ +where(function($query) use ($whereToSearch, $textToSearch) { + $query->whereLike($whereToSearch, $textToSearch); + }); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php index 71b242d..84ce636 100644 --- a/app/Http/Controllers/FileController.php +++ b/app/Http/Controllers/FileController.php @@ -2,9 +2,11 @@ namespace App\Http\Controllers; +use App\Filters\FileFilters; use App\Helpers\LengthFormatter; use App\Lib\RabbitMQSender; use App\Models\File; +use App\Models\TrackType; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; @@ -26,18 +28,35 @@ class FileController extends Controller 'app_id' => 'shortstr', 'cluster_id' => 'shortstr', ); - public function index() { - //ToDo + + + public function index(FileFilters $filters) { + + if (!isset($filters->per_page) || is_null($filters)) { + $pagination = 20; + } else { + $pagination = $filters->per_page; + } + + return File::filter($filters)->cursorPaginate($pagination)->toJson(); } + /** + * Save first data into cc_files row and the file in libretime-tmp storage, + * then send a message to Analyzer to move it to libretime-storage (then Analyzer + * call the update method) + * @param Request $request + * @return void + * @throws \Exception + */ public function store(Request $request) { -// $user = Auth::user(); -// -// //Accept request only from logged-in users -// if (!$user) { -// throw new \Exception("You must be logged in"); -// } + $user = Auth::user(); + + //Accept request only from logged-in users + if (!$user) { + throw new \Exception("You must be logged in"); + } //Mime type list: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types $request->validate([ @@ -55,7 +74,7 @@ class FileController extends Controller 'md5' => md5($request->file('file')->getRealPath()), 'length' => $time->format(), //preparare helper che trasforma tempo in 0 years 0 mons 0 days 0 hours 0 mins 0.0 secs 'file_exists' => true, - 'owner_id' => 1, //$user->id, //Get the user ID + 'owner_id' => $user->id, //Get the user ID 'cuein' => $time->format(), //preparare helper che trasforma tempo in 0 years 0 mons 0 days 0 hours 0 mins 0.0 secs 'cueout' => $time->format(), //preparare helper che trasforma tempo in 0 years 0 mons 0 days 0 hours 0 mins 0.0 secs 'silan_check' => false, @@ -70,29 +89,56 @@ class FileController extends Controller RabbitMQSender::SendMessageToAnalyzer( $tmpFile->getPathname(), - Storage::disk('libretime-upload')->path('').'/'.'1', //$user->id, + Storage::disk('libretime-upload')->path('').'/'.$user->id, $originalName, $dbFile->id, $dbFile->track_type_id ); } + /** + * Retrieve a single file metadata + * @param $id + * @return mixed + */ public function show($id) { - //Todo + return File::whereId($id)->toJson(); } + /** + * Update a cc_files row, either with metadata from Analyzer or + * with manually updated metadata + * @param Request $request + * @param $id + * @return mixed + */ public function update(Request $request, $id) { - //ToDo + $file = File::whereId($id)->first(); + + $request->length = new LengthFormatter($request->length); + + //Check if there is a track type id, if not add "MUSIC" id + if (is_null($file->track_type_id)) { + $request->track_type_id = TrackType::where('code','MUSICA')->first()->id; + } + + $file->fill($request->all())->save(); + return $file->toJson(); } + /** + * Delete file from storage and DB + * @param $id + * @return true + */ public function destroy($id) { - //ToDo - } - - public function upload(Request $request) { - + $file = File::whereId($id)->first(); + Storage::disk('libretime')->delete($file->filepath); + $file->delete(); + return true; } + //Test Method, must be deleted before finishing public function test() { return view('upload'); } diff --git a/app/Models/File.php b/app/Models/File.php index b72d8a2..03a4fcd 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -89,4 +89,8 @@ class File extends Model public function trackType() { return $this->belongsTo(TrackType::class, 'track_type_id'); } + + public function scopeFilter($query, $filters) { + return $filters->apply($query); + } }