From d00c1dc91c7ccc014c39ef2a087cfea13e0e4d87 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Thu, 14 Apr 2016 12:42:13 +0200 Subject: [PATCH] Use PSR-7 streams for conversion --- composer.json | 3 +- composer.lock | 95 ++++++++++++++++++++++++++++++++- controllers/FrontController.php | 47 ++++++---------- 3 files changed, 112 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index 0b578a3..18a6a5e 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "ptachoire/process-builder-chain": "~1.2.0", "ffmpeg/ffmpeg": "dev-release", "rudloff/smarty-plugin-noscheme": "~0.1.0", - "rudloff/rtmpdump-bin": "~2.3" + "rudloff/rtmpdump-bin": "~2.3", + "rudloff/psr7-process-stream": "~0.1.0" }, "require-dev": { "symfony/var-dumper": "~3.0.0" diff --git a/composer.lock b/composer.lock index 5022148..d6a574e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "a0c23015c2a2bbb700ae566a04ca8679", - "content-hash": "42d3272dbfe226c0ef3da4e135e2dae3", + "hash": "0fef6027910cac993af5690ffd131b1c", + "content-hash": "ae728e70b08dd126d55576800f334529", "packages": [ { "name": "container-interop/container-interop", @@ -48,6 +48,64 @@ ], "type": "library" }, + { + "name": "guzzlehttp/psr7", + "version": "1.3.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "31382fef2889136415751badebbd1cb022a4ed72" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/31382fef2889136415751badebbd1cb022a4ed72", + "reference": "31382fef2889136415751badebbd1cb022a4ed72", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "PSR-7 message implementation", + "keywords": [ + "http", + "message", + "stream", + "uri" + ], + "time": "2016-04-13 19:56:01" + }, { "name": "jeremykendall/php-domain-parser", "version": "3.0.0", @@ -408,6 +466,39 @@ }, "type": "library" }, + { + "name": "rudloff/psr7-process-stream", + "version": "0.1.0", + "source": { + "type": "git", + "url": "https://github.com/Rudloff/psr7-process-stream.git", + "reference": "055ecbdadee9b43904aed75d5a548c277cac10a8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Rudloff/psr7-process-stream/zipball/055ecbdadee9b43904aed75d5a548c277cac10a8", + "reference": "055ecbdadee9b43904aed75d5a548c277cac10a8", + "shasum": "" + }, + "require": { + "guzzlehttp/psr7": "~1.3.0" + }, + "require-dev": { + "symfony/var-dumper": "~3.0.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "ProcessStream\\": "classes/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-3.0" + ], + "description": "PSR-7 stream implementation that can be used to manage processes", + "time": "2016-04-14 10:36:10" + }, { "name": "rudloff/rtmpdump-bin", "version": "2.3", diff --git a/controllers/FrontController.php b/controllers/FrontController.php index 03f2f01..ab3b5aa 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -16,6 +16,7 @@ use Alltube\VideoDownload; use Alltube\Config; use Symfony\Component\Process\ProcessBuilder; use Chain\Chain; +use ProcessStream\PopenStream; /** * Main controller @@ -136,6 +137,21 @@ class FrontController 'user_agent', $video->http_headers->{'User-Agent'} ); + + $response = $response->withHeader( + 'Content-Disposition', + 'attachment; filename="'. + html_entity_decode( + pathinfo( + $video->_filename, + PATHINFO_FILENAME + ).'.mp3', + ENT_COMPAT, + 'ISO-8859-1' + ).'"' + ); + $response = $response->withHeader('Content-Type', 'audio/mpeg'); + if (parse_url($video->url, PHP_URL_SCHEME) == 'rtmp') { $builder = new ProcessBuilder( array( @@ -164,21 +180,6 @@ class FrontController } $chain = new Chain($builder->getProcess()); $chain->add('|', $avconvProc); - ob_end_flush(); - header( - 'Content-Disposition: attachment; filename="'. - html_entity_decode( - pathinfo( - $video->_filename, - PATHINFO_FILENAME - ).'.mp3', - ENT_COMPAT, - 'ISO-8859-1' - ).'"' - ); - header("Content-Type: audio/mpeg"); - passthru($chain->getProcess()->getCommandLine()); - exit; } else { $chain = new Chain( ProcessBuilder::create( @@ -193,22 +194,8 @@ class FrontController ) ); $chain->add('|', $avconvProc); - ob_end_flush(); - header( - 'Content-Disposition: attachment; filename="'. - html_entity_decode( - pathinfo( - $video->_filename, - PATHINFO_FILENAME - ).'.mp3', - ENT_COMPAT, - 'ISO-8859-1' - ).'"' - ); - header("Content-Type: audio/mpeg"); - passthru($chain->getProcess()->getCommandLine()); - exit; } + return $response->withBody(new PopenStream($chain->getProcess()->getCommandLine())); } } catch (\Exception $e) { $error = $e->getMessage();