Raw, download and trans

This commit is contained in:
Simon Vieille 2015-05-07 13:38:24 +02:00
parent 461c2d390c
commit 5667ec6066
10 changed files with 129 additions and 52 deletions

View file

@ -6,6 +6,14 @@ view:
path: /view/{gist}/{commit} path: /view/{gist}/{commit}
defaults: {_controller: Gist\Controller\ViewController::viewAction, _locale: en, commit: 0} 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: revisions:
path: /revs/{gist} path: /revs/{gist}
defaults: {_controller: Gist\Controller\ViewController::revisionsAction, _locale: en} defaults: {_controller: Gist\Controller\ViewController::revisionsAction, _locale: en}

View file

@ -13,6 +13,12 @@ gist:
404: 404:
title: 'Oops!' title: 'Oops!'
message: 'The gist was not found...' message: 'The gist was not found...'
action:
view: 'View'
history: 'Commit(s)'
raw: 'RAW'
download: 'Download'
fork: 'Fork'
date: date:
format: 'Y-m-d h:i:s' format: 'Y-m-d h:i:s'

View file

@ -13,6 +13,12 @@ gist:
404: 404:
title: 'Arf...' title: 'Arf...'
message: "Ce gist n'existe pas." message: "Ce gist n'existe pas."
action:
view: 'Afficher'
history: 'Révision(s)'
raw: 'RAW'
download: 'Télécharger'
fork: 'Clôner'
date: date:
format: 'd/m/Y H\hi s\s' format: 'd/m/Y H\hi s\s'

View file

@ -6,6 +6,7 @@ use Silex\Application;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Gist\Model\GistQuery; use Gist\Model\GistQuery;
use Gist\Model\Gist; use Gist\Model\Gist;
use Symfony\Component\HttpFoundation\Response;
/** /**
* Class HomeController * Class HomeController
@ -13,34 +14,82 @@ use Gist\Model\Gist;
*/ */
class ViewController 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); $gist = GistQuery::create()->findOneByFile($gist);
if (null === $gist) { if (null === $gist) {
return $this->notFoundResponse($app); return null;
} }
$history = $app['gist']->getHistory($gist); $history = $app['gist']->getHistory($gist);
if (empty($history)) { if (empty($history)) {
return $this->notFoundResponse($app); return null;
} }
$content = $this->getContentByCommit($app, $gist, $commit, $history); $content = $this->getContentByCommit($app, $gist, $commit, $history);
return $app['twig']->render( return array(
'View/view.html.twig', 'gist' => $gist,
array( 'type' => $gist->getType(),
'gist' => $gist, 'history' => $history,
'type' => $gist->getType(), 'commit' => $commit,
'history' => $history, 'raw_content' => $content,
'commit' => $commit, 'content' => $app['gist']->highlight($gist->getType(), $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) public function revisionsAction(Request $request, Application $app, $gist)
{ {
$gist = GistQuery::create()->findOneByFile($gist); $gist = GistQuery::create()->findOneByFile($gist);

View file

@ -25,4 +25,19 @@ class Gist extends BaseGist
{ {
$this->setFile(uniqid()); $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());
}
} }

View file

@ -12,11 +12,13 @@
{% if not gist.cipher %} {% if not gist.cipher %}
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
<li> <li>
<a href="{{ path('view', {gist: gist.file}) }}">View</a> <a href="{{ path('view', {gist: gist.file}) }}">
{{ 'gist.action.view'|trans }}
</a>
</li> </li>
<li class="active"> <li class="active">
<a href="{{ path('revisions', {gist: gist.file}) }}"> <a href="{{ path('revisions', {gist: gist.file}) }}">
Révisions {{ 'gist.action.history'|trans }}
<span class="badge">{{ history|length }}</span> <span class="badge">{{ history|length }}</span>
</a> </a>
</li> </li>
@ -25,23 +27,6 @@
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">
{% if not gist.cipher %}
<div class="pull-right actions">
<a class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-eye-open"></span>
RAW
</a>
<a class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-save-file"></span>
Download
</a>
<a class="btn btn-success btn-sm">
<span class="glyphicon glyphicon-copy"></span>
Fork
</a>
</div>
{% endif %}
{{ gist.title ? gist.title : 'gist.untitled'|trans }} {{ gist.title ? gist.title : 'gist.untitled'|trans }}
</div> </div>
<div class="panel-body"> <div class="panel-body">

View file

@ -12,11 +12,13 @@
{% if not gist.cipher %} {% if not gist.cipher %}
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
<li class="active"> <li class="active">
<a href="{{ path('view', {gist: gist.file}) }}">View</a> <a href="{{ path('view', {gist: gist.file}) }}">
{{ 'gist.action.view'|trans }}
</a>
</li> </li>
<li> <li>
<a href="{{ path('revisions', {gist: gist.file}) }}"> <a href="{{ path('revisions', {gist: gist.file}) }}">
Révisions {{ 'gist.action.history'|trans }}
<span class="badge">{{ history|length }}</span> <span class="badge">{{ history|length }}</span>
</a> </a>
</li> </li>
@ -26,17 +28,17 @@
<div class="panel-heading"> <div class="panel-heading">
{% if not gist.cipher %} {% if not gist.cipher %}
<div class="pull-right actions"> <div class="pull-right actions">
<a class="btn btn-default btn-sm"> <a href="{{ path('raw', app.request.attributes.get('_route_params')) }}" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-eye-open"></span> <span class="glyphicon glyphicon-eye-open"></span>
RAW {{ 'gist.action.raw'|trans }}
</a> </a>
<a class="btn btn-default btn-sm"> <a href="{{ path('download', app.request.attributes.get('_route_params')) }}" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-save-file"></span> <span class="glyphicon glyphicon-save-file"></span>
Download {{ 'gist.action.download'|trans }}
</a> </a>
<a class="btn btn-success btn-sm"> <a class="btn btn-success btn-sm">
<span class="glyphicon glyphicon-copy"></span> <span class="glyphicon glyphicon-copy"></span>
Fork {{ 'gist.action.fork'|trans }}
</a> </a>
</div> </div>
{% endif %} {% endif %}
@ -46,11 +48,11 @@
<div class="panel-body"> <div class="panel-body">
<div class="tab-content"> <div class="tab-content">
<div id="view" class="tab-pane active in"> <div id="view" class="tab-pane active in">
<div class="pull-right"> <div class="pull-right">
<span class="btn btn-warning btn-xs"> <span class="btn btn-warning btn-xs">
{{ commit|slice(0, 10) }} {{ commit|slice(0, 10) }}
</span> </span>
</div> </div>
<div class="btn-toolbar"> <div class="btn-toolbar">
<div class="btn-group" id="languages"> <div class="btn-group" id="languages">
<div class="btn-group"> <div class="btn-group">

View file

@ -41,14 +41,16 @@
</a> </a>
</li> </li>
</ul> </ul>
<p class="navbar-text navbar-right"> {% block langs %}
<a class="btn btn-xs" href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({_locale: 'en'})) }}"> <p class="navbar-text navbar-right">
<span class="flag-icon flag-icon-gb"></span> <a class="btn btn-xs" href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({_locale: 'en'})) }}">
</a> <span class="flag-icon flag-icon-gb"></span>
<a class="btn btn-xs" href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({_locale: 'fr'})) }}"> </a>
<span class="flag-icon flag-icon-fr"></span> <a class="btn btn-xs" href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({_locale: 'fr'})) }}">
</a> <span class="flag-icon flag-icon-fr"></span>
</p> </a>
</p>
{% endblock %}
</div> </div>
</div> </div>
</nav> </nav>

View file

@ -4,6 +4,10 @@
Error {{ code }} - {{ name }} Error {{ code }} - {{ name }}
{% endblock %} {% endblock %}
{% block langs %}
{% endblock %}
{% block body %} {% block body %}
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">

View file

@ -66,7 +66,7 @@ class GistService
public function getContent(Gist $gist, $commit) public function getContent(Gist $gist, $commit)
{ {
$command = GitCommand::getInstance('cat-file', '--textconv', $commit.':'.$gist->getFile()); $command = GitCommand::getInstance('cat-file', '-p', $commit.':'.$gist->getFile());
$command->setDirectory($this->gistPath); $command->setDirectory($this->gistPath);
$command->bypass(false); $command->bypass(false);