Start expiry addition

This commit is contained in:
Eliot Whalan 2016-06-25 08:13:36 +10:00
parent 6420d7dfed
commit 63dff38c3c
1 changed files with 49 additions and 13 deletions

50
main.go
View File

@ -13,6 +13,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"time"
"github.com/dchest/uniuri" "github.com/dchest/uniuri"
"github.com/ewhal/pygments" "github.com/ewhal/pygments"
@ -37,6 +38,7 @@ type Response struct {
URL string `json:"url"` URL string `json:"url"`
SIZE int `json:"size"` SIZE int `json:"size"`
DELKEY string `json:"delkey"` DELKEY string `json:"delkey"`
EXPIRY time.Time `json:"expiry"`
} }
type Page struct { type Page struct {
@ -83,15 +85,16 @@ func hash(paste string) string {
return sha return sha
} }
func save(raw string, lang string, title string) []string { func save(raw string, lang string, title string, expiry string) []string {
db, err := sql.Open("mysql", DATABASE) db, err := sql.Open("mysql", DATABASE)
check(err) check(err)
sha := hash(raw) sha := hash(raw)
query, err := db.Query("select id, title, hash, data, delkey from pastebin") query, err := db.Query("select id, title, hash, data, delkey, expiry from pastebin")
for query.Next() { for query.Next() {
var id, title, hash, paste, delkey string var id, title, hash, paste, delkey string
err := query.Scan(&id, &title, &hash, &paste, &delkey) var expiry time.Time
err := query.Scan(&id, &title, &hash, &paste, &delkey, &expiry)
check(err) check(err)
if hash == sha { if hash == sha {
url := ADDRESS + "/p/" + id url := ADDRESS + "/p/" + id
@ -105,16 +108,48 @@ func save(raw string, lang string, title string) []string {
} else { } else {
url = ADDRESS + "/p/" + id + "/" + lang url = ADDRESS + "/p/" + id + "/" + lang
} }
now := time.Now()
switch expiry {
case "5 minutes":
expiry := now.Add(time.Minute * 5).Format(time.RFC3339)
break
case "1 hour":
expiry := now.Add(time.Hour + 1).Format(time.RFC3339)
break
case "1 day":
expiry := now.Add(time.Hour * 24 * 1).Format(time.RFC3339)
break
case "1 week":
expiry := now.Add(time.Hour * 24 * 7).Format(time.RFC3339)
break
case "1 month":
expiry := now.Add(time.Hour * 24 * 30).Format(time.RFC3339)
break
case "1 year":
expiry := now.Add(time.Hour * 24 * 365).Format(time.RFC3339)
break
default:
expiry := now.Format(time.RFC3339)
break
}
delKey := uniuri.NewLen(40) delKey := uniuri.NewLen(40)
paste := html.EscapeString(raw) paste := html.EscapeString(raw)
stmt, err := db.Prepare("INSERT INTO pastebin(id, title, hash, data, delkey) values(?,?,?,?,?)") stmt, err := db.Prepare("INSERT INTO pastebin(id, title, hash, data, delkey, expiry) values(?,?,?,?,?,?)")
check(err) check(err)
if title == "" { if title == "" {
_, err = stmt.Exec(id, id, sha, paste, delKey) _, err = stmt.Exec(id, id, sha, paste, delKey, expiry)
check(err) check(err)
} else { } else {
_, err = stmt.Exec(id, html.EscapeString(title), sha, paste, delKey) _, err = stmt.Exec(id, html.EscapeString(title), sha, paste, delKey, expiry)
check(err) check(err)
} }
db.Close() db.Close()
@ -152,11 +187,12 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
paste := r.FormValue("p") paste := r.FormValue("p")
lang := r.FormValue("lang") lang := r.FormValue("lang")
title := r.FormValue("title") title := r.FormValue("title")
expiry := r.FormValue("expiry")
if paste == "" { if paste == "" {
http.Error(w, "Empty paste", 500) http.Error(w, "Empty paste", 500)
return return
} }
values := save(paste, lang, title) values := save(paste, lang, title, expiry)
b := &Response{ b := &Response{
ID: values[0], ID: values[0],
TITLE: values[1], TITLE: values[1],