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

85 lines
2.3 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
/**
* Play a song.
*
* @link https://github.com/phanan/koel/wiki#streaming-music
*
* @param Song $song
2016-08-03 10:42:39 +00:00
* @param bool $transcode
2016-08-03 10:42:11 +00:00
* @param null $bitrate
2015-12-30 04:14:47 +00:00
*/
2016-08-03 10:42:11 +00:00
public function play(Song $song, $transcode = false, $bitrate = null)
2015-12-30 04:14:47 +00:00
{
2016-08-03 10:42:11 +00:00
if (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();
}
// If transcode parameter isn't passed, the default is to only transcode flac
2016-08-10 01:08:52 +00:00
if (false === $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
*
* @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-07-30 15:32:17 +00:00
'youtube' => YouTube::searchVideosRelatedToSong($song),
]);
}
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
}