-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
145 lines (136 loc) · 3.95 KB
/
endpoints.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
package main
import (
"bytes"
"fmt"
"io"
"mime"
"net/http"
"os"
"path/filepath"
"strings"
)
const (
maxInputLen = 32 << 20
formFileKey = "file"
formPathKey = "path"
headerKeyContentType = "Content-Type"
)
type ErrorResponse struct {
Message string
Path string
}
func newErrorResponse(msg, path string) ErrorResponse {
return ErrorResponse{Message: msg, Path: path}
}
func getFile(svc FilevaultService) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
return
}
path := r.FormValue(formPathKey)
data, err := svc.ReadFile(path)
if err != nil {
httperr := newErrorResponse(err.Error(), path)
encode(w, r, http.StatusBadRequest, httperr)
return
}
filename := filepath.Base(path)
ext := filepath.Ext(filename)
contentType := mime.TypeByExtension(ext)
w.Header().Set(headerKeyContentType, contentType)
w.WriteHeader(http.StatusOK)
_, err = io.Copy(w, bytes.NewReader(data))
if err != nil {
httperr := newErrorResponse(err.Error(), path)
encode(w, r, http.StatusInternalServerError, httperr)
return
}
})
}
func removeFile(svc FilevaultService) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
httperr := newErrorResponse(err.Error(), "")
encode(w, r, http.StatusBadRequest, httperr)
return
}
path := r.FormValue(formPathKey)
if err := svc.RemoveFile(path); err != nil {
httperr := newErrorResponse(err.Error(), path)
encode(w, r, http.StatusBadRequest, httperr)
return
}
w.WriteHeader(http.StatusNoContent)
})
}
func uploadFile(svc FilevaultService) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type resp struct {
Dir string
Filename string
Path string
}
if err := r.ParseMultipartForm(maxInputLen); err != nil {
httperr := newErrorResponse(err.Error(), "")
encode(w, r, http.StatusBadRequest, httperr)
return
}
file, header, err := r.FormFile(formFileKey)
if err != nil {
msg := fmt.Sprintf("missing form key '%s' for file", formFileKey)
httperr := newErrorResponse(msg, "")
encode(w, r, http.StatusBadRequest, httperr)
return
}
defer file.Close()
_, _, ok := strings.Cut(header.Filename, ".")
if !ok {
httperr := newErrorResponse("filename has to be in format of <name>.<ext>. For examle filevault.json", "")
encode(w, r, http.StatusBadRequest, httperr)
return
}
dir := r.FormValue("dir")
path := filepath.Join(dir, header.Filename)
if err := svc.CreateFile(path, file); err != nil {
httperr := newErrorResponse(err.Error(), "")
encode(w, r, http.StatusBadRequest, httperr)
return
}
res := resp{
Dir: filepath.Dir(path),
Filename: header.Filename,
Path: path,
}
encode(w, r, http.StatusCreated, res)
})
}
func health(cfg Config) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
const testDir = "test"
const testFile = "test.md"
if ok, err := isDirExisting(cfg.Dir); err != nil || !ok {
httperr := newErrorResponse("directory does not exist", cfg.Dir)
encode(w, r, http.StatusBadRequest, httperr)
return
}
path := filepath.Join(cfg.Dir, testDir)
mode := os.FileMode(0755)
if err := os.MkdirAll(path, mode); err != nil {
httperr := newErrorResponse("cannot create a directory", path)
encode(w, r, http.StatusBadRequest, httperr)
return
}
pathFile := filepath.Join(path, testFile)
if _, err := os.Create(pathFile); err != nil {
httperr := newErrorResponse("cannot create a file", path)
encode(w, r, http.StatusBadRequest, httperr)
return
}
if err := os.RemoveAll(path); err != nil {
httperr := newErrorResponse("something went wrong while cleaning up", path)
encode(w, r, http.StatusInternalServerError, httperr)
return
}
w.WriteHeader(http.StatusOK)
})
}