forked from techknowlogick/caddy-s3browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowse.go
More file actions
136 lines (126 loc) · 3.52 KB
/
Copy pathbrowse.go
File metadata and controls
136 lines (126 loc) · 3.52 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
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
package s3browser
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"strings"
"time"
"github.com/admpub/caddy/caddyhttp/httpserver"
"github.com/minio/minio-go/v7"
)
type Browse struct {
Next httpserver.Handler
Config Config
Client *minio.Client
Fs map[string]Directory
Template *template.Template
}
const (
AssetsPrefix = `/---caddy-s3browser---/`
MIMEOctetStream = "application/octet-stream"
)
func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
path := r.URL.Path
if len(path) == 0 {
path = "/"
} else if len(b.Config.CSSCDN) == 0 && strings.HasPrefix(path, AssetsPrefix) {
path = strings.TrimPrefix(path, AssetsPrefix)
fp, err := assets.Open(path)
if err != nil {
return http.StatusNotFound, nil
}
defer fp.Close()
fi, err := fp.Stat()
if err != nil {
return http.StatusNotFound, nil
}
if fi.IsDir() {
return http.StatusNotFound, nil
}
if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && fi.ModTime().Before(t.Add(1*time.Second)) {
w.Header().Del("Content-Type")
w.Header().Del("Content-Length")
w.WriteHeader(http.StatusNotModified)
return http.StatusNotModified, nil
}
w.Header().Set("Content-Type", ContentTypeByExtension(fi.Name()))
w.Header().Set("Last-Modified", fi.ModTime().UTC().Format(http.TimeFormat))
n, err := io.Copy(w, fp)
if err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Length", fmt.Sprintf(`%d`, n))
return http.StatusOK, err
}
if _, ok := b.Fs[path]; !ok {
if !strings.HasSuffix(path, `/`) { // 访问的是文件
account, err := b.Config.GetAccount()
if err != nil {
return http.StatusInternalServerError, err
}
if len(account.CDNURL) > 0 { // 如果指定了CDN的网址,则跳转到CDN网址
endpoint := account.CDNURL + path
http.Redirect(w, r, endpoint, http.StatusFound)
return http.StatusFound, nil
}
return b.Next.ServeHTTP(w, r)
}
// 访问未登记的目录返回 not found
return http.StatusNotFound, nil
}
switch r.Method {
case http.MethodGet, http.MethodHead:
// proceed, noop
case "PROPFIND", http.MethodOptions:
return http.StatusNotImplemented, nil
default:
return b.Next.ServeHTTP(w, r)
}
var buf *bytes.Buffer
var err error
acceptHeader := strings.ToLower(strings.Join(r.Header["Accept"], ","))
switch {
case strings.Contains(acceptHeader, "application/json"):
if buf, err = b.formatAsJSON(b.Fs[path]); err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
default:
if buf, err = b.formatAsHTML(b.Fs[path]); err != nil {
if b.Config.Debug {
fmt.Println(err)
}
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
buf.WriteTo(w)
return http.StatusOK, nil
}
func (b Browse) formatAsJSON(listing Directory) (*bytes.Buffer, error) {
account, err := b.Config.GetAccount()
if err != nil {
return nil, err
}
data := TmplData{CDNURL: account.CDNURL, Directory: listing}
marsh, err := json.Marshal(data)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
_, err = buf.Write(marsh)
return buf, err
}
func (b Browse) formatAsHTML(listing Directory) (*bytes.Buffer, error) {
account, err := b.Config.GetAccount()
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
data := TmplData{CDNURL: account.CDNURL, Directory: listing}
err = b.Template.Execute(buf, data)
return buf, err
}