sintonia_webapp/app/Http/Controllers/FileController.php

133 lines
4.1 KiB
PHP

<?php
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\Auth;
use Illuminate\Support\Facades\Storage;
class FileController extends Controller
{
/**
* Return file list, with optional filters, defined in app/Filters/FileFilters
* @param FileFilters $filters
* @return mixed
*/
public function index(FileFilters $filters) {
if (!isset($filters->per_page) || is_null($filters)) {
$pagination = 5;
} else {
$pagination = $filters->per_page;
}
return File::searchFilter($filters)->with('trackType')->orderBy('artist_name')->paginate($pagination);
}
/**
* 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");
}
//Mime type list: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
$request->validate([
'file' => 'required|file|mimes:audio/mpeg,mpga,mp3,audio/x-wav,audio/ogg,audio/mp4',
]);
$file = $request->file('file');
$originalName = $file->getClientOriginalName();
$time = new LengthFormatter('00:00:00');
$dbFile = File::create([
'import_status' => 1,
'currentlyaccessing' => 0,
'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' => $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,
'hidden' => true,
'is_scheduled' => false,
'is_playlist' => false,
'filesize' => 0,
'track_type_id' => $request->track_type_id ? $request->track_type_id : null,
]);
$tmpFile = $file->move(Storage::disk('libretime-tmp')->path(''), $dbFile->id.'-'.$originalName);
RabbitMQSender::SendMessageToAnalyzer(
$tmpFile->getPathname(),
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) {
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) {
$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) {
$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');
}
}