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;
|
|
|
|
|
|
|
|
class SongController extends Controller
|
|
|
|
{
|
2015-12-30 04:14:47 +00:00
|
|
|
/**
|
|
|
|
* Play a song.
|
|
|
|
*
|
|
|
|
* @link https://github.com/phanan/koel/wiki#streaming-music
|
|
|
|
*
|
|
|
|
* @param Song $song
|
|
|
|
*/
|
2016-05-21 15:38:16 +00:00
|
|
|
public function play(Song $song, $transcode = null, $bitrate = null)
|
2015-12-30 04:14:47 +00:00
|
|
|
{
|
2016-05-21 15:38:16 +00:00
|
|
|
if (is_null($bitrate)) {
|
|
|
|
$bitrate = env('OUTPUT_BIT_RATE', 128);
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:04:42 +00:00
|
|
|
if ($song->s3_params) {
|
|
|
|
return (new S3Streamer($song))->stream();
|
|
|
|
}
|
|
|
|
|
2016-05-21 15:38:16 +00:00
|
|
|
// If transcode parameter isn't passed, the default is to only transcode flac
|
|
|
|
if (is_null($transcode) && ends_with(mime_content_type($song->path), 'flac')) {
|
|
|
|
$transcode = true;
|
|
|
|
} else {
|
|
|
|
$transcode = (bool) $transcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($transcode) {
|
|
|
|
return (new TranscodingStreamer($song, $bitrate, request()->input('time', 0)))->stream();
|
2016-01-28 15:19:06 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 04:14:47 +00:00
|
|
|
switch (env('STREAMING_METHOD')) {
|
|
|
|
case 'x-sendfile':
|
|
|
|
return (new XSendFileStreamer($song))->stream();
|
|
|
|
case 'x-accel-redirect':
|
|
|
|
return (new XAccelRedirectStreamer($song))->stream();
|
|
|
|
default:
|
|
|
|
return (new PHPStreamer($song))->stream();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2015-12-19 16:36:44 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2016-05-30 05:50:59 +00:00
|
|
|
public function show(Song $song)
|
2015-12-19 16:36:44 +00:00
|
|
|
{
|
|
|
|
return response()->json([
|
|
|
|
'lyrics' => $song->lyrics,
|
|
|
|
'album_info' => $song->album->getInfo(),
|
2016-04-17 15:38:06 +00:00
|
|
|
'artist_info' => $song->artist->getInfo(),
|
2015-12-19 16:36:44 +00:00
|
|
|
]);
|
|
|
|
}
|
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
|
|
|
}
|