get cp paste file upload gogo via multipart form
This commit is contained in:
@@ -3,9 +3,12 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"local/gziphttp"
|
||||
"local/router"
|
||||
"local/simpleserve/simpleserve"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
@@ -59,6 +62,11 @@ func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (server *Server) tryCatchHttpHandler(handler func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if gziphttp.Can(r) {
|
||||
w2 := gziphttp.New(w)
|
||||
defer w2.Close()
|
||||
w = w2
|
||||
}
|
||||
if err := handler(w, r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
@@ -66,6 +74,23 @@ func (server *Server) tryCatchHttpHandler(handler func(http.ResponseWriter, *htt
|
||||
}
|
||||
|
||||
func (server *Server) apiV0TreeHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
_, pretty := r.URL.Query()["pretty"]
|
||||
if pretty {
|
||||
return server.apiV0TreePrettyHandler(w, r)
|
||||
}
|
||||
return server.apiV0TreePlainHandler(w, r)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0TreePrettyHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
tree := server.tree()
|
||||
branches, err := tree.GetPretty()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(w).Encode(branches)
|
||||
}
|
||||
|
||||
func (server *Server) apiV0TreePlainHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
tree := server.tree()
|
||||
branches, err := tree.Get()
|
||||
if err != nil {
|
||||
@@ -82,9 +107,8 @@ func ensureAndWrite(p string, b []byte) error {
|
||||
}
|
||||
|
||||
func (server *Server) apiV0MediaHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
id := uuid.New().String()
|
||||
filePath := server.diskMediaPath(id)
|
||||
if err := server.postContentHandler(filePath, w, r); err != nil {
|
||||
id, err := server.postContentHandler(path.Dir(server.diskMediaPath("id")), w, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(w).Encode(map[string]map[string]string{
|
||||
@@ -129,15 +153,49 @@ func (server *Server) getContentHandler(filePath string, w http.ResponseWriter,
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
simpleserve.SetContentTypeIfMedia(w, r)
|
||||
io.Copy(w, f)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (server *Server) postContentHandler(filePath string, w http.ResponseWriter, r *http.Request) error {
|
||||
func (server *Server) postContentHandler(fileDir string, w http.ResponseWriter, r *http.Request) (string, error) {
|
||||
if r.Method != http.MethodPost {
|
||||
return errors.New("not found")
|
||||
return "", errors.New("not found")
|
||||
}
|
||||
return server.putContentHandler(filePath, w, r)
|
||||
id := uuid.New().String()
|
||||
if strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data") {
|
||||
kb := int64(1 << 10)
|
||||
mb := kb << 10
|
||||
if err := r.ParseMultipartForm(10 * mb); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(r.MultipartForm.File) != 1 {
|
||||
return "", errors.New("not exactly 1 file found in request")
|
||||
}
|
||||
for _, infos := range r.MultipartForm.File {
|
||||
if len(infos) != 1 {
|
||||
return "", errors.New("not exactly 1 file info found in request")
|
||||
}
|
||||
ext := path.Ext(infos[0].Filename)
|
||||
if h, ok := infos[0].Header["Content-Type"]; ok {
|
||||
ext = path.Base(h[0])
|
||||
}
|
||||
id += "." + ext
|
||||
f, err := infos[0].Open()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
r.Body = f
|
||||
}
|
||||
} else if strings.HasPrefix(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", fmt.Errorf("parse form: %+v", r.PostForm)
|
||||
}
|
||||
filePath := path.Join(fileDir, id)
|
||||
return id, server.putContentHandler(filePath, w, r)
|
||||
}
|
||||
|
||||
func (server *Server) putContentHandler(filePath string, w http.ResponseWriter, r *http.Request) error {
|
||||
@@ -166,9 +224,8 @@ func (server *Server) diskMediaPath(id string) string {
|
||||
}
|
||||
|
||||
func (server *Server) apiV0FilesHandler(w http.ResponseWriter, r *http.Request) error {
|
||||
id := uuid.New().String()
|
||||
filePath := server.diskFilePath(id)
|
||||
if err := server.postContentHandler(filePath, w, r); err != nil {
|
||||
id, err := server.postContentHandler(path.Dir(server.diskFilePath("id")), w, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tree := server.tree()
|
||||
|
||||
Reference in New Issue
Block a user