-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhandler.go
More file actions
376 lines (310 loc) · 11.8 KB
/
handler.go
File metadata and controls
376 lines (310 loc) · 11.8 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"html/template"
"net/http"
"path/filepath"
"strings"
"time"
"fmt"
"os"
"encoding/json"
)
func RegisterRouter() {
http.HandleFunc("/login/", LoginHandler)
http.HandleFunc("/", IndexHandler)
http.HandleFunc("/UI/", UIHandler)
http.HandleFunc("/docs/", APIDocsHandler)
UpdateShareHandler()
http.Handle("/api/share/", &DynamicHandler{})
http.HandleFunc("/api/file-list/", FileInfoHandler)
http.HandleFunc("/api/file-property/", ViewFilePropertyHandler)
http.HandleFunc("/api/edit-announcement/", AuthorizeStatusMiddleware(&ANNOUNCEMENT_STATUS)(EditAnnouncementBoardHandler))
http.HandleFunc("/api/announcement-content/", AuthorizeStatusMiddleware(&ANNOUNCEMENT_STATUS)(PublishAnnouncementBoardHandler))
http.HandleFunc("/api/upload-file/", AuthorizeStatusMiddleware(&UPLOAD_STATUS)(UploadFileHandler))
http.HandleFunc("/api/search-file/", AuthorizeStatusMiddleware(&SEARCH_STATUS)(SearchFileHandler))
http.HandleFunc("/api/delete-file/", AuthorizeStatusMiddleware(&DELETE_STATUS)(DeleteFileHandler))
http.HandleFunc("/api/batch-download/", BatchDownloadHandler)
http.HandleFunc("/api/make-directory/", AuthorizeStatusMiddleware(&MKDIR_STATUS)(MakeDirectoryHandler))
http.HandleFunc("/api/copy-file/", AuthorizeStatusMiddleware(©_STATUS)(CopyFileHandler))
http.HandleFunc("/api/move-file/", AuthorizeStatusMiddleware(&MOVE_STATUS)(MoveFileHandler))
http.HandleFunc("/api/rename-file/", AuthorizeStatusMiddleware(&RENAME_STATUS)(RenameFileHandler))
}
func LoginHandler(w http.ResponseWriter, r *http.Request) {
// 如果是 GET 请求, 直接返回 login.html 页面.
if r.Method == http.MethodGet {
data, err := ui.ReadFile("UI/login.html")
if err != nil {
http.Error(w, "[* HTTP 500]: fail to load \"login.html\" webpage.", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.Write(data)
return
}
// 必须通过 POST 请求登录前端页面.
if r.Method != http.MethodPost {
http.Error(w, "[* HTTP 405]: only POST request is allowed.", http.StatusMethodNotAllowed)
return
}
// 解析前端输入框密码表单.
err := r.ParseForm()
if err != nil {
http.Error(w, "[* HTTP 400]: invalid request body.", http.StatusBadRequest)
return
}
// 获取用户登录密码.
password := r.FormValue("password")
/* 获取用户记录, 采用 ip + user-agent 格式.
比如用户用 Edge 浏览器登录, 再用 Chrome 浏览器登录, 这属于两个 user, 每次都需要登录. */
user := strings.Split(r.RemoteAddr, ":")[0] + "+" + r.Header.Get("User-Agent")
// 如果用户登录密码等于文件传输服务登录密码, 则取消该用户登录锁.
if password == LOGIN_PASSWORD {
USER_LOCK[user] = false
go func(user string) {
// 启动该用户的登录锁协程, 半小时后再次给该 user 设置锁, 需要重新登录.
time.Sleep(30 * time.Minute)
delete(USER_LOCK, user)
}(user)
// 登录成功, 重定向到 index.html 页面.
http.Redirect(w, r, "/", http.StatusMovedPermanently)
return
} else {
// 如果 user 登录密码不正确, 前端打印错误信息.
http.Error(w, "[* HTTP 401]: wrong password.", http.StatusUnauthorized)
return
}
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
// 判断用户是否有登录锁, 若有锁, 则重定向到 login.html 页面.
user := strings.Split(r.RemoteAddr, ":")[0] + "+" + r.Header.Get("User-Agent")
if AuthorizeUser(user) {
http.Redirect(w, r, "/login/", http.StatusMovedPermanently)
return
}
if r.URL.Path != "/" {
// 如果路由不存在, 返回 HTTP 404.
http.Error(w, "[* HTTP 404]: not found.", http.StatusNotFound)
return
}
// 渲染 HTML 模板, 渲染参数 utils.PARAMS.
data, err := template.ParseFS(ui, "UI/index.html")
if err != nil {
http.Error(w, "[* HTTP 500]: fail to load \"index.html\" webpage.", http.StatusInternalServerError)
return
}
data.Execute(w, SERVER_PARAMS{
ANNOUNCEMENT_STATUS_: ANNOUNCEMENT_STATUS,
UPLOAD_STATUS_: UPLOAD_STATUS,
SEARCH_STATUS_: SEARCH_STATUS,
DELETE_STATUS_: DELETE_STATUS,
MKDIR_STATUS_: MKDIR_STATUS,
COPY_STATUS_: COPY_STATUS,
MOVE_STATUS_: MOVE_STATUS,
RENAME_STATUS_: RENAME_STATUS,
})
}
func UIHandler(w http.ResponseWriter, r *http.Request) {
// 判断用户是否有登录锁, 若有锁, 则重定向到 login.html 页面.
user := strings.Split(r.RemoteAddr, ":")[0] + "+" + r.Header.Get("User-Agent")
if AuthorizeUser(user) {
http.Redirect(w, r, "/login/", http.StatusMovedPermanently)
return
}
// 加载 index.html 所有的静态资源.
data, err := ui.ReadFile(r.URL.Path[1:])
if err != nil {
http.Error(w, "[* HTTP 404]: fail to find file \""+r.URL.Path[1:]+"\"", http.StatusNotFound)
return
}
switch filepath.Ext(r.URL.Path) {
case ".css":
w.Header().Set("Content-Type", "text/css")
case ".js":
w.Header().Set("Content-Type", "application/javascript")
case ".svg":
w.Header().Set("Content-Type", "image/svg+xml")
default:
w.Header().Set("Content-Type", "text/plain")
}
w.Write(data)
}
func APIDocsHandler(w http.ResponseWriter, r *http.Request) {
user := strings.Split(r.RemoteAddr, ":")[0] + "+" + r.Header.Get("User-Agent")
if AuthorizeUser(user) {
http.Redirect(w, r, "/login/", http.StatusMovedPermanently)
return
}
data, err := ui.ReadFile("UI/docs.html")
if err != nil {
http.Error(w, "[* HTTP 500]: fail to load \"docs.html\" webpage.", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.Write(data)
}
func FileInfoHandler(w http.ResponseWriter, r *http.Request) {
// 判断用户是否有登录锁, 若有锁, 则重定向到 login.html 页面.
user := strings.Split(r.RemoteAddr, ":")[0] + "+" + r.Header.Get("User-Agent")
if AuthorizeUser(user) {
http.Redirect(w, r, "/login/", http.StatusMovedPermanently)
return
}
var file_list []FILE_INFO
path := r.URL.Query().Get("path")
if path != "." {
file_list = append(file_list, FILE_INFO{
FileName: ". .",
FileSize: "",
FileIcon: "NULL",
ModifiedTime: "",
})
}
if !AccessSuperPath(path) {
http.Error(w, "[* HTTP 400]: forbid accessing parent directory of the shared folder.", http.StatusBadRequest)
return
}
file_list = append(file_list, GetFileInfo(filepath.Join(SHARE_DIR, path))...)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(file_list)
}
// 处理当前目录文件列表选中删除的文件.
func ViewFilePropertyHandler(w http.ResponseWriter, r *http.Request) {
var selected_view_file_property []FILE_REQUEST
if err := json.NewDecoder(r.Body).Decode(&selected_view_file_property); err != nil {
http.Error(w, "[* HTTP 400]: invalid request body.", http.StatusBadRequest)
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(CalculateProperty(selected_view_file_property))
}
// 获取公告栏编辑的数据来修改 golang 内存变量.
func EditAnnouncementBoardHandler(w http.ResponseWriter, r *http.Request) {
var contenteditable CONTENTEDITABLE_REQUEST
if err := json.NewDecoder(r.Body).Decode(&contenteditable); err != nil {
http.Error(w, "[* HTTP 400]: invalid request body.", http.StatusBadRequest)
return
}
CONTENTEDITABLE = contenteditable.Content
}
// 发送内存变量数据到前端公告栏.
func PublishAnnouncementBoardHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
response := map[string]string{"Content": CONTENTEDITABLE}
json.NewEncoder(w).Encode(response)
}
// 处理文件上传.
func UploadFileHandler(w http.ResponseWriter, r *http.Request) {
content_length := r.ContentLength
if content_length > ParseStorageUnit(MAX_SIZE) {
http.Error(w, "[* HTTP 413]: request is too large.", http.StatusRequestEntityTooLarge)
return
}
if err := r.ParseMultipartForm(ParseStorageUnit(MAX_SIZE)); err != nil {
http.Error(w, "[* HTTP 500]: fail to parse form.", http.StatusInternalServerError)
return
}
upload_files := r.MultipartForm.File["File"]
upload_file_relative_paths := r.MultipartForm.Value["RelativePath"]
current_dirs := r.MultipartForm.Value["CurrentDir"]
SaveUploadFile(upload_files, upload_file_relative_paths, current_dirs)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(nil)
}
func SearchFileHandler(w http.ResponseWriter, r *http.Request) {
var selected_file FILE_SEARCH
if err := json.NewDecoder(r.Body).Decode(&selected_file); err != nil {
http.Error(w, "[* HTTP 400]: invalid request body.", http.StatusBadRequest)
return
}
ans := SearchFile(selected_file)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(ans)
}
func DeleteFileHandler(w http.ResponseWriter, r *http.Request) {
var selected_file []FILE_REQUEST
if err := json.NewDecoder(r.Body).Decode(&selected_file); err != nil {
http.Error(w, "[* HTTP 400]: invalid request body.", http.StatusBadRequest)
return
}
DeleteSelectedFile(selected_file)
}
// 批量下载功能.
func BatchDownloadHandler(w http.ResponseWriter, r *http.Request) {
user := strings.Split(r.RemoteAddr, ":")[0] + "+" + r.Header.Get("User-Agent")
if AuthorizeUser(user) {
http.Redirect(w, r, "/login/", http.StatusMovedPermanently)
return
}
if r.Method != http.MethodPost {
http.Error(w, "[* HTTP 405]: only POST request is allowed.", http.StatusMethodNotAllowed)
return
}
var selected_file []FILE_REQUEST
if err := json.NewDecoder(r.Body).Decode(&selected_file); err != nil {
http.Error(w, "[* HTTP 400]: invalid request body.", http.StatusBadRequest)
return
}
buffer := ArchiveZip(selected_file)
if buffer == nil {
http.Error(w, "[* HTTP 500]: fail to archive the compressed zip file.", http.StatusInternalServerError)
return
} else {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", "attachment; filename=archive.zip")
w.Header().Set("Content-Length", fmt.Sprintf("%d", buffer.Len()))
if _, err := buffer.WriteTo(w); err != nil {
http.Error(w, "[* HTTP 500]: fail to archive the compressed zip file.", http.StatusInternalServerError)
return
}
}
}
// 创建递归文件夹.
func MakeDirectoryHandler(w http.ResponseWriter, r *http.Request) {
var makedir FILE_REQUEST
if err := json.NewDecoder(r.Body).Decode(&makedir); err != nil {
http.Error(w, "[* HTTP 400]: invalid request body.", http.StatusBadRequest)
return
}
err := os.MkdirAll(filepath.Join(SHARE_DIR, makedir.CurrentDir, makedir.Path), os.ModePerm)
if err != nil {
http.Error(w, "[* HTTP 500]: fail to create directory.", http.StatusInternalServerError)
return
} else {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(nil)
}
}
func CopyFileHandler(w http.ResponseWriter, r *http.Request) {
var selected_file []FILE_REQUEST
if err := json.NewDecoder(r.Body).Decode(&selected_file); err != nil {
http.Error(w, "[* HTTP 400]: invalid request body.", http.StatusBadRequest)
return
}
CopySelectedFile(selected_file)
}
// 移动文件.
func MoveFileHandler(w http.ResponseWriter, r *http.Request) {
var selected_file []FILE_REQUEST
if err := json.NewDecoder(r.Body).Decode(&selected_file); err != nil {
http.Error(w, "[* HTTP 400]: invalid request body.", http.StatusBadRequest)
return
}
MoveSelectedFile(selected_file)
}
// 重命名文件.
func RenameFileHandler(w http.ResponseWriter, r *http.Request) {
var selected_file []FILE_RENAME
if err := json.NewDecoder(r.Body).Decode(&selected_file); err != nil {
http.Error(w, "[* HTTP 400]: invalid request body.", http.StatusBadRequest)
return
}
RenameSelectedFile(selected_file)
}