diff --git a/app/Http/Controllers/PasteController.php b/app/Http/Controllers/PasteController.php index 2daeb54..1dc0290 100644 --- a/app/Http/Controllers/PasteController.php +++ b/app/Http/Controllers/PasteController.php @@ -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 } @@ -91,6 +118,52 @@ class PasteController extends Controller return response($paste->content)->header('Content-Type', 'text/plain'); } } + + 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) { diff --git a/app/Http/routes.php b/app/Http/routes.php index 4d0024b..5b80e9c 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -15,4 +15,6 @@ Route::get('/', function () { return view('welcome'); }); +Route::post('/paste', 'PasteController@create'); + Route::get('/paste/{paste}', 'PasteController@view'); diff --git a/app/Paste.php b/app/Paste.php index 4bbba5c..886a8ca 100644 --- a/app/Paste.php +++ b/app/Paste.php @@ -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); @@ -40,7 +55,7 @@ class Paste extends Model { Storage::disk('ephemeral')->delete($this->id); } - } + } public function getDeletedAttribute() {