From 5fa15802d63f4056edf63288edad2ed5b62c283f Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Fri, 24 Jun 2016 15:24:15 +1000 Subject: [PATCH] Add title --- assets/index.html | 1 + main.go | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/assets/index.html b/assets/index.html index 8a7d997..51bddaf 100644 --- a/assets/index.html +++ b/assets/index.html @@ -33,6 +33,7 @@
+ Paste your text here
diff --git a/main.go b/main.go index 9300c5c..c3ef6cd 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,7 @@ const ( type Response struct { ID string `json:"id"` + TITLE string `json:"title"` HASH string `json:"hash"` URL string `json:"url"` SIZE int `json:"size"` @@ -82,7 +83,7 @@ func hash(paste string) string { return sha } -func save(raw string, lang string) []string { +func save(raw string, lang string, title string) []string { db, err := sql.Open("mysql", DATABASE) check(err) @@ -107,12 +108,17 @@ func save(raw string, lang string) []string { delKey := uniuri.NewLen(40) paste := html.EscapeString(raw) - stmt, err := db.Prepare("INSERT INTO pastebin(id, hash, data, delkey) values(?,?,?,?)") - check(err) - _, err = stmt.Exec(id, sha, paste, delKey) + stmt, err := db.Prepare("INSERT INTO pastebin(id, title, hash, data, delkey) values(?,?,?,?,?)") check(err) + if title == "" { + _, err = stmt.Exec(id, id, sha, paste, delKey) + check(err) + } else { + _, err = stmt.Exec(id, title, sha, paste, delKey) + check(err) + } db.Close() - return []string{id, sha, url, paste, delKey} + return []string{id, title, sha, url, paste, delKey} } func delHandler(w http.ResponseWriter, r *http.Request) { @@ -145,17 +151,19 @@ func saveHandler(w http.ResponseWriter, r *http.Request) { case "POST": paste := r.FormValue("p") lang := r.FormValue("lang") + title := r.FormValue("title") if paste == "" { http.Error(w, "Empty paste", 500) return } - values := save(paste, lang) + values := save(paste, lang, title) b := &Response{ ID: values[0], - HASH: values[1], - URL: values[2], - SIZE: len(values[3]), - DELKEY: values[4], + TITLE: values[1], + HASH: values[2], + URL: values[3], + SIZE: len(values[4]), + DELKEY: values[5], } switch output {