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

94 lines
2.8 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
2016-03-05 09:01:12 +00:00
use App\Http\Requests\API\SongUpdateRequest;
2015-12-30 04:14:47 +00:00
use App\Http\Streamers\PHPStreamer;
2016-06-13 09:04:42 +00:00
use App\Http\Streamers\S3Streamer;
2016-01-31 14:00:15 +00:00
use App\Http\Streamers\TranscodingStreamer;
2015-12-30 04:14:47 +00:00
use App\Http\Streamers\XAccelRedirectStreamer;
use App\Http\Streamers\XSendFileStreamer;
2015-12-13 04:42:28 +00:00
use App\Models\Song;
2016-07-30 15:32:17 +00:00
use YouTube;
2015-12-13 04:42:28 +00:00
class SongController extends Controller
{
2015-12-30 04:14:47 +00:00
/**
2016-08-10 01:23:52 +00:00
* Play/stream a song.
2015-12-30 04:14:47 +00:00
*
* @link https://github.com/phanan/koel/wiki#streaming-music
*
2016-08-10 01:23:52 +00:00
* @param Song $song The song to stream.
* @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
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2015-12-30 04:14:47 +00:00
*/
2016-08-10 01:23:52 +00:00
public function play(Song $song, $transcode = null, $bitRate = null)
2015-12-30 04:14:47 +00:00
{
2016-06-13 09:04:42 +00:00
if ($song->s3_params) {
return (new S3Streamer($song))->stream();
}
2016-08-10 01:23:52 +00:00
// If `transcode` parameter isn't passed, the default is to only transcode FLAC.
if ($transcode === null && ends_with(mime_content_type($song->path), 'flac')) {
$transcode = true;
}
2016-08-10 01:23:52 +00:00
$streamer = null;
if ($transcode) {
2016-08-10 01:23:52 +00:00
$streamer = new TranscodingStreamer(
$song,
2016-08-21 15:19:03 +00:00
$bitRate ?: config('koel.streaming.bitrate'),
2016-08-10 01:23:52 +00:00
request()->input('time', 0)
);
} else {
2016-09-10 07:39:57 +00:00
switch (config('koel.streaming.method')) {
2016-08-10 01:23:52 +00:00
case 'x-sendfile':
$streamer = new XSendFileStreamer($song);
break;
case 'x-accel-redirect':
$streamer = new XAccelRedirectStreamer($song);
break;
default:
$streamer = new PHPStreamer($song);
break;
}
2016-01-28 15:19:06 +00:00
}
2016-08-10 01:23:52 +00:00
$streamer->stream();
2015-12-30 04:14:47 +00:00
}
2015-12-13 04:42:28 +00:00
/**
2015-12-20 12:17:35 +00:00
* Get extra information about a song via Last.fm.
2016-03-05 09:01:12 +00:00
*
2015-12-27 13:29:03 +00:00
* @param Song $song
*
* @return \Illuminate\Http\JsonResponse
*/
2016-05-30 05:50:59 +00:00
public function show(Song $song)
{
return response()->json([
'lyrics' => $song->lyrics,
'album_info' => $song->album->getInfo(),
2016-04-17 15:38:06 +00:00
'artist_info' => $song->artist->getInfo(),
2016-09-26 06:49:30 +00:00
'youtube' => $song->getRelatedYouTubeVideos(),
]);
}
2015-12-20 12:17:35 +00:00
2016-03-05 09:01:12 +00:00
/**
* Update songs info.
*
* @param SongUpdateRequest $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function update(SongUpdateRequest $request)
{
return response()->json(Song::updateInfo($request->songs, $request->data));
}
2015-12-13 04:42:28 +00:00
}