neko/internal/api/room/upload.go
2021-01-09 16:24:05 +01:00

85 lines
1.5 KiB
Go

package room
import (
"os"
"io"
"io/ioutil"
"path"
"strconv"
"net/http"
"demodesk/neko/internal/utils"
)
const (
// Maximum upload of 32 MB files.
MAX_UPLOAD_SIZE = 32 << 20
)
func (h *RoomHandler) uploadDrop(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(MAX_UPLOAD_SIZE)
X, err := strconv.Atoi(r.FormValue("x"))
if err != nil {
utils.HttpBadRequest(w, "No X coordinate received.")
return
}
Y, err := strconv.Atoi(r.FormValue("y"))
if err != nil {
utils.HttpBadRequest(w, "No Y coordinate received.")
return
}
if r.MultipartForm == nil {
utils.HttpBadRequest(w, "No MultipartForm received.")
return
}
req_files := r.MultipartForm.File["files"]
if len(req_files) == 0 {
utils.HttpBadRequest(w, "No files received.")
return
}
dir, err := ioutil.TempDir("", "neko-drop-*")
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
files := []string{}
for _, req_file := range req_files {
path := path.Join(dir, req_file.Filename)
srcFile, err := req_file.Open()
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
defer srcFile.Close()
dstFile, err := os.OpenFile(path, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
files = append(files, path)
}
h.desktop.DropFiles(X, Y, files)
r.MultipartForm.RemoveAll()
utils.HttpSuccess(w)
}