Implement basic paste viewing

master
Les De Ridder 7 years ago
parent f8fbce2285
commit 03aece231f
  1. 196
      app/Http/Controllers/PasteController.php
  2. 2
      app/Http/routes.php
  3. 5
      app/Paste.php

@ -0,0 +1,196 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Paste;
use Pygmentize\Pygmentize;
class PasteController extends Controller
{
public function view(Request $request, Paste $paste)
{
$format = $request->input('format', 'html'); //TODO: Use HTTP content negotiation for the default format
switch($format)
{
case 'html':
return $this->view_html($paste);
case 'raw':
return $this->view_raw($paste);
case 'json':
return $this->view_json($paste);
case 'latex':
return $this->view_latex($paste);
case 'png':
return $this->view_png($paste);
default:
break; //TODO: Throw an exception
}
}
private function view_html(Paste $paste)
{
if($paste->deleted)
{
return view('paste.deleted', [
'id' => $paste->id,
'deletion_reason' => $paste->deletion->reason,
'deleted_by' => $paste->deletion->deleted_by,
'deleted_at' => $paste->deletion->deleted_at
]);
}
else if($paste->is_future_paste)
{
return view('paste.future_paste', [
'id' => $paste->id,
'available_at' => $paste->created_at
]);
}
else if($paste->has_expired)
{
return view('paste.expired', [
'id' => $paste->id
]);
}
else
{
$content = Pygmentize::highlight($paste->content, $paste->language, "utf-8", "html");
return view('paste.available', [
'id' => $paste->id,
'language' => $paste->language,
'created_at' => $paste->created_at,
'expires_at' => $paste->expires_at,
'content' => $content
]);
}
}
private function view_raw(Paste $paste)
{
//TODO: More informative messages
if($paste->deleted)
{
response('This paste was deleted.')->header('Content-Type', 'text/plain');
}
else if($paste->is_future_paste)
{
response('This paste is not available yet.')->header('Content-Type', 'text/plain');
}
else if($paste->has_expired)
{
response('This paste has expired.')->header('Content-Type', 'text/plain');
}
else
{
return response($paste->content)->header('Content-Type', 'text/plain');
}
}
private function view_json(Paste $paste)
{
if($paste->deleted)
{
return response()->json([
'id' => $paste->id,
'deleted' => true,
'deletion_reason' => $paste->deletion->reason,
'deleted_by' => $paste->deletion->deleted_by,
'deleted_at' => $paste->deletion->deleted_at
]);
}
else if($paste->is_future_paste)
{
return response()->json([
'id' => $paste->id,
'deleted' => false,
'future_paste' => true,
'available_at' => $paste->created_at
]);
}
else if($paste->has_expired)
{
return response()->json([
'id' => $paste->id,
'deleted' => false,
'future_paste' => false,
'expired' => true
]);
}
else
{
return response()->json([
'id' => $paste->id,
'deleted' => false,
'future_paste' => false,
'expired' => false,
'language' => $paste->language,
'created_at' => $paste->created_at,
'expires_at' => $paste->expires_at,
'content' => $paste->content
]);
}
}
private function view_latex(Paste $paste)
{
//TODO: More informative messages (PNG?)
if($paste->deleted)
{
response('\begin{Verbatim}
This paste was deleted.
\end{Verbatim}
')
->header('Content-Type', 'text/plain');
}
else if($paste->is_future_paste)
{
response('\begin{Verbatim}
This paste is not available yet.
\end{Verbatim}
')
->header('Content-Type', 'text/plain');
}
else if($paste->has_expired)
{
response('\begin{Verbatim}
This paste has expired.
\end{Verbatim}
')
->header('Content-Type', 'text/plain');
}
else
{
$content = Pygmentize::highlight($paste->content, $paste->language, "utf-8", "latex");
return response($content)->header('Content-Type', 'text/plain');
}
}
private function view_png(Paste $paste)
{
//TODO: More informative messages (PNG?)
if($paste->deleted)
{
response('This paste was deleted.')->header('Content-Type', 'text/plain');
}
else if($paste->is_future_paste)
{
response('This paste is not available yet.')->header('Content-Type', 'text/plain');
}
else if($paste->has_expired)
{
response('This paste has expired.')->header('Content-Type', 'text/plain');
}
else
{
$content = Pygmentize::highlight($paste->content, $paste->language, "utf-8", "png");
return response($content)->header('Content-Type', 'image/png');
}
}
}

@ -14,3 +14,5 @@
Route::get('/', function () {
return view('welcome');
});
Route::get('/paste/{paste}', 'PasteController@view');

@ -57,6 +57,11 @@ class Paste extends Model
return $this->expires && $this->expires_at < Carbon::now();
}
public function getIsFuturePasteAttribute()
{
return $this->created_at > Carbon::now();
}
public function getContentAttribute()
{
if($this->deleted)

Loading…
Cancel
Save