mirror of
https://github.com/koel/koel
synced 2024-12-23 11:03:05 +00:00
42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Exceptions\MediaPathNotSetException;
|
|
use App\Exceptions\SongUploadFailedException;
|
|
use App\Http\Requests\API\UploadRequest;
|
|
use App\Http\Resources\AlbumResource;
|
|
use App\Http\Resources\SongResource;
|
|
use App\Models\User;
|
|
use App\Repositories\AlbumRepository;
|
|
use App\Repositories\SongRepository;
|
|
use App\Services\UploadService;
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
use Illuminate\Http\Response;
|
|
|
|
class UploadController extends Controller
|
|
{
|
|
/** @param User $user */
|
|
public function __invoke(
|
|
UploadService $uploadService,
|
|
AlbumRepository $albumRepository,
|
|
SongRepository $songRepository,
|
|
UploadRequest $request,
|
|
Authenticatable $user
|
|
) {
|
|
$this->authorize('admin', User::class);
|
|
|
|
try {
|
|
$song = $songRepository->getOne($uploadService->handleUploadedFile($request->file)->id);
|
|
|
|
return response()->json([
|
|
'song' => SongResource::make($song),
|
|
'album' => AlbumResource::make($albumRepository->getOne($song->album_id)),
|
|
]);
|
|
} catch (MediaPathNotSetException $e) {
|
|
abort(Response::HTTP_FORBIDDEN, $e->getMessage());
|
|
} catch (SongUploadFailedException $e) {
|
|
abort(Response::HTTP_BAD_REQUEST, $e->getMessage());
|
|
}
|
|
}
|
|
}
|