This repository has been archived by the owner on Aug 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
serve.go
127 lines (104 loc) · 2.93 KB
/
serve.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
package s3browser
import (
"encoding/json"
"io"
"net/http"
"strings"
"time"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
func (b S3Browser) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
fullPath := r.URL.Path
if fullPath == "" {
fullPath = "/"
}
switch r.Method {
case http.MethodGet, http.MethodHead:
// proceed, noop
case http.MethodPost:
return b.serveAPI(w, r)
case "PROPFIND", http.MethodOptions:
return caddyhttp.Error(http.StatusNotImplemented, nil)
default:
return next.ServeHTTP(w, r)
}
if dir, ok := b.s3Cache.GetDir(fullPath); ok {
return b.serveDirectory(w, r, dir)
}
if _, ok := b.s3Cache.GetFile(fullPath); ok {
if b.SignedURLRedirect {
return b.signedRedirect(w, r, normalizePath(fullPath))
}
return b.serveFile(w, r, normalizePath(fullPath))
}
return next.ServeHTTP(w, r)
}
func (b *S3Browser) serveAPI(w http.ResponseWriter, r *http.Request) error {
if b.RefreshAPISecret != "" {
if _, pwd, ok := r.BasicAuth(); !ok || pwd != b.RefreshAPISecret {
return caddyhttp.Error(http.StatusUnauthorized, nil)
}
}
b.refreshTrigger <- struct{}{}
return nil
}
func (b *S3Browser) serveDirectory(w http.ResponseWriter, r *http.Request, dir Directory) error {
renderFunc := b.renderHTML
contentType := "text/html"
acceptHeader := strings.ToLower(strings.Join(r.Header["Accept"], ","))
if strings.Contains(acceptHeader, "application/json") {
renderFunc = b.renderJSON
contentType = "application/json"
}
w.Header().Set("Content-Type", contentType+"; charset=utf-8")
return renderFunc(w, dir)
}
func (b *S3Browser) renderJSON(w io.Writer, dir Directory) error {
var data []byte
var err error
if !b.Debug {
data, err = json.Marshal(dir)
} else {
data, err = json.MarshalIndent(dir, "", " ")
}
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
func (b *S3Browser) renderHTML(w io.Writer, dir Directory) error {
return b.template.Execute(w, TemplateArgs{
SiteName: b.SiteName,
Dir: dir,
})
}
func (b *S3Browser) signedRedirect(w http.ResponseWriter, r *http.Request, filePath string) error {
client := b.newS3Client()
url, err := client.s3.PresignedGetObject(b.Bucket, filePath[1:], 10*time.Minute, nil)
if err == nil {
return err
}
http.Redirect(w, r, url.String(), http.StatusTemporaryRedirect)
return nil
}
func (b *S3Browser) serveFile(w http.ResponseWriter, r *http.Request, filePath string) error {
client := b.newS3Client()
var rangeHdr string
if val, ok := r.Header["Range"]; ok {
rangeHdr = val[0]
}
reader, _, headers, err := client.GetObject(filePath, rangeHdr)
if err != nil {
return err
}
w.Header().Set("Content-Type", headers.Get("Content-Type"))
w.Header().Set("Content-Length", headers.Get("Content-Length"))
if headers.Get("Content-Range") != "" {
w.Header().Set("Content-Range", headers.Get("Content-Range"))
}
if _, err := io.Copy(w, reader); err != nil {
return err
}
return nil
}