-
Notifications
You must be signed in to change notification settings - Fork 1
/
browse.go
190 lines (171 loc) · 4.67 KB
/
browse.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package fancyindex
import (
_ "embed"
"errors"
"html/template"
"io"
"io/fs"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strings"
"github.com/sirupsen/logrus"
)
//go:embed index.html
var indexTemplateSource string
type FileServer struct {
relativePath string
fileSystem fs.FS
indexTemplate *template.Template
}
func (f *FileServer) serveDir(dir fs.File, s fs.FileInfo, w http.ResponseWriter, r *http.Request) {
d, ok := dir.(fs.ReadDirFile)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("file does not readdir"))
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
listing, err := f.loadDirectoryContents(d, s.Name(), path.Clean(r.URL.Path))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("could not load directory contents"))
return
}
f.browseApplyQueryParams(w, r, &listing)
err = f.indexTemplate.Execute(w, listing)
if err != nil {
panic(err)
}
}
func (f *FileServer) loadDirectoryContents(dir fs.ReadDirFile, root, urlPath string) (browseTemplateContext, error) {
direntries, err := dir.ReadDir(-1)
if err != nil {
return browseTemplateContext{}, err
}
sort.Slice(direntries, func(i, j int) bool {
return direntries[i].Name() < direntries[j].Name()
})
var files []os.FileInfo
for _, direntry := range direntries {
info, err := direntry.Info()
if err != nil {
logrus.Errorf("error reading info for %s: %s", direntry.Name(), err)
continue
}
files = append(files, info)
}
// user can presumably browse "up" to parent folder if path is longer than "/"
canGoUp := len(urlPath) > 1
return f.directoryListing(files, canGoUp, root, urlPath), nil
}
func (f *FileServer) browseApplyQueryParams(w http.ResponseWriter, r *http.Request, listing *browseTemplateContext) {
sortParam := r.URL.Query().Get("sort")
orderParam := r.URL.Query().Get("order")
limitParam := r.URL.Query().Get("limit")
offsetParam := r.URL.Query().Get("offset")
// first figure out what to sort by
switch sortParam {
case "":
sortParam = sortByNameDirFirst
if sortCookie, sortErr := r.Cookie("sort"); sortErr == nil {
sortParam = sortCookie.Value
}
case sortByName, sortByNameDirFirst, sortBySize, sortByTime:
http.SetCookie(w, &http.Cookie{Name: "sort", Value: sortParam, Secure: r.TLS != nil})
}
// then figure out the order
switch orderParam {
case "":
orderParam = "asc"
if orderCookie, orderErr := r.Cookie("order"); orderErr == nil {
orderParam = orderCookie.Value
}
case "asc", "desc":
http.SetCookie(w, &http.Cookie{Name: "order", Value: orderParam, Secure: r.TLS != nil})
}
// finally, apply the sorting and limiting
listing.applySortAndLimit(sortParam, orderParam, limitParam, offsetParam)
}
func (f *FileServer) init(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
r.URL.Path = strings.TrimPrefix(r.URL.Path, f.relativePath)
r.URL.Path = path.Join("/", r.URL.Path)
}
func (f *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.init(w, r)
_path := r.URL.Path
if len(_path) > 0 && _path[0] == '/' {
_path = _path[1:]
}
if strings.HasSuffix(_path, "/") {
_path = _path[:len(_path)-1]
}
if len(_path) == 0 {
_path = "."
}
file, err := f.fileSystem.Open(_path)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("file not found"))
return
}
if errors.Is(err, fs.ErrPermission) {
w.WriteHeader(http.StatusForbidden)
_, _ = w.Write([]byte("permission denied"))
return
}
panic(err)
}
defer func(file fs.File) {
err = file.Close()
if err != nil {
logrus.Warning(err)
}
}(file)
s, err := file.Stat()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("could not stat file"))
panic(err)
}
if s.IsDir() {
f.serveDir(file, s, w, r)
return
}
rs, ok := file.(io.ReadSeeker)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("fs file does not support Seek"))
return
}
if w.Header().Get("Content-Type") == "" {
mtyp := mime.TypeByExtension(filepath.Ext(s.Name()))
if mtyp == "" {
mtyp = "application/octet-stream"
}
}
http.ServeContent(w, r, s.Name(), s.ModTime(), rs)
}
func New(relativePath, root string) http.Handler {
if !strings.HasPrefix(relativePath, "/") {
relativePath = "/" + relativePath
}
indexTemplate, err := template.New("index").Parse(indexTemplateSource)
if err != nil {
panic(err)
}
h := &FileServer{
relativePath: relativePath,
indexTemplate: indexTemplate,
}
h.fileSystem = os.DirFS(h.calculateAbsolutePath(root))
return h
}