koel/app/Http/Controllers/Download/DownloadSongsController.php

32 lines
1.2 KiB
PHP
Raw Normal View History

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;
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-05-19 05:49:42 +00:00
// 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));
2024-05-19 05:49:42 +00:00
abort_unless((bool) $downloadablePath, Response::HTTP_BAD_REQUEST, 'Song or episode cannot be downloaded.');
return response()->download($downloadablePath);
2024-01-03 17:02:18 +00:00
}
}