diff --git a/app/config/routing.yml b/app/config/routing.yml index 0421f47..2da2256 100644 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -6,6 +6,14 @@ view: path: /view/{gist}/{commit} defaults: {_controller: Gist\Controller\ViewController::viewAction, _locale: en, commit: 0} +raw: + path: /raw/{gist}/{commit} + defaults: {_controller: Gist\Controller\ViewController::rawAction, _locale: en, commit: 0} + +download: + path: /download/{gist}/{commit} + defaults: {_controller: Gist\Controller\ViewController::downloadAction, _locale: en, commit: 0} + revisions: path: /revs/{gist} defaults: {_controller: Gist\Controller\ViewController::revisionsAction, _locale: en} diff --git a/app/locales/en.yml b/app/locales/en.yml index aa687a9..0608499 100644 --- a/app/locales/en.yml +++ b/app/locales/en.yml @@ -13,6 +13,12 @@ gist: 404: title: 'Oops!' message: 'The gist was not found...' + action: + view: 'View' + history: 'Commit(s)' + raw: 'RAW' + download: 'Download' + fork: 'Fork' date: format: 'Y-m-d h:i:s' diff --git a/app/locales/fr.yml b/app/locales/fr.yml index 7fce02a..e8d67de 100644 --- a/app/locales/fr.yml +++ b/app/locales/fr.yml @@ -13,6 +13,12 @@ gist: 404: title: 'Arf...' message: "Ce gist n'existe pas." + action: + view: 'Afficher' + history: 'Révision(s)' + raw: 'RAW' + download: 'Télécharger' + fork: 'Clôner' date: format: 'd/m/Y H\hi s\s' diff --git a/src/Gist/Controller/ViewController.php b/src/Gist/Controller/ViewController.php index 0f7f01e..a4aded3 100644 --- a/src/Gist/Controller/ViewController.php +++ b/src/Gist/Controller/ViewController.php @@ -6,6 +6,7 @@ use Silex\Application; use Symfony\Component\HttpFoundation\Request; use Gist\Model\GistQuery; use Gist\Model\Gist; +use Symfony\Component\HttpFoundation\Response; /** * Class HomeController @@ -13,34 +14,82 @@ use Gist\Model\Gist; */ class ViewController { - public function viewAction(Request $request, Application $app, $gist, $commit) + protected function getViewOptions(Request $request, Application $app, $gist, $commit) { $gist = GistQuery::create()->findOneByFile($gist); if (null === $gist) { - return $this->notFoundResponse($app); + return null; } $history = $app['gist']->getHistory($gist); if (empty($history)) { - return $this->notFoundResponse($app); + return null; } $content = $this->getContentByCommit($app, $gist, $commit, $history); - return $app['twig']->render( - 'View/view.html.twig', - array( - 'gist' => $gist, - 'type' => $gist->getType(), - 'history' => $history, - 'commit' => $commit, - 'content' => $app['gist']->highlight($gist->getType(), $content), - ) + return array( + 'gist' => $gist, + 'type' => $gist->getType(), + 'history' => $history, + 'commit' => $commit, + 'raw_content' => $content, + 'content' => $app['gist']->highlight($gist->getType(), $content), ); } + public function viewAction(Request $request, Application $app, $gist, $commit) + { + $viewOptions = $this->getViewOptions($request, $app, $gist, $commit); + + if (is_array($viewOptions)) { + return $app['twig']->render('View/view.html.twig', $viewOptions); + } else { + return $this->notFoundResponse($app); + } + } + + public function rawAction(Request $request, Application $app, $gist, $commit) + { + $viewOptions = $this->getViewOptions($request, $app, $gist, $commit); + + if (is_array($viewOptions)) { + return new Response( + $viewOptions['raw_content'], + 200, + array( + 'Content-Type' => 'text/plain', + ) + ); + } else { + return $this->notFoundResponse($app); + } + } + + public function downloadAction(Request $request, Application $app, $gist, $commit) + { + $viewOptions = $this->getViewOptions($request, $app, $gist, $commit); + + if (is_array($viewOptions)) { + $gist = $viewOptions['gist']; + $file = $app['gist_path'].'/'.$gist->getFile(); + + return new Response( + $viewOptions['raw_content'], + 200, + array( + 'Content-Disposition' => sprintf('filename=%s.%s', $gist->getFile(), $gist->getTypeAsExtension()), + 'Content-Length' => filesize($file), + 'Content-Type' => 'application/force-download', + ) + ); + } else { + return $this->notFoundResponse($app); + } + } + public function revisionsAction(Request $request, Application $app, $gist) { $gist = GistQuery::create()->findOneByFile($gist); diff --git a/src/Gist/Model/Gist.php b/src/Gist/Model/Gist.php index fa73369..4870d2e 100644 --- a/src/Gist/Model/Gist.php +++ b/src/Gist/Model/Gist.php @@ -25,4 +25,19 @@ class Gist extends BaseGist { $this->setFile(uniqid()); } + + public function getTypeAsExtension() + { + $data = array( + 'javascript' => 'js', + 'yaml'=> 'yml', + 'perl' => 'pl', + 'python' => 'py', + 'bash' => 'sh', + 'actionscript3' => 'as', + 'text' => 'txt', + ); + + return str_replace(array_keys($data), array_values($data), $this->getType()); + } } diff --git a/src/Gist/Resources/views/View/revisions.html.twig b/src/Gist/Resources/views/View/revisions.html.twig index a9b61b8..c89ada5 100644 --- a/src/Gist/Resources/views/View/revisions.html.twig +++ b/src/Gist/Resources/views/View/revisions.html.twig @@ -12,11 +12,13 @@ {% if not gist.cipher %}