-
Notifications
You must be signed in to change notification settings - Fork 6
/
upload.go
172 lines (134 loc) · 4.25 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package room
import (
"io"
"net/http"
"os"
"path"
"strconv"
"github.com/demodesk/neko/pkg/utils"
)
// TODO: Extract file uploading to custom utility.
// maximum upload size of 32 MB
const maxUploadSize = 32 << 20
func (h *RoomHandler) uploadDrop(w http.ResponseWriter, r *http.Request) error {
if !h.desktop.IsUploadDropEnabled() {
return utils.HttpBadRequest("upload drop is disabled")
}
err := r.ParseMultipartForm(maxUploadSize)
if err != nil {
return utils.HttpBadRequest("failed to parse multipart form").WithInternalErr(err)
}
//nolint
defer r.MultipartForm.RemoveAll()
X, err := strconv.Atoi(r.FormValue("x"))
if err != nil {
return utils.HttpBadRequest("no X coordinate received").WithInternalErr(err)
}
Y, err := strconv.Atoi(r.FormValue("y"))
if err != nil {
return utils.HttpBadRequest("no Y coordinate received").WithInternalErr(err)
}
req_files := r.MultipartForm.File["files"]
if len(req_files) == 0 {
return utils.HttpBadRequest("no files received")
}
dir, err := os.MkdirTemp("", "neko-drop-*")
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to create temporary directory")
}
files := []string{}
for _, req_file := range req_files {
path := path.Join(dir, req_file.Filename)
srcFile, err := req_file.Open()
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to open uploaded file")
}
defer srcFile.Close()
dstFile, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to open destination file")
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to copy uploaded file to destination file")
}
files = append(files, path)
}
if !h.desktop.DropFiles(X, Y, files) {
return utils.HttpInternalServerError().
WithInternalMsg("unable to drop files")
}
return utils.HttpSuccess(w)
}
func (h *RoomHandler) uploadDialogPost(w http.ResponseWriter, r *http.Request) error {
if !h.desktop.IsFileChooserDialogEnabled() {
return utils.HttpBadRequest("file chooser dialog is disabled")
}
err := r.ParseMultipartForm(maxUploadSize)
if err != nil {
return utils.HttpBadRequest("failed to parse multipart form").WithInternalErr(err)
}
//nolint
defer r.MultipartForm.RemoveAll()
req_files := r.MultipartForm.File["files"]
if len(req_files) == 0 {
return utils.HttpBadRequest("no files received")
}
if !h.desktop.IsFileChooserDialogOpened() {
return utils.HttpUnprocessableEntity("file chooser dialog is not open")
}
dir, err := os.MkdirTemp("", "neko-dialog-*")
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to create temporary directory")
}
for _, req_file := range req_files {
path := path.Join(dir, req_file.Filename)
srcFile, err := req_file.Open()
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to open uploaded file")
}
defer srcFile.Close()
dstFile, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to open destination file")
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to copy uploaded file to destination file")
}
}
if err := h.desktop.HandleFileChooserDialog(dir); err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to handle file chooser dialog")
}
return utils.HttpSuccess(w)
}
func (h *RoomHandler) uploadDialogClose(w http.ResponseWriter, r *http.Request) error {
if !h.desktop.IsFileChooserDialogEnabled() {
return utils.HttpBadRequest("file chooser dialog is disabled")
}
if !h.desktop.IsFileChooserDialogOpened() {
return utils.HttpUnprocessableEntity("file chooser dialog is not open")
}
h.desktop.CloseFileChooserDialog()
return utils.HttpSuccess(w)
}