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

60 lines
1.5 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
use App\Http\Streamers\PHPStreamer;
2015-12-16 15:42:11 +00:00
use App\Http\Streamers\XAccelRedirectStreamer;
2015-12-16 16:15:20 +00:00
use App\Http\Streamers\XSendFileStreamer;
2015-12-13 04:42:28 +00:00
use App\Models\Song;
class SongController extends Controller
{
/**
* Play a song.
2015-12-16 17:25:25 +00:00
*
2015-12-16 17:24:33 +00:00
* @link https://github.com/phanan/koel/wiki#streaming-music
2015-12-13 04:42:28 +00:00
*
2015-12-27 13:29:03 +00:00
* @param Song $song
2015-12-13 04:42:28 +00:00
*/
2015-12-27 13:29:03 +00:00
public function play(Song $song)
2015-12-13 04:42:28 +00:00
{
2015-12-16 15:42:11 +00:00
switch (env('STREAMING_METHOD')) {
case 'x-sendfile':
2015-12-27 13:29:03 +00:00
return (new XSendFileStreamer($song))->stream();
2015-12-16 15:42:11 +00:00
case 'x-accel-redirect':
2015-12-27 13:29:03 +00:00
return (new XAccelRedirectStreamer($song))->stream();
2015-12-16 15:42:11 +00:00
default:
2015-12-27 13:29:03 +00:00
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.
*
2015-12-27 13:29:03 +00:00
* @param Song $song
*
* @return \Illuminate\Http\JsonResponse
*/
2015-12-27 13:29:03 +00:00
public function getInfo(Song $song)
{
return response()->json([
'lyrics' => $song->lyrics,
'album_info' => $song->album->getInfo(),
'artist_info' => $song->album->artist->getInfo(),
]);
}
2015-12-20 12:17:35 +00:00
/**
* Scrobble a song.
*
2015-12-27 14:06:10 +00:00
* @param Song $song
2015-12-20 12:17:35 +00:00
* @param string $timestamp The UNIX timestamp when the song started playing.
*
* @return \Illuminate\Http\JsonResponse
*/
2015-12-27 13:29:03 +00:00
public function scrobble(Song $song, $timestamp)
2015-12-20 12:17:35 +00:00
{
2015-12-27 13:29:03 +00:00
return response()->json($song->scrobble($timestamp));
2015-12-20 12:17:35 +00:00
}
2015-12-13 04:42:28 +00:00
}