go fmt
This commit is contained in:
105
routes/paste.go
105
routes/paste.go
@@ -1,20 +1,20 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sour.is/x/httpsrv"
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"sour.is/x/log"
|
||||
"os"
|
||||
"golang.org/x/sys/unix"
|
||||
"crypto/rand"
|
||||
"strconv"
|
||||
"bufio"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"github.com/gorilla/mux"
|
||||
"golang.org/x/sys/unix"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"sour.is/x/httpsrv"
|
||||
"sour.is/x/log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -26,23 +26,23 @@ func init() {
|
||||
httpsrv.RegisterModule("paste", setConfig)
|
||||
|
||||
httpsrv.HttpRegister("paste", httpsrv.HttpRoutes{
|
||||
{ "getRandom", "GET", "/paste/rng", getRandom, },
|
||||
{ "getPaste", "GET", "/paste/{id}", getPaste, },
|
||||
{ "getPaste", "GET", "/paste/get/{id}", getPaste, },
|
||||
{ "postPaste", "POST", "/paste", postPaste, },
|
||||
{"getRandom", "GET", "/paste/rng", getRandom},
|
||||
{"getPaste", "GET", "/paste/{id}", getPaste},
|
||||
{"getPaste", "GET", "/paste/get/{id}", getPaste},
|
||||
{"postPaste", "POST", "/paste", postPaste},
|
||||
|
||||
{ "getRandom", "GET", "/api/rng", getRandom, },
|
||||
{ "getPaste", "GET", "/api/{id}", getPaste, },
|
||||
{ "getPaste", "GET", "/api/get/{id}", getPaste, },
|
||||
{ "postPaste", "POST", "/api", postPaste, },
|
||||
{"getRandom", "GET", "/api/rng", getRandom},
|
||||
{"getPaste", "GET", "/api/{id}", getPaste},
|
||||
{"getPaste", "GET", "/api/get/{id}", getPaste},
|
||||
{"postPaste", "POST", "/api", postPaste},
|
||||
})
|
||||
|
||||
httpsrv.AssetRegister("paste", httpsrv.AssetRoutes{
|
||||
{ "Assets", "/", httpsrv.FsHtml5( assetFS() ) },
|
||||
{"Assets", "/", httpsrv.FsHtml5(assetFS())},
|
||||
})
|
||||
}
|
||||
|
||||
func setConfig (config map[string]string) {
|
||||
func setConfig(config map[string]string) {
|
||||
|
||||
store = "data/"
|
||||
if config["store"] != "" {
|
||||
@@ -63,28 +63,48 @@ func setConfig (config map[string]string) {
|
||||
|
||||
func chkStore(path string) bool {
|
||||
file, err := os.Stat(path)
|
||||
if err == nil { return true }
|
||||
if os.IsNotExist(err) { return false }
|
||||
if !file.IsDir() { return false }
|
||||
if unix.Access(path, unix.W_OK & unix.R_OK) != nil { return false }
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
if !file.IsDir() {
|
||||
return false
|
||||
}
|
||||
if unix.Access(path, unix.W_OK&unix.R_OK) != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func chkFile(path string) bool {
|
||||
file, err := os.Stat(path)
|
||||
if err == nil { return true }
|
||||
if os.IsNotExist(err) { return false }
|
||||
if file.IsDir() { return false }
|
||||
if unix.Access(path, unix.W_OK & unix.R_OK) != nil { return false }
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
if file.IsDir() {
|
||||
return false
|
||||
}
|
||||
if unix.Access(path, unix.W_OK&unix.R_OK) != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func chkGone(path string) bool {
|
||||
file, err := os.Stat(path)
|
||||
if err != nil { return true }
|
||||
if file.Size() == 0 { return true }
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
if file.Size() == 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -92,7 +112,7 @@ func getRandom(w http.ResponseWriter, r *http.Request) {
|
||||
s := make([]byte, randBytes)
|
||||
rand.Read(s)
|
||||
|
||||
w.Header().Set("content-type","application/octet-stream")
|
||||
w.Header().Set("content-type", "application/octet-stream")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(s)
|
||||
}
|
||||
@@ -102,22 +122,21 @@ func getPaste(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
|
||||
|
||||
if !chkFile(store + id) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("ERR Not Found"))
|
||||
return
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte("ERR Not Found"))
|
||||
return
|
||||
}
|
||||
|
||||
if chkGone(store + id) {
|
||||
w.WriteHeader(http.StatusGone)
|
||||
w.Write([]byte("ERR Gone"))
|
||||
w.WriteHeader(http.StatusGone)
|
||||
w.Write([]byte("ERR Gone"))
|
||||
return
|
||||
}
|
||||
|
||||
head, err := os.Open(store + id)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer head.Close()
|
||||
|
||||
@@ -127,7 +146,7 @@ func getPaste(w http.ResponseWriter, r *http.Request) {
|
||||
txt := scanner.Text()
|
||||
log.Debug(txt)
|
||||
|
||||
if (txt == "") {
|
||||
if txt == "" {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -183,7 +202,7 @@ func postPaste(w http.ResponseWriter, r *http.Request) {
|
||||
s256 := sha256.Sum256(body)
|
||||
id := base64.RawURLEncoding.EncodeToString(s256[12:])
|
||||
|
||||
ioutil.WriteFile(store + id, body, 0644)
|
||||
ioutil.WriteFile(store+id, body, 0644)
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
w.Write([]byte("OK " + id))
|
||||
@@ -194,11 +213,11 @@ func postPaste(w http.ResponseWriter, r *http.Request) {
|
||||
func checkErr(err error, w http.ResponseWriter) {
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(err);
|
||||
json.NewEncoder(w).Encode(err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteFile(path string) {
|
||||
ioutil.WriteFile(path, []byte(""), 0644)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user