neko/internal/api/room/upload.go

159 lines
3.1 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
"io"
2021-02-15 02:40:17 +13:00
"net/http"
"os"
2021-01-07 08:03:41 +13:00
"path"
"strconv"
2021-01-07 06:57:50 +13:00
"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-26 05:45:52 +13:00
err := r.ParseMultipartForm(MAX_UPLOAD_SIZE)
if err != nil {
2021-04-25 07:37:43 +12:00
utils.HttpBadRequest(w, "failed to parse multipart form")
2021-01-15 07:54:22 +13:00
return
}
2021-01-26 05:45:52 +13:00
//nolint
2021-01-15 07:54:22 +13:00
defer r.MultipartForm.RemoveAll()
2021-01-07 08:03:41 +13:00
X, err := strconv.Atoi(r.FormValue("x"))
if err != nil {
2021-04-25 07:37:43 +12:00
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 {
2021-04-25 07:37:43 +12:00
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 {
2021-04-25 07:37:43 +12:00
utils.HttpBadRequest(w, "no files received")
2021-01-07 08:03:41 +13:00
return
}
2021-08-30 03:12:37 +12:00
dir, err := os.MkdirTemp("", "neko-drop-*")
2021-01-07 08:03:41 +13:00
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()
2021-02-15 02:40:17 +13:00
dstFile, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
2021-01-07 08:03:41 +13:00
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) {
2021-04-25 07:37:43 +12:00
utils.HttpInternalServerError(w, "unable to drop files")
2021-01-15 07:54:22 +13:00
return
}
2021-01-07 06:57:50 +13:00
utils.HttpSuccess(w)
}
2021-01-18 11:50:03 +13:00
2021-01-18 22:34:33 +13:00
func (h *RoomHandler) uploadDialogPost(w http.ResponseWriter, r *http.Request) {
2021-01-26 05:45:52 +13:00
err := r.ParseMultipartForm(MAX_UPLOAD_SIZE)
if err != nil {
2021-04-25 07:37:43 +12:00
utils.HttpBadRequest(w, "failed to parse multipart form")
2021-01-18 11:50:03 +13:00
return
}
2021-01-26 05:45:52 +13:00
//nolint
2021-01-18 11:50:03 +13:00
defer r.MultipartForm.RemoveAll()
if !h.desktop.IsFileChooserDialogOpened() {
2021-04-25 07:37:43 +12:00
utils.HttpBadRequest(w, "open file chooser dialog first")
2021-01-18 11:50:03 +13:00
return
}
req_files := r.MultipartForm.File["files"]
if len(req_files) == 0 {
2021-04-25 07:37:43 +12:00
utils.HttpBadRequest(w, "no files received")
2021-01-18 11:50:03 +13:00
return
}
2021-08-30 03:12:37 +12:00
dir, err := os.MkdirTemp("", "neko-dialog-*")
2021-01-18 11:50:03 +13:00
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()
2021-02-15 02:40:17 +13:00
dstFile, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
2021-01-18 11:50:03 +13:00
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 {
2021-04-25 07:37:43 +12:00
utils.HttpInternalServerError(w, "unable to handle file chooser dialog")
2021-01-18 11:50:03 +13:00
return
}
utils.HttpSuccess(w)
}
2021-01-18 22:34:33 +13:00
func (h *RoomHandler) uploadDialogClose(w http.ResponseWriter, r *http.Request) {
if !h.desktop.IsFileChooserDialogOpened() {
2021-04-25 07:37:43 +12:00
utils.HttpBadRequest(w, "file chooser dialog is not open")
2021-01-18 22:34:33 +13:00
return
}
h.desktop.CloseFileChooserDialog()
2021-01-18 22:34:33 +13:00
utils.HttpSuccess(w)
}