-
Notifications
You must be signed in to change notification settings - Fork 3
/
file.go
156 lines (126 loc) · 2.76 KB
/
file.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
package led
import (
"context"
"io/fs"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"golang.org/x/net/webdav"
)
type FileHandler struct {
Root string
Name string
fs http.Handler
dav http.Handler
}
func NewHandler(root string, name string) *FileHandler {
path := filepath.Join(root, name)
if _, err := os.Stat(path); os.IsNotExist(err) {
path = filepath.Join(root, "default")
}
var fs, dav http.Handler
fs = http.FileServer(leDir{http.Dir(path)})
dav = &webdav.Handler{
Prefix: "/+/dav",
FileSystem: PickDir{Dir: webdav.Dir(path)},
LockSystem: webdav.NewMemLS(),
}
return &FileHandler{
Root: path,
Name: name,
fs: fs,
dav: dav,
}
}
func (h *FileHandler) Rewritten(w http.ResponseWriter, req *http.Request) bool {
b, err := os.ReadFile(path.Join(h.Root, "rewrite.txt"))
if os.IsNotExist(err) {
return false
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return false
}
lines := strings.TrimSpace(string(b))
for _, line := range strings.Split(lines, "\n") {
parts := strings.Split(line, " -> ")
if len(parts) < 2 {
continue
}
oldURL := parts[0]
newURL := parts[1]
if req.URL.Path == oldURL {
http.Redirect(w, req, newURL, http.StatusMovedPermanently)
return true
}
}
return false
}
type leDir struct {
fs http.FileSystem
}
func (d leDir) Open(path string) (f http.File, err error) {
if strings.HasSuffix(path, "/env") || strings.HasSuffix(path, ".md") {
return nil, fs.ErrPermission
}
if strings.HasSuffix(path, ".html") {
htm := strings.TrimSuffix(path, "l")
if f, err = d.fs.Open(htm); err == nil {
return f, nil
}
}
f, err = d.fs.Open(path)
if err != nil {
return nil, err
}
s, err := f.Stat()
if s.IsDir() {
if a, err := d.fs.Open(path + "/.autoindex"); err == nil {
a.Close()
return f, nil
}
if _, err := d.fs.Open(path + "/index.htm"); err != nil {
if _, err := d.fs.Open(path + "/index.html"); err != nil {
return nil, err
}
}
}
return f, nil
}
type PickDir struct {
webdav.Dir
}
func (d PickDir) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
f, err := d.Dir.OpenFile(ctx, name, flag, perm)
if err != nil {
return nil, err
}
return PickFile{File: f, Ignore: `(map.xml|feed.xml|\.(\d+\.svg|htm|yml))$`}, nil
}
type PickFile struct {
webdav.File
Ignore string
}
func (f PickFile) Readdir(n int) ([]os.FileInfo, error) {
if n > 0 {
return nil, os.ErrInvalid
}
fs2 := []os.FileInfo{}
fs, err := f.File.Readdir(n)
if err != nil {
return nil, err
}
for _, f1 := range fs {
ok, err := regexp.MatchString(f.Ignore, f1.Name())
if err != nil {
return nil, err
} else if !ok {
fs2 = append(fs2, f1)
}
}
return fs2, nil
}