From 8f5b41695d0f2b291b4ce3e26ee24c5f26e04cfb Mon Sep 17 00:00:00 2001 From: KatolaZ Date: Mon, 3 Jul 2017 13:08:46 +0100 Subject: moved paste operations into paste/paste.go. Using paste.Store() --- main.go | 59 ++++++++++++++++++++--------------------------------------- 1 file changed, 20 insertions(+), 39 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 4aef702..817e4da 100644 --- a/main.go +++ b/main.go @@ -25,15 +25,14 @@ package main import ( - "crypto/sha256" "fmt" - "io/ioutil" "log" "net/http" "os" "path/filepath" "time" "io" + "binnit/paste" ) @@ -101,7 +100,6 @@ func handle_put_paste(w http.ResponseWriter, r *http.Request) { // Invalid POST -- let's serve the default file http.ServeFile(w, r, p_conf.templ_dir + "/index.html") } else { - h := sha256.New() req_body := r.PostForm orig_IP := r.RemoteAddr @@ -110,48 +108,30 @@ func handle_put_paste(w http.ResponseWriter, r *http.Request) { // get title, body, and time title := req_body.Get("title") - paste := req_body.Get("paste") - now := time.Now().String() - // format content + date := time.Now().String() + content := req_body.Get("paste") + + content = content[0:min(len(content), int(p_conf.max_size))] - paste = paste[0:min(len(paste), int(p_conf.max_size))] + ID, err := paste.Store(title, date, content, p_conf.paste_dir) - content := fmt.Sprintf("# Title: %s\n# Pasted: %s\n------------\n%s", title, now, paste) - - // ccompute the sha256 hash using title, body, and time - h.Write([]byte(content)) - - paste_hash := fmt.Sprintf("%x", h.Sum(nil)) - log.Printf(" `-- hash: %s\n", paste_hash) - paste_dir := p_conf.paste_dir + "/" - - // Now we save the file - for i := 0; i < len(paste_hash)-16; i++ { - paste_name := paste_hash[i:i+16] - if _, err := os.Stat(paste_dir + paste_name); os.IsNotExist(err) { - // The file does not exist, so we can create it - if err := ioutil.WriteFile(paste_dir+ paste_name, []byte(content), 0644); err == nil { - // and then we return the URL: - log.Printf(" `-- saving paste to : %s", paste_dir + paste_name) - //hostname := r.Host - hostname := p_conf.server_name - if show := req_body.Get("show"); show != "1" { - fmt.Fprintf(w, "%s/%s", hostname, paste_name) - return - } else{ - fmt.Fprintf(w, "Link: http://%s/%s", - hostname, paste_hash[i:i+16], hostname, paste_hash[i:i+16]) - return - } - } else { - fmt.Fprintf(w, "Cannot create the paste.. Sorry!\n") - return - } + log.Printf(" ID: %s; err: %s\n", ID, err) + + if err == nil { + hostname := p_conf.server_name + if show := req_body.Get("show"); show != "1" { + fmt.Fprintf(w, "%s/%s", hostname, ID) + return + } else{ + fmt.Fprintf(w, "Link: http://%s/%s", + hostname, ID, hostname, ID) + return } + } else { + fmt.Fprintf(w, "%s\n", err) } } } - func req_handler(w http.ResponseWriter, r *http.Request) { switch r.Method { @@ -190,6 +170,7 @@ func main() { log.Printf(" + templ_dir: %s\n", p_conf.templ_dir) log.Printf(" + max_size: %d\n", p_conf.max_size) + // FIXME: create paste_dir if it does not exist http.HandleFunc("/", req_handler) log.Fatal(http.ListenAndServe(p_conf.bind_addr + ":" + p_conf.bind_port, nil)) -- cgit v1.2.3