faq page; put uploads; config
This commit is contained in:
+26
-21
@@ -62,7 +62,8 @@ func staticPage(pages *template.Template, name string) http.HandlerFunc {
|
||||
|
||||
func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
if r.Method != http.MethodPost && r.Method != http.MethodPut {
|
||||
w.Header().Set("Allow", "POST,PUT")
|
||||
http.Error(w, "invalid method", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
@@ -70,10 +71,14 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.Handl
|
||||
// use the largest of the sizes, and then do another size check per type down the line
|
||||
r.Body = http.MaxBytesReader(w, r.Body, max(cfg.MaxFileBytes, cfg.MaxPasteBytes))
|
||||
|
||||
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
http.Error(w, "failed to find or parse Content-Type", http.StatusBadRequest)
|
||||
return
|
||||
var err error
|
||||
mediaType := "text/plain"
|
||||
if r.Method == http.MethodPost || r.Header.Get("Content-Type") != "" {
|
||||
mediaType, _, err = mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
http.Error(w, "failed to find or parse Content-Type", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
idLength := 16
|
||||
@@ -86,20 +91,6 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.Handl
|
||||
returnType = "json"
|
||||
}
|
||||
|
||||
expLabel := r.URL.Query().Get("expiry")
|
||||
if expLabel == "" {
|
||||
expLabel = r.PostFormValue("expiry") // html form request
|
||||
}
|
||||
if expLabel == "" {
|
||||
expLabel = cfg.Expiries[0].Label
|
||||
}
|
||||
|
||||
selected, found := findExpiry(cfg.Expiries, expLabel)
|
||||
if !found {
|
||||
http.Error(w, "invalid expiry", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var input UploadInput
|
||||
switch mediaType {
|
||||
case "multipart/form-data": // file uploads
|
||||
@@ -132,13 +123,13 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.Handl
|
||||
r.Body = http.MaxBytesReader(w, r.Body, cfg.MaxPasteBytes)
|
||||
var content string
|
||||
if mediaType == "application/x-www-form-urlencoded" {
|
||||
err = r.ParseForm()
|
||||
err := r.ParseForm()
|
||||
if err == nil {
|
||||
content = r.PostForm.Get("content")
|
||||
}
|
||||
} else {
|
||||
var body []byte
|
||||
body, err = io.ReadAll(r.Body)
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err == nil {
|
||||
content = string(body)
|
||||
}
|
||||
@@ -170,6 +161,20 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template) http.Handl
|
||||
return
|
||||
}
|
||||
|
||||
expLabel := r.URL.Query().Get("expiry")
|
||||
if expLabel == "" {
|
||||
expLabel = r.PostForm.Get("expiry") // html form request
|
||||
}
|
||||
if expLabel == "" {
|
||||
expLabel = cfg.Expiries[0].Label
|
||||
}
|
||||
|
||||
selected, found := findExpiry(cfg.Expiries, expLabel)
|
||||
if !found {
|
||||
http.Error(w, "invalid expiry", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
id := generateId(idLength)
|
||||
path := getStoragePath(cfg.UploadDir, id)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user