2024-01-03 17:02:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Download;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests\Download\DownloadSongsRequest;
|
2024-01-10 23:11:45 +00:00
|
|
|
use App\Models\Song;
|
2024-01-03 17:02:18 +00:00
|
|
|
use App\Repositories\SongRepository;
|
|
|
|
use App\Services\DownloadService;
|
2024-02-05 21:17:41 +00:00
|
|
|
use Illuminate\Http\Response;
|
2024-01-03 17:02:18 +00:00
|
|
|
|
|
|
|
class DownloadSongsController extends Controller
|
|
|
|
{
|
2024-01-10 23:11:45 +00:00
|
|
|
public function __invoke(DownloadSongsRequest $request, DownloadService $service, SongRepository $repository)
|
2024-01-03 17:02:18 +00:00
|
|
|
{
|
2024-01-10 23:11:45 +00:00
|
|
|
// Don't use SongRepository::findMany() because it'd have been already catered to the current user.
|
|
|
|
$songs = Song::query()->findMany($request->songs);
|
2024-01-07 12:43:10 +00:00
|
|
|
$songs->each(fn ($song) => $this->authorize('download', $song));
|
|
|
|
|
2024-02-05 21:17:41 +00:00
|
|
|
$downloadablePath = $service->getDownloadablePath($repository->getMany($request->songs));
|
|
|
|
|
|
|
|
abort_unless((bool) $downloadablePath, Response::HTTP_BAD_REQUEST, 'Song cannot be downloaded.');
|
|
|
|
|
|
|
|
return response()->download($downloadablePath);
|
2024-01-03 17:02:18 +00:00
|
|
|
}
|
|
|
|
}
|