server: handle multiple uploads
This commit is contained in:
parent
dca72442d9
commit
f6b9fd2536
13
Server.go
13
Server.go
@ -138,18 +138,7 @@ func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
this.handleAbout(w)
|
this.handleAbout(w)
|
||||||
|
|
||||||
} else if r.Method == "POST" && r.URL.Path == `/upload` {
|
} else if r.Method == "POST" && r.URL.Path == `/upload` {
|
||||||
mp, mph, err := r.FormFile("f")
|
this.handleUpload(w, r)
|
||||||
if err != nil {
|
|
||||||
log.Printf("%s Invalid request: %s\n", r.RemoteAddr, err.Error())
|
|
||||||
http.Error(w, "Invalid request", 400)
|
|
||||||
}
|
|
||||||
path, err := this.handleUpload(mp, mph, strings.TrimRight(r.RemoteAddr, "0123456789:")) // strip remote-port
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("%s Upload failed: %s\n", r.RemoteAddr, err.Error())
|
|
||||||
http.Error(w, "Upload failed", 500)
|
|
||||||
} else {
|
|
||||||
http.Redirect(w, r, `/view/`+path, http.StatusFound)
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// TODO embed into binary
|
// TODO embed into binary
|
||||||
|
53
upload.go
53
upload.go
@ -3,9 +3,12 @@ package contented
|
|||||||
import (
|
import (
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"mime"
|
"mime"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -13,7 +16,55 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (this *Server) handleUpload(src multipart.File, hdr *multipart.FileHeader, UploadIP string) (string, error) {
|
func (this *Server) handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
err := r.ParseMultipartForm(this.opts.MaxUploadBytes * 2)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%s Invalid request: %s\n", r.RemoteAddr, err.Error())
|
||||||
|
http.Error(w, "Invalid request", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.MultipartForm == nil || r.MultipartForm.File == nil || len(r.MultipartForm.File["f"]) < 1 {
|
||||||
|
log.Printf("%s Invalid request: no multipart content\n", r.RemoteAddr)
|
||||||
|
http.Error(w, "Invalid request", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteIP := strings.TrimRight(r.RemoteAddr, "0123456789:")
|
||||||
|
|
||||||
|
ret := make([]string, 0, len(r.MultipartForm.File["f"]))
|
||||||
|
|
||||||
|
for _, fhs := range r.MultipartForm.File["f"] {
|
||||||
|
f, err := fhs.Open()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%s Internal error: %s\n", r.RemoteAddr, err.Error())
|
||||||
|
http.Error(w, "Internal error", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
path, err := this.handleUploadFile(f, fhs, remoteIP)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%s Upload failed: %s\n", r.RemoteAddr, err.Error())
|
||||||
|
http.Error(w, "Upload failed", 500)
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = append(ret, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
jb, err := json.Marshal(ret)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("%s Internal error: %s\n", r.RemoteAddr, err.Error())
|
||||||
|
http.Error(w, "Internal error", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set(`Content-Type`, `application/json`)
|
||||||
|
w.WriteHeader(200)
|
||||||
|
w.Write(jb)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Server) handleUploadFile(src multipart.File, hdr *multipart.FileHeader, UploadIP string) (string, error) {
|
||||||
|
|
||||||
// Get file length
|
// Get file length
|
||||||
srcLen, err := src.Seek(0, io.SeekEnd)
|
srcLen, err := src.Seek(0, io.SeekEnd)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user