2016-06-10 13:56:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-06-10 14:17:27 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/dchest/uniuri"
|
2016-06-10 13:56:42 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-06-10 14:17:27 +00:00
|
|
|
"os"
|
2016-06-10 13:56:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func check(e error) {
|
|
|
|
if e != nil {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:17:27 +00:00
|
|
|
func exists(location string) bool {
|
|
|
|
if _, err := os.Stat(location); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateName() string {
|
|
|
|
s := uniuri.NewLen(4)
|
|
|
|
return s
|
|
|
|
|
|
|
|
}
|
|
|
|
func save(buf []byte) string {
|
|
|
|
paste := buf[92 : len(buf)-46]
|
|
|
|
address := "localhost:8080/p/"
|
|
|
|
|
|
|
|
dir := "/tmp/"
|
|
|
|
s := generateName()
|
|
|
|
loc := dir + s
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(loc, paste, 0644)
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
url := address + s
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2016-06-10 13:56:42 +00:00
|
|
|
func pasteHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
buf, _ := ioutil.ReadAll(req.Body)
|
2016-06-10 14:17:27 +00:00
|
|
|
fmt.Fprintf(w, save(buf))
|
2016-06-10 13:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", pasteHandler)
|
2016-06-10 14:17:27 +00:00
|
|
|
http.Handle("/p/", http.StripPrefix("/p/", http.FileServer(http.Dir("/tmp"))))
|
|
|
|
|
2016-06-10 13:56:42 +00:00
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
|
|
|
|
}
|