Add basic paste creation

This commit is contained in:
Les De Ridder 2016-05-29 17:32:30 +02:00
parent 03aece231f
commit 21aa7e2dcf
3 changed files with 91 additions and 1 deletions

View File

@ -9,9 +9,32 @@ use App\Http\Requests;
use App\Paste;
use Pygmentize\Pygmentize;
use Carbon\Carbon;
class PasteController extends Controller
{
public function create(Request $request)
{
//TODO: Input validation
$paste = new Paste;
$paste->id = Paste::create_id();
$paste->created_at = $request->input('available_at', Carbon::now());
$paste->expires_at = $request->input('expires_at', null);
$paste->language = $request->input('language', 'text');
$paste->save();
$paste->content = $request->input('content');
if($request->input('redirect',false))
{
return redirect(url("/paste/$paste->id"));
}
else
{
return response(url("/paste/$paste->id"))->header('Content-Type', 'text/plain');
}
}
public function view(Request $request, Paste $paste)
{
$format = $request->input('format', 'html'); //TODO: Use HTTP content negotiation for the default format
@ -28,6 +51,10 @@ class PasteController extends Controller
return $this->view_latex($paste);
case 'png':
return $this->view_png($paste);
case 'terminal':
return $this->view_terminal($paste);
case 'terminal256':
return $this->view_terminal256($paste);
default:
break; //TODO: Throw an exception
}
@ -92,6 +119,52 @@ class PasteController extends Controller
}
}
private function view_terminal(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
{
$content = Pygmentize::highlight($paste->content, $paste->language, "utf-8", "terminal");
return response($content)->header('Content-Type', 'text/plain');
}
}
private function view_terminal256(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
{
$content = Pygmentize::highlight($paste->content, $paste->language, "utf-8", "terminal256");
return response($content)->header('Content-Type', 'text/plain');
}
}
private function view_json(Paste $paste)
{
if($paste->deleted)

View File

@ -15,4 +15,6 @@ Route::get('/', function () {
return view('welcome');
});
Route::post('/paste', 'PasteController@create');
Route::get('/paste/{paste}', 'PasteController@view');

View File

@ -12,6 +12,21 @@ class Paste extends Model
public $incrementing = false;
public $timestamps = false;
public static function create_id()
{
//TODO: Check if id is unique
$characters = 'abcdefghijklmnopqrstuvwxyz';
$id = '';
for($i = 0; $i < 6; $i++) //TODO: Make id length configurable (?)
{
$id .= $characters[rand(0, strlen($characters) -1)];
}
return $id;
}
public function deletion()
{
return $this->hasOne(Deletion::class);