neko/internal/api/room/upload.go

151 lines
2.9 KiB
Go
Raw Normal View History

2021-01-07 06:57:50 +13:00
package room
import (
2021-01-07 08:03:41 +13:00
"os"
"io"
"io/ioutil"
"path"
"strconv"
2021-01-07 06:57:50 +13:00
"net/http"
"demodesk/neko/internal/utils"
)
2021-01-07 08:03:41 +13:00
const (
// Maximum upload of 32 MB files.
MAX_UPLOAD_SIZE = 32 << 20
)
2021-01-07 06:57:50 +13:00
2021-01-08 06:28:23 +13:00
func (h *RoomHandler) uploadDrop(w http.ResponseWriter, r *http.Request) {
2021-01-07 08:03:41 +13:00
r.ParseMultipartForm(MAX_UPLOAD_SIZE)
2021-01-15 07:54:22 +13:00
if r.MultipartForm == nil {
utils.HttpBadRequest(w, "No MultipartForm received.")
return
}
defer r.MultipartForm.RemoveAll()
2021-01-07 08:03:41 +13:00
X, err := strconv.Atoi(r.FormValue("x"))
if err != nil {
utils.HttpBadRequest(w, "No X coordinate received.")
2021-01-07 08:03:41 +13:00
return
}
Y, err := strconv.Atoi(r.FormValue("y"))
if err != nil {
utils.HttpBadRequest(w, "No Y coordinate received.")
return
}
2021-01-07 08:03:41 +13:00
req_files := r.MultipartForm.File["files"]
if len(req_files) == 0 {
utils.HttpBadRequest(w, "No files received.")
2021-01-07 08:03:41 +13:00
return
}
dir, err := ioutil.TempDir("", "neko-drop-*")
if err != nil {
utils.HttpInternalServerError(w, err)
2021-01-07 06:57:50 +13:00
return
}
2021-01-07 08:03:41 +13:00
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)
}
2021-01-15 07:54:22 +13:00
if !h.desktop.DropFiles(X, Y, files) {
utils.HttpInternalServerError(w, "Unable to drop files.")
return
}
2021-01-07 06:57:50 +13:00
utils.HttpSuccess(w)
}
2021-01-18 11:50:03 +13:00
func (h *RoomHandler) uploadDialog(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(MAX_UPLOAD_SIZE)
if r.MultipartForm == nil {
utils.HttpBadRequest(w, "No MultipartForm received.")
return
}
defer r.MultipartForm.RemoveAll()
if !h.desktop.IsFileChooserDialogOpen() {
utils.HttpBadRequest(w, "Open file chooser dialog first.")
return
}
req_files := r.MultipartForm.File["files"]
if len(req_files) == 0 {
utils.HttpBadRequest(w, "No files received.")
return
}
dir, err := ioutil.TempDir("", "neko-dialog-*")
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
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
}
}
if err := h.desktop.HandleFileChooserDialog(dir); err != nil {
utils.HttpInternalServerError(w, "Unable to handle file chooser dialog.")
return
}
utils.HttpSuccess(w)
}