184 lines
5.7 KiB
PHP
184 lines
5.7 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 App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
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(Request $request) {
|
|
|
|
if (!isset($request->per_page) || is_null($request)) {
|
|
$pagination = 5;
|
|
} else {
|
|
$pagination = $request->per_page;
|
|
}
|
|
|
|
$files = File::searchFilter($request)
|
|
->where('import_status', '=', 0)
|
|
->with('track_type')
|
|
->with('owner');
|
|
|
|
if($request->track_type == 'spot'){
|
|
$trackTypes = TrackType::where('code', '=', 'SPOT')->orWhere('parent_id', '=', '3')->pluck('id');
|
|
$files = $files->whereIn('track_type_id', $trackTypes->toArray());
|
|
}
|
|
|
|
return $files->orderBy('artist_name')
|
|
->paginate($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();
|
|
|
|
$apiKey = $request->header('php-auth-user');
|
|
|
|
//Accept request only from logged-in users
|
|
if (!$user) {
|
|
if ($apiKey != 'some_secret_api_key') {
|
|
throw new \Exception("You must be logged in");
|
|
}
|
|
//ToDo: check how to work in Legacy, getting user in this way is quite horrible
|
|
try {
|
|
$user = User::where('type','=','P')->orderBy('id','ASC')->first();
|
|
} catch (\Exception $e) {
|
|
Log::error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
//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
|
|
);
|
|
|
|
return $dbFile->toJson();
|
|
}
|
|
|
|
/**
|
|
* Retrieve a single file metadata
|
|
* @param $id
|
|
* @return mixed
|
|
*/
|
|
public function show($id) {
|
|
return File::whereId($id)->first()->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();
|
|
$fields = $request->all();
|
|
|
|
if (isset($fields['name']) && is_null($fields['name'])) {
|
|
$fields['name'] = "";
|
|
}
|
|
|
|
//Only from Analyzer
|
|
if (isset($fields['full_path'])) {
|
|
$fields['filepath'] = $fields['full_path'];
|
|
}
|
|
|
|
//Check Import status
|
|
if ($fields['import_status'] == 'PipelineStatus.SUCCEED') {
|
|
$fields['import_status'] = 0;
|
|
}
|
|
|
|
//Check if there is a track type id, if not add "MUSIC" id
|
|
if (is_null($file->track_type_id)) {
|
|
$fields['track_type_id'] = TrackType::where('code','MUSICA')->first()->id;
|
|
}
|
|
|
|
//Force BPM to int
|
|
if (isset($fields['bpm'])) {
|
|
$fields['bpm'] = intval($fields['bpm']);
|
|
}
|
|
|
|
//return json_encode(['req' => $fields,'id' => $id]);
|
|
|
|
$file->fill($fields)->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;
|
|
}
|
|
|
|
public function checkUploadStatus($id) {
|
|
return File::whereId($id)->first()->only('import_status');
|
|
}
|
|
|
|
//Test Method, must be deleted before finishing
|
|
public function test() {
|
|
return view('upload');
|
|
}
|
|
}
|