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

78 lines
2 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-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
*/
public function play(Song $song)
{
2016-01-28 15:19:06 +00:00
if (ends_with(mime_content_type($song->path), 'flac')) {
return (new TranscodingStreamer($song))->stream();
}
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
*/
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.
2016-03-05 09:01:12 +00:00
*
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.
2016-03-05 09:01:12 +00:00
*
2015-12-20 12:17:35 +00:00
* @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
}
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
}