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

44 lines
1.4 KiB
PHP
Raw Normal View History

2020-06-07 20:43:04 +00:00
<?php
namespace App\Http\Controllers\API;
use App\Exceptions\MediaPathNotSetException;
use App\Exceptions\SongUploadFailedException;
2022-07-29 06:47:10 +00:00
use App\Http\Controllers\Controller;
2020-06-07 20:43:04 +00:00
use App\Http\Requests\API\UploadRequest;
2022-06-10 10:47:46 +00:00
use App\Http\Resources\AlbumResource;
use App\Http\Resources\SongResource;
use App\Models\User;
use App\Repositories\AlbumRepository;
use App\Repositories\SongRepository;
2020-06-07 20:43:04 +00:00
use App\Services\UploadService;
2022-06-10 10:47:46 +00:00
use Illuminate\Contracts\Auth\Authenticatable;
2020-10-26 15:29:29 +00:00
use Illuminate\Http\Response;
2020-06-07 20:43:04 +00:00
class UploadController extends Controller
{
2022-06-10 10:47:46 +00:00
/** @param User $user */
public function __invoke(
UploadService $uploadService,
AlbumRepository $albumRepository,
SongRepository $songRepository,
UploadRequest $request,
Authenticatable $user
) {
$this->authorize('admin', User::class);
2020-06-07 20:43:04 +00:00
try {
2022-06-10 10:47:46 +00:00
$song = $songRepository->getOne($uploadService->handleUploadedFile($request->file)->id);
return response()->json([
'song' => SongResource::make($song),
'album' => AlbumResource::make($albumRepository->getOne($song->album_id)),
]);
2020-06-07 20:43:04 +00:00
} 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
}
}
}