-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
103 lines (82 loc) · 1.87 KB
/
Copy pathapi.go
File metadata and controls
103 lines (82 loc) · 1.87 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
package main
import (
"encoding/json"
"log"
"net/http"
)
type ResJson struct {
Status string `json:"status"`
Data any `json:"data"`
Error string `json:"error"`
}
func MakeJsonRes(w http.ResponseWriter, dataBody any) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Content-Type", "application/json")
resJson := &ResJson{
Status: "ok",
Data: dataBody,
}
resByte, err := json.Marshal(resJson)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(resByte)
}
func MakeErrRes(w http.ResponseWriter, errMess string) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Content-Type", "application/json")
resJson := &ResJson{
Status: "error",
Error: errMess,
}
resByte, err := json.Marshal(resJson)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusBadRequest)
w.Write(resByte)
}
func ListFilesHandle(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
if path == "" {
MakeErrRes(w, "path require")
return
}
files, err := ListFiles(path)
if err != nil {
MakeErrRes(w, err.Error())
return
}
MakeJsonRes(w, files)
}
func DeleteFileHandle(w http.ResponseWriter, r *http.Request) {
log.Println("On call delete")
path := r.URL.Query().Get("path")
if path == "" {
MakeErrRes(w, "path require")
return
}
err := DeleteFile(path)
if err != nil {
MakeErrRes(w, err.Error())
return
}
MakeJsonRes(w, nil)
}
func CreateDirHandle(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
if path == "" {
MakeErrRes(w, "path require")
return
}
err := CreateDir(path)
if err != nil {
MakeErrRes(w, err.Error())
return
}
MakeJsonRes(w, nil)
}