koel/app/Http/Controllers/API/UploadController.php

37 lines
1 KiB
PHP
Raw Normal View History

2020-06-07 20:43:04 +00:00
<?php
namespace App\Http\Controllers\API;
use App\Events\MediaCacheObsolete;
use App\Exceptions\MediaPathNotSetException;
use App\Exceptions\SongUploadFailedException;
use App\Http\Requests\API\UploadRequest;
use App\Services\UploadService;
use Illuminate\Http\JsonResponse;
2020-10-26 15:29:29 +00:00
use Illuminate\Http\Response;
2020-06-07 20:43:04 +00:00
class UploadController extends Controller
{
2021-06-05 10:47:56 +00:00
private UploadService $uploadService;
2020-06-07 20:43:04 +00:00
public function __construct(UploadService $uploadService)
{
$this->uploadService = $uploadService;
}
public function store(UploadRequest $request): JsonResponse
{
try {
$song = $this->uploadService->handleUploadedFile($request->file);
} catch (MediaPathNotSetException $e) {
2020-10-26 15:29:29 +00:00
abort(Response::HTTP_FORBIDDEN, $e->getMessage());
2020-06-07 20:43:04 +00:00
} catch (SongUploadFailedException $e) {
2020-10-26 15:29:29 +00:00
abort(Response::HTTP_BAD_REQUEST, $e->getMessage());
2020-06-07 20:43:04 +00:00
}
event(new MediaCacheObsolete());
return response()->json($song->load('album', 'artist'));
2020-06-07 20:43:04 +00:00
}
}