commit 0b30ea77e3a8f409ee3af21b0f831bafba76cc1d
author: monaco <arianitkukaj@gmail.com>
date: 2023-01-27 23:01
parents: 0a4e6923
Fully featured paste
| D | kpsm2.pl | +0 | -75 |
| A | paste.pl | +85 | -0 |
diff --git a/kpsm2.pl b/kpsm2.pl deleted file mode 100644 index 1c137d2..0000000 --- a/kpsm2.pl +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env perl - -use Mojolicious::Lite; -use DBI; -use Digest::SHA qw(sha1_hex); - -my $dbh = DBI->connect("dbi:SQLite:dbname=pastes.db", "", "", { RaiseError => 1 }); - -my $table_exists = $dbh->selectrow_array("SELECT name FROM sqlite_master WHERE type='table' AND name='pastes'"); -if (!$table_exists) { - $dbh->do("CREATE TABLE pastes (id TEXT PRIMARY KEY, content TEXT)"); -} - -get '/' => sub { - my $c = shift; - $c->render(template => 'index'); -}; - -post '/create' => sub { - my $c = shift; - my $content = $c->param('content'); - my $id = sha1_hex(time . $content); - $dbh->do("INSERT INTO pastes (id, content) VALUES (?, ?)", undef, $id, $content); - $c->redirect_to("/view/$id"); -}; - -get '/view/:id' => sub { - my $c = shift; - my $id = $c->param('id'); - my $content = $dbh->selectrow_array("SELECT content FROM pastes WHERE id = ?", undef, $id); - $c->render(text => $content); -}; - -get '/search' => sub { - my $c = shift; - my $query = $c->param('q'); - my $results = $dbh->selectall_arrayref("SELECT id FROM pastes WHERE content LIKE ?", { Slice => {} }, "%$query%"); - $c->render(template => 'search', results => $results); -}; - -app->start; -__DATA__ - -@@ index.html.ep -<!DOCTYPE html> -<html> -<head> - <title>Pastebin</title> - <link rel="stylesheet" type="text/css" href="style.css"> -</head> -<body> - <h1>Create a new paste</h1> - <form action="/create" method="post"> - <textarea name="content" rows="10" cols="80"></textarea> - <input type="submit" value="Create"> - </form> - <h1>Search for a paste</h1> - <form action="/search" method="get"> - <input type="text" name="q"> - <input type="submit" value="Search"> - </form> -</body> -</html> - -@@ search.html.ep -<!DOCTYPE html> -<html> -<head> - <title>Pastebin</title> - <link rel="stylesheet" type="text/css" href="style.css"> -</head> -<body> - <h1>Search Results</h1> - <ul> - % for my $result (@$results) { diff --git a/paste.pl b/paste.pl new file mode 100644 index 0000000..f3f4375 --- /dev/null +++ b/paste.pl @@ -0,0 +1,85 @@ +#!/usr/bin/env perl + +use Mojolicious::Lite; +use DBI; +use File::Slurp; + +# set up the sqlite database +my $dbh = DBI->connect("dbi:SQLite:dbname=pastes.db","","", { RaiseError => 1, AutoCommit => 1 }); +$dbh->do("CREATE TABLE IF NOT EXISTS pastes (id INTEGER PRIMARY KEY, content TEXT, expires TIMESTAMP)"); + +# set up the routes +get '/' => sub { + my $c = shift; + $c->render(template => 'index'); +}; + +post '/' => sub { + my $c = shift; + my $content = $c->param('content'); + my $expires = time() + (60 * 60 * 24 * 60); # 60 days from now + my $id = int(rand(1000000)); + my $sth = $dbh->prepare("INSERT INTO pastes (id, content, expires) VALUES (?, ?, ?)"); + $sth->execute($id, $content, $expires); + write_file("pastes/$id.txt", $content); + $c->redirect_to("/$id"); +}; + +get '/:id' => sub { + my $c = shift; + my $id = $c->param('id'); + my $sth = $dbh->prepare("SELECT * FROM pastes WHERE id = ?"); + $sth->execute($id); + my $paste = $sth->fetchrow_hashref; + if ($paste) { + $c->render(template => 'paste', paste => $paste); + } else { + $c->render(text => "Paste not found"); + } +}; + +# start the app +app->start; + +__DATA__ + +@@ index.html.ep +<!DOCTYPE html> +<html> +<head> + <title>Pastebin</title> + <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> +</head> +<body> +<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"> +<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> +<meta name="HandheldFriendly" content="true"> + + <center><h1>Pastebin</h1></center> + <center><form method="post"> + <textarea name="content" style="margin: 0px; width: 1078px; height: 356px;"></textarea> + </br> + <input type="submit" value="Create paste"></center> + </form> +</body> +</html> + +@@ paste.html.ep +<!DOCTYPE html> +<html> +<head> + <title>Paste</title> + <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> +</head> +<body> + +<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"> +<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> +<meta name="HandheldFriendly" content="true"> + + + <h1>Paste</h1> + <pre></p><%= $paste->{content} %></p></pre> + <p>Expires: <%= $paste->{expires} %></p> +</body> +</html>