From df9f60339d6d1b6363cce1114903e39073dd887e Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Thu, 7 Apr 2016 02:09:34 +0200 Subject: [PATCH] WIP stream --- composer.json | 3 +- composer.lock | 175 +++++++++++++++++++++++++++++++- controllers/FrontController.php | 32 ++++-- index.php | 4 +- templates/video.tpl | 37 ++++--- 5 files changed, 216 insertions(+), 35 deletions(-) diff --git a/composer.json b/composer.json index 86c5e22..9401c8d 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "symfony/yaml": "~3.0.0", "symfony/process": "~3.0.0", "ffmpeg/ffmpeg": "~2.8.2", - "rudloff/smarty-plugin-noscheme": "~0.1.0" + "rudloff/smarty-plugin-noscheme": "~0.1.0", + "guzzlehttp/guzzle": "~6.2.0" }, "require-dev": { "symfony/var-dumper": "~3.0.0" diff --git a/composer.lock b/composer.lock index e589dc2..9c2c859 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": "829ac07cae5bc43ef0ad3f5e5be618c5", - "content-hash": "a7131399bb03f78ba0dcfd8ab7e98d59", + "hash": "6ef0d5dc0640824fef3033cba540c6fb", + "content-hash": "dd3d8be96114267b553f77632c326e2c", "packages": [ { "name": "container-interop/container-interop", @@ -48,6 +48,177 @@ ], "type": "library" }, + { + "name": "guzzlehttp/guzzle", + "version": "6.2.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "d094e337976dff9d8e2424e8485872194e768662" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/d094e337976dff9d8e2424e8485872194e768662", + "reference": "d094e337976dff9d8e2424e8485872194e768662", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "~1.0", + "guzzlehttp/psr7": "~1.1", + "php": ">=5.5.0" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "~4.0", + "psr/log": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.2-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2016-03-21 20:02:09" + }, + { + "name": "guzzlehttp/promises", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "bb9024c526b22f3fe6ae55a561fd70653d470aa8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/bb9024c526b22f3fe6ae55a561fd70653d470aa8", + "reference": "bb9024c526b22f3fe6ae55a561fd70653d470aa8", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "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": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-03-08 01:15:46" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.2.3", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "2e89629ff057ebb49492ba08e6995d3a6a80021b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/2e89629ff057ebb49492ba08e6995d3a6a80021b", + "reference": "2e89629ff057ebb49492ba08e6995d3a6a80021b", + "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-02-18 21:54:00" + }, { "name": "jeremykendall/php-domain-parser", "version": "3.0.0", diff --git a/controllers/FrontController.php b/controllers/FrontController.php index 06f8188..6e734cd 100644 --- a/controllers/FrontController.php +++ b/controllers/FrontController.php @@ -103,11 +103,12 @@ class FrontController public static function video($request, $response) { global $container; + $params = $request->getQueryParams(); $config = Config::getInstance(); - if (isset($_GET["url"])) { - if (isset($_GET['audio'])) { + if (isset($params["url"])) { + if (isset($params['audio'])) { try { - $video = VideoDownload::getJSON($_GET["url"]); + $video = VideoDownload::getJSON($params["url"]); //Vimeo needs a correct user-agent $UA = VideoDownload::getUA(); @@ -168,7 +169,7 @@ class FrontController } } else { try { - $video = VideoDownload::getJSON($_GET["url"]); + $video = VideoDownload::getJSON($params["url"]); $container->view->render( $response, 'head.tpl', @@ -219,12 +220,22 @@ class FrontController public static function redirect($request, $response) { global $app; - if (isset($_GET["url"])) { + $params = $request->getQueryParams(); + if (isset($params["url"])) { try { - $video = VideoDownload::getURL($_GET["url"]); - return $response->withRedirect($video['url']); + $format = isset($params["format"]) ? $params["format"] : 'best'; + $video = VideoDownload::getJSON($params["url"], $format); + $client = new \GuzzleHttp\Client(); + $stream = $client->request('GET', $video->url, array('stream'=>true)); + $response = $response->withHeader('Content-Disposition', 'inline; filename="'.$video->_filename.'"'); + $response = $response->withHeader('Content-Type', $stream->getHeader('Content-Type')); + $response = $response->withHeader('Content-Length', $stream->getHeader('Content-Length')); + if ($request->isGet()) { + $response = $response->withBody($stream->getBody()); + } + return $response; } catch (\Exception $e) { - echo $e->getMessage().PHP_EOL; + $response->getBody()->write($e->getMessage().PHP_EOL); return $response->withHeader('Content-Type', 'text/plain'); } } @@ -241,9 +252,10 @@ class FrontController public static function json($request, $response) { global $app; - if (isset($_GET["url"])) { + $params = $request->getQueryParams(); + if (isset($params["url"])) { try { - $video = VideoDownload::getJSON($_GET["url"]); + $video = VideoDownload::getJSON($params["url"]); return $response->withJson($video); } catch (\Exception $e) { return $response->withJson( diff --git a/index.php b/index.php index 468a685..c26d743 100644 --- a/index.php +++ b/index.php @@ -22,7 +22,7 @@ $container['view'] = function ($c) { $view = new \Slim\Views\Smarty(__DIR__.'/templates/'); $view->addSlimPlugins($c['router'], $c['request']->getUri()); - $view->registerPlugin('modifier', 'noscheme', 'Smarty_Modifier_noscheme'); + $view->registerPlugin('modifier', 'noscheme', 'Smarty_Modifier_noscheme'); return $view; @@ -43,7 +43,7 @@ $app->get( $app->get( '/redirect', array('Alltube\Controller\FrontController', 'redirect') -); +)->setName('redirect'); $app->get( '/json', array('Alltube\Controller\FrontController', 'json') diff --git a/templates/video.tpl b/templates/video.tpl index b6a1231..31c0b10 100644 --- a/templates/video.tpl +++ b/templates/video.tpl @@ -22,27 +22,24 @@ {/if}
- {if isset($video->formats)} -

Available formats:

-

(You might have to do a Right click > Save as)

-

+

+
+ +

+
+
{else}