Add title

This commit is contained in:
Eliot Whalan 2016-06-24 15:24:15 +10:00
parent 9356680dbe
commit da55f3847c
2 changed files with 19 additions and 10 deletions

View File

@ -33,6 +33,7 @@
<fieldset> <fieldset>
<div class="well" style="padding-top: 10px;"> <div class="well" style="padding-top: 10px;">
<div class="form-group is-empty" style="margin-top: 0px;"> <div class="form-group is-empty" style="margin-top: 0px;">
<textarea class="form-control" rows="1" id="textArea" name="title"></textarea>
<textarea class="form-control" rows="20" id="textArea" name="p"></textarea> <textarea class="form-control" rows="20" id="textArea" name="p"></textarea>
<span class="help-block">Paste your text here</span> <span class="help-block">Paste your text here</span>
</div> </div>

28
main.go
View File

@ -32,6 +32,7 @@ const (
type Response struct { type Response struct {
ID string `json:"id"` ID string `json:"id"`
TITLE string `json:"title"`
HASH string `json:"hash"` HASH string `json:"hash"`
URL string `json:"url"` URL string `json:"url"`
SIZE int `json:"size"` SIZE int `json:"size"`
@ -82,7 +83,7 @@ func hash(paste string) string {
return sha return sha
} }
func save(raw string, lang string) []string { func save(raw string, lang string, title string) []string {
db, err := sql.Open("mysql", DATABASE) db, err := sql.Open("mysql", DATABASE)
check(err) check(err)
@ -107,12 +108,17 @@ func save(raw string, lang string) []string {
delKey := uniuri.NewLen(40) delKey := uniuri.NewLen(40)
paste := html.EscapeString(raw) paste := html.EscapeString(raw)
stmt, err := db.Prepare("INSERT INTO pastebin(id, hash, data, delkey) values(?,?,?,?)") stmt, err := db.Prepare("INSERT INTO pastebin(id, title, hash, data, delkey) values(?,?,?,?,?)")
check(err)
_, err = stmt.Exec(id, sha, paste, delKey)
check(err) 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() 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) { func delHandler(w http.ResponseWriter, r *http.Request) {
@ -145,17 +151,19 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
case "POST": case "POST":
paste := r.FormValue("p") paste := r.FormValue("p")
lang := r.FormValue("lang") lang := r.FormValue("lang")
title := r.FormValue("title")
if paste == "" { if paste == "" {
http.Error(w, "Empty paste", 500) http.Error(w, "Empty paste", 500)
return return
} }
values := save(paste, lang) values := save(paste, lang, title)
b := &Response{ b := &Response{
ID: values[0], ID: values[0],
HASH: values[1], TITLE: values[1],
URL: values[2], HASH: values[2],
SIZE: len(values[3]), URL: values[3],
DELKEY: values[4], SIZE: len(values[4]),
DELKEY: values[5],
} }
switch output { switch output {