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
This commit is contained in:
Marco Cavalli 2025-02-11 17:06:15 +01:00
parent 95b414fb80
commit d921da99f4
4 changed files with 112 additions and 17 deletions

View file

@ -0,0 +1,33 @@
<?php
namespace App\Filters;
class FileFilters
{
protected $filters = [
'track_title' => 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));
}
}

View file

@ -0,0 +1,12 @@
<?php
namespace App\Filters;
class GenericFilter
{
function __invoke($query, $whereToSearch, $textToSearch) {
return $query->where(function($query) use ($whereToSearch, $textToSearch) {
$query->whereLike($whereToSearch, $textToSearch);
});
}
}

View file

@ -2,9 +2,11 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Filters\FileFilters;
use App\Helpers\LengthFormatter; use App\Helpers\LengthFormatter;
use App\Lib\RabbitMQSender; use App\Lib\RabbitMQSender;
use App\Models\File; use App\Models\File;
use App\Models\TrackType;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
@ -26,18 +28,35 @@ class FileController extends Controller
'app_id' => 'shortstr', 'app_id' => 'shortstr',
'cluster_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) { public function store(Request $request) {
// $user = Auth::user(); $user = Auth::user();
//
// //Accept request only from logged-in users //Accept request only from logged-in users
// if (!$user) { if (!$user) {
// throw new \Exception("You must be logged in"); throw new \Exception("You must be logged in");
// } }
//Mime type list: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types //Mime type list: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
$request->validate([ $request->validate([
@ -55,7 +74,7 @@ class FileController extends Controller
'md5' => md5($request->file('file')->getRealPath()), '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 '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, '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 '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 '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, 'silan_check' => false,
@ -70,29 +89,56 @@ class FileController extends Controller
RabbitMQSender::SendMessageToAnalyzer( RabbitMQSender::SendMessageToAnalyzer(
$tmpFile->getPathname(), $tmpFile->getPathname(),
Storage::disk('libretime-upload')->path('').'/'.'1', //$user->id, Storage::disk('libretime-upload')->path('').'/'.$user->id,
$originalName, $originalName,
$dbFile->id, $dbFile->id,
$dbFile->track_type_id $dbFile->track_type_id
); );
} }
/**
* Retrieve a single file metadata
* @param $id
* @return mixed
*/
public function show($id) { 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) { 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) { public function destroy($id) {
//ToDo $file = File::whereId($id)->first();
} Storage::disk('libretime')->delete($file->filepath);
$file->delete();
public function upload(Request $request) { return true;
} }
//Test Method, must be deleted before finishing
public function test() { public function test() {
return view('upload'); return view('upload');
} }

View file

@ -89,4 +89,8 @@ class File extends Model
public function trackType() { public function trackType() {
return $this->belongsTo(TrackType::class, 'track_type_id'); return $this->belongsTo(TrackType::class, 'track_type_id');
} }
public function scopeFilter($query, $filters) {
return $filters->apply($query);
}
} }