2021-01-06 18:57:50 +01:00
|
|
|
package room
|
|
|
|
|
|
|
|
import (
|
2021-01-06 20:03:41 +01:00
|
|
|
"io"
|
2021-02-14 14:40:17 +01:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-01-06 20:03:41 +01:00
|
|
|
"path"
|
|
|
|
"strconv"
|
2021-01-06 18:57:50 +01:00
|
|
|
|
|
|
|
"demodesk/neko/internal/utils"
|
|
|
|
)
|
|
|
|
|
2021-09-02 21:37:24 +02:00
|
|
|
// maximum upload size of 32 MB
|
|
|
|
const maxUploadSize = 32 << 20
|
2021-01-06 18:57:50 +01:00
|
|
|
|
2021-01-07 18:28:23 +01:00
|
|
|
func (h *RoomHandler) uploadDrop(w http.ResponseWriter, r *http.Request) {
|
2021-09-02 21:37:24 +02:00
|
|
|
err := r.ParseMultipartForm(maxUploadSize)
|
2021-01-25 17:45:52 +01:00
|
|
|
if err != nil {
|
2021-04-24 21:37:43 +02:00
|
|
|
utils.HttpBadRequest(w, "failed to parse multipart form")
|
2021-01-14 19:54:22 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-25 17:45:52 +01:00
|
|
|
//nolint
|
2021-01-14 19:54:22 +01:00
|
|
|
defer r.MultipartForm.RemoveAll()
|
|
|
|
|
2021-01-06 20:03:41 +01:00
|
|
|
X, err := strconv.Atoi(r.FormValue("x"))
|
|
|
|
if err != nil {
|
2021-04-24 21:37:43 +02:00
|
|
|
utils.HttpBadRequest(w, "no X coordinate received")
|
2021-01-06 20:03:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Y, err := strconv.Atoi(r.FormValue("y"))
|
|
|
|
if err != nil {
|
2021-04-24 21:37:43 +02:00
|
|
|
utils.HttpBadRequest(w, "no Y coordinate received")
|
2021-01-09 16:24:05 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-06 20:03:41 +01:00
|
|
|
req_files := r.MultipartForm.File["files"]
|
|
|
|
if len(req_files) == 0 {
|
2021-04-24 21:37:43 +02:00
|
|
|
utils.HttpBadRequest(w, "no files received")
|
2021-01-06 20:03:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:12:37 +02:00
|
|
|
dir, err := os.MkdirTemp("", "neko-drop-*")
|
2021-01-06 20:03:41 +01:00
|
|
|
if err != nil {
|
|
|
|
utils.HttpInternalServerError(w, err)
|
2021-01-06 18:57:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-06 20:03:41 +01: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-14 14:40:17 +01:00
|
|
|
dstFile, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
2021-01-06 20:03:41 +01: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-14 19:54:22 +01:00
|
|
|
if !h.desktop.DropFiles(X, Y, files) {
|
2021-04-24 21:37:43 +02:00
|
|
|
utils.HttpInternalServerError(w, "unable to drop files")
|
2021-01-14 19:54:22 +01:00
|
|
|
return
|
|
|
|
}
|
2021-01-09 16:24:05 +01:00
|
|
|
|
2021-01-06 18:57:50 +01:00
|
|
|
utils.HttpSuccess(w)
|
|
|
|
}
|
2021-01-17 23:50:03 +01:00
|
|
|
|
2021-01-18 10:34:33 +01:00
|
|
|
func (h *RoomHandler) uploadDialogPost(w http.ResponseWriter, r *http.Request) {
|
2021-09-02 21:37:24 +02:00
|
|
|
err := r.ParseMultipartForm(maxUploadSize)
|
2021-01-25 17:45:52 +01:00
|
|
|
if err != nil {
|
2021-04-24 21:37:43 +02:00
|
|
|
utils.HttpBadRequest(w, "failed to parse multipart form")
|
2021-01-17 23:50:03 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-25 17:45:52 +01:00
|
|
|
//nolint
|
2021-01-17 23:50:03 +01:00
|
|
|
defer r.MultipartForm.RemoveAll()
|
|
|
|
|
2021-01-19 21:01:31 +01:00
|
|
|
if !h.desktop.IsFileChooserDialogOpened() {
|
2021-04-24 21:37:43 +02:00
|
|
|
utils.HttpBadRequest(w, "open file chooser dialog first")
|
2021-01-17 23:50:03 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req_files := r.MultipartForm.File["files"]
|
|
|
|
if len(req_files) == 0 {
|
2021-04-24 21:37:43 +02:00
|
|
|
utils.HttpBadRequest(w, "no files received")
|
2021-01-17 23:50:03 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-29 17:12:37 +02:00
|
|
|
dir, err := os.MkdirTemp("", "neko-dialog-*")
|
2021-01-17 23:50:03 +01: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-14 14:40:17 +01:00
|
|
|
dstFile, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
2021-01-17 23:50:03 +01: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-24 21:37:43 +02:00
|
|
|
utils.HttpInternalServerError(w, "unable to handle file chooser dialog")
|
2021-01-17 23:50:03 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.HttpSuccess(w)
|
|
|
|
}
|
2021-01-18 10:34:33 +01:00
|
|
|
|
|
|
|
func (h *RoomHandler) uploadDialogClose(w http.ResponseWriter, r *http.Request) {
|
2021-01-19 21:01:31 +01:00
|
|
|
if !h.desktop.IsFileChooserDialogOpened() {
|
2021-04-24 21:37:43 +02:00
|
|
|
utils.HttpBadRequest(w, "file chooser dialog is not open")
|
2021-01-18 10:34:33 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-19 21:01:31 +01:00
|
|
|
h.desktop.CloseFileChooserDialog()
|
2021-01-18 10:34:33 +01:00
|
|
|
utils.HttpSuccess(w)
|
|
|
|
}
|