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}
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}

View file

@ -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'

View file

@ -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'

View file

@ -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);

View file

@ -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());
}
}

View file

@ -12,11 +12,13 @@
{% if not gist.cipher %}
<ul class="nav nav-tabs">
<li>
<a href="{{ path('view', {gist: gist.file}) }}">View</a>
<a href="{{ path('view', {gist: gist.file}) }}">
{{ 'gist.action.view'|trans }}
</a>
</li>
<li class="active">
<a href="{{ path('revisions', {gist: gist.file}) }}">
Révisions
{{ 'gist.action.history'|trans }}
<span class="badge">{{ history|length }}</span>
</a>
</li>
@ -25,23 +27,6 @@
<div class="panel panel-default">
<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 }}
</div>
<div class="panel-body">

View file

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

View file

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

View file

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

View file

@ -66,7 +66,7 @@ class GistService
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->bypass(false);