Add X-Accel-Redirect stream support

This commit is contained in:
An Phan 2015-12-16 23:42:11 +08:00
parent ff78b4a9f1
commit 9a806632cb
4 changed files with 32 additions and 35 deletions

View file

@ -31,11 +31,3 @@
# Disable deflation for media files.
SetEnvIfNoCase Request_URI "^/api/play/" no-gzip dont-vary
</IfModule>
<IfModule mod_xsendfile.c>
# Set a MOD_X_SENDFILE_ENABLED env variable for PHP to use later.
<Files *.php>
XSendFile On
SetEnv MOD_X_SENDFILE_ENABLED 1
</Files>
</IfModule>

View file

@ -4,6 +4,7 @@ namespace App\Http\Controllers\API;
use App\Http\Streamers\PHPStreamer;
use App\Http\Streamers\XSendFileStreamer;
use App\Http\Streamers\XAccelRedirectStreamer;
use App\Models\Song;
class SongController extends Controller
@ -16,16 +17,14 @@ class SongController extends Controller
*/
public function play($id)
{
if (env('MOD_X_SENDFILE_ENABLED')) {
(new XSendFileStreamer($id))->stream();
return;
switch (env('STREAMING_METHOD')) {
case 'x-sendfile':
return (new XSendFileStreamer($id))->stream();
case 'x-accel-redirect':
return (new XAccelRedirectStreamer($id))->stream();
default:
return (new PHPStreamer($id))->stream();
}
(new PHPStreamer($id))->stream();
// Exit here to avoid accidentally sending extra content at the end of the file.
exit;
}
/**

View file

@ -16,7 +16,13 @@ class XAccelRedirectStreamer extends BaseStreamer implements StreamerInterface
*/
public function stream()
{
header('X-Accel-Redirect: '.str_replace(Setting::get('media_path'), '/media/', $this->song->path));
$relativePath = str_replace(Setting::get('media_path'), '', $this->song->path);
// We send our media_path value as a 'X-Media-Root' header to downstream (nginx)
// It will then be use as `alias` in X-Accel config location block.
// See nginx.conf.example.
header('X-Media-Root: '.Setting::get('media_path'));
header('X-Accel-Redirect: /media/'.$relativePath);
header("Content-Type: {$this->contentType}");
header('Content-Disposition: inline; filename="'.basename($this->song->path).'"');

View file

@ -1,7 +1,7 @@
server {
listen *:8080;
server_name koel.dev;
root /Users/an/www/koel;
root /var/www/koel;
index index.php;
# Deny access to dotfiles