-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_server.go
138 lines (118 loc) · 2.96 KB
/
file_server.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
package main
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"sort"
"strings"
)
func JsonFileServer(root http.FileSystem, pathPrefix string) http.Handler {
return &jsonFileHandler{root, pathPrefix}
}
type jsonFileHandler struct {
root http.FileSystem
pathPrefix string
}
type fileContentResponse struct {
Content string `json:"content"`
}
type dirContentResponse struct {
Dir string `json:"dir"`
Files []string `json:"files"`
}
func (f jsonFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
upath := r.URL.Path
if !strings.HasPrefix(upath, "/") {
upath = "/" + upath
}
if !strings.HasPrefix(upath, f.pathPrefix) {
msg, code := toHTTPError(os.ErrNotExist)
http.Error(w, msg, code)
return
}
upath = upath[len(f.pathPrefix):] // TODO ?
log.Printf("server file %s", upath)
serveFile(w, r, f.root, path.Clean(upath))
}
func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string) {
f, err := fs.Open(name)
if err != nil {
msg, code := toHTTPError(err)
http.Error(w, msg, code)
return
}
defer f.Close()
d, err := f.Stat()
if err != nil {
msg, code := toHTTPError(err)
http.Error(w, msg, code)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
// Still a directory? (we didn't find an index.html file)
if d.IsDir() {
serverDirContent(w, r, f)
return
}
serveFileContent(w, r, d.Name(), f)
}
func serverDirContent(w http.ResponseWriter, r *http.Request, f http.File) {
dirs, err := f.Readdir(-1)
if err != nil {
http.Error(w, "Error reading directory", http.StatusInternalServerError)
return
}
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
files := make([]string, 0)
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
if len(name) <= 0 {
continue
}
files = append(files, name)
}
curDir := f.(*os.File)
resp := dirContentResponse { curDir.Name(), files }
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
func serveFileContent(w http.ResponseWriter, r *http.Request, name string, content io.ReadSeeker) {
if !fileTypeSupport(name) {
http.Error(w, "File type not support", http.StatusBadRequest)
return
}
contentStr, err := ioutil.ReadAll(content)
if err != nil {
http.Error(w, "File content err", http.StatusBadRequest)
return
}
resp := fileContentResponse { Content: string(contentStr) }
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
func fileTypeSupport(name string) bool {
if strings.HasSuffix(name, ".yaml") || strings.HasSuffix(name, ".yml") {
return true
}
if strings.HasSuffix(name, ".json") {
return true
}
return false
}
func toHTTPError(err error) (msg string, httpStatus int) {
if os.IsNotExist(err) {
return "404 page not found", http.StatusNotFound
}
if os.IsPermission(err) {
return "403 Forbidden", http.StatusForbidden
}
// Default:
return "500 Internal Server Error", http.StatusInternalServerError
}