mirror of
https://github.com/koel/koel
synced 2024-12-22 02:23:14 +00:00
31 lines
1.2 KiB
PHP
31 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Download;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Download\DownloadSongsRequest;
|
|
use App\Models\Song;
|
|
use App\Repositories\SongRepository;
|
|
use App\Services\DownloadService;
|
|
use Illuminate\Http\Response;
|
|
|
|
class DownloadSongsController extends Controller
|
|
{
|
|
public function __invoke(DownloadSongsRequest $request, DownloadService $service, SongRepository $repository)
|
|
{
|
|
// Don't use SongRepository::findMany() because it'd have been already catered to the current user.
|
|
$songs = Song::query()->findMany($request->songs);
|
|
$songs->each(fn ($song) => $this->authorize('download', $song));
|
|
|
|
// For a single episode, we'll just redirect to its original media to save time and bandwidth
|
|
if ($songs->count() === 1 && $songs[0]->isEpisode()) {
|
|
return response()->redirectTo($songs[0]->path);
|
|
}
|
|
|
|
$downloadablePath = $service->getDownloadablePath($repository->getMany($request->songs));
|
|
|
|
abort_unless((bool) $downloadablePath, Response::HTTP_BAD_REQUEST, 'Song or episode cannot be downloaded.');
|
|
|
|
return response()->download($downloadablePath);
|
|
}
|
|
}
|