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

45 lines
1.5 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;
use App\Services\SongStorages\SongStorage;
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(
SongStorage $storage,
2022-06-10 10:47:46 +00:00
AlbumRepository $albumRepository,
SongRepository $songRepository,
UploadRequest $request,
Authenticatable $user
) {
2024-01-03 17:02:18 +00:00
$this->authorize('upload', User::class);
2020-06-07 20:43:04 +00:00
try {
// @todo decouple Song from storage, as storages should not be responsible for creating a song.
$song = $songRepository->getOne($storage->storeUploadedFile($request->file, $user)->id, $user);
2022-06-10 10:47:46 +00:00
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
}
}
}