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

85 lines
2.9 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
2018-08-22 17:59:14 +00:00
use App\Factories\StreamerFactory;
use App\Http\Requests\API\SongPlayRequest;
2016-03-05 09:01:12 +00:00
use App\Http\Requests\API\SongUpdateRequest;
2017-04-24 06:38:41 +00:00
use App\Models\Song;
2018-08-29 06:30:39 +00:00
use App\Repositories\AlbumRepository;
use App\Repositories\ArtistRepository;
2018-08-18 13:19:40 +00:00
use App\Services\MediaInformationService;
2017-06-04 01:30:45 +00:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
2015-12-13 04:42:28 +00:00
/**
* @group 3. Song interactions
*/
2015-12-13 04:42:28 +00:00
class SongController extends Controller
{
2018-08-18 13:19:40 +00:00
private $mediaInformationService;
2018-08-22 17:59:14 +00:00
private $streamerFactory;
2018-08-29 06:30:39 +00:00
private $artistRepository;
private $albumRepository;
2018-08-18 13:19:40 +00:00
2018-08-29 06:30:39 +00:00
public function __construct(
MediaInformationService $mediaInformationService,
StreamerFactory $streamerFactory,
ArtistRepository $artistRepository,
AlbumRepository $albumRepository
2018-08-29 07:07:44 +00:00
) {
2018-08-18 13:19:40 +00:00
$this->mediaInformationService = $mediaInformationService;
2018-08-22 17:59:14 +00:00
$this->streamerFactory = $streamerFactory;
2018-08-29 06:30:39 +00:00
$this->artistRepository = $artistRepository;
$this->albumRepository = $albumRepository;
2018-08-18 13:19:40 +00:00
}
2015-12-30 04:14:47 +00:00
/**
2020-04-12 08:08:37 +00:00
* Play a song.
*
* The GET request to play/stream a song. By default Koel will serve the file as-is, unless it's a FLAC.
* If the value of `transcode` is truthy, Koel will attempt to transcode the file into `bitRate`kbps using ffmpeg.
*
* @response {}
*
* @queryParam jwt-token required The JWT token.
2015-12-30 04:14:47 +00:00
*
* @link https://github.com/phanan/koel/wiki#streaming-music
*
2018-08-29 07:07:44 +00:00
* @param null|bool $transcode Whether to force transcoding the song.
* If this is omitted, by default Koel will transcode FLAC.
* @param null|int $bitRate The target bit rate to transcode, defaults to OUTPUT_BIT_RATE.
* Only taken into account if $transcode is truthy.
2016-08-16 15:12:11 +00:00
*
2017-06-04 01:30:45 +00:00
* @return RedirectResponse|Redirector
2015-12-30 04:14:47 +00:00
*/
2018-08-24 15:27:19 +00:00
public function play(SongPlayRequest $request, Song $song, ?bool $transcode = null, ?int $bitRate = null)
2015-12-30 04:14:47 +00:00
{
2018-08-22 17:59:14 +00:00
return $this->streamerFactory
->createStreamer($song, $transcode, $bitRate, floatval($request->time))
->stream();
2015-12-30 04:14:47 +00:00
}
2016-03-05 09:01:12 +00:00
/**
2020-04-12 08:08:37 +00:00
* Update song information.
*
* @bodyParam songs array required An array of song IDs to be updated.
* @bodyParam data object required The new data, with these supported fields: `title`, `artistName`, `albumName`, and `lyrics`.
*
* @group 5. Media information
2016-03-05 09:01:12 +00:00
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2016-03-05 09:01:12 +00:00
*/
public function update(SongUpdateRequest $request)
{
2018-08-29 06:30:39 +00:00
$updatedSongs = Song::updateInfo($request->songs, $request->data);
return response()->json([
'artists' => $this->artistRepository->getByIds($updatedSongs->pluck('artist_id')->all()),
'albums' => $this->albumRepository->getByIds($updatedSongs->pluck('album_id')->all()),
'songs' => $updatedSongs,
]);
2016-03-05 09:01:12 +00:00
}
2015-12-13 04:42:28 +00:00
}