-
Notifications
You must be signed in to change notification settings - Fork 21
/
browse.go
160 lines (139 loc) · 3.79 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
package caddys3proxy
import (
"bytes"
"encoding/json"
"html/template"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/dustin/go-humanize"
)
var bufPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
type PageObj struct {
Count int64 `json:"count"`
Items []Item `json:"items"`
MoreLink string `json:"more"`
}
type Item struct {
Name string `json:"name"`
IsDir bool `json:"is_dir"`
Key string `json:"key"`
Url string `json:"url"`
Size string `json:"size"`
LastModified string `json:"last_modified"`
}
// GenerateJson generates JSON output for the PageObj
func (po PageObj) GenerateJson(w http.ResponseWriter) error {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)
err := json.NewEncoder(buf).Encode(po)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, err = buf.WriteTo(w)
return err
}
func (p S3Proxy) ConstructListObjInput(r *http.Request, key string) s3.ListObjectsV2Input {
// We need to strip the first '/' from the key to make it a valid prefix
prefix := strings.TrimPrefix(key, "/")
input := s3.ListObjectsV2Input{
Bucket: aws.String(p.Bucket),
Prefix: aws.String(prefix),
Delimiter: aws.String("/"),
}
nextToken := r.URL.Query().Get("next")
if nextToken != "" {
input.ContinuationToken = aws.String(nextToken)
}
maxPerPage := r.URL.Query().Get("max")
if maxPerPage != "" {
maxKeys, err := strconv.ParseInt(maxPerPage, 10, 64)
if err == nil && maxKeys > 0 && maxKeys <= 1000 {
input.MaxKeys = aws.Int64(maxKeys)
}
}
return input
}
// GenerateHtml generates html output for the PageObj
func (po PageObj) GenerateHtml(w http.ResponseWriter, template *template.Template) error {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)
err := template.Execute(buf, po)
if err != nil {
return err
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, err = buf.WriteTo(w)
return err
}
func (p S3Proxy) MakePageObj(result *s3.ListObjectsV2Output) PageObj {
po := PageObj{}
po.Count = *result.KeyCount
if result.NextContinuationToken != nil {
var nextUrl url.URL
queryItems := nextUrl.Query()
queryItems.Add("next", *result.NextContinuationToken)
if result.MaxKeys != nil {
queryItems.Add("max", strconv.FormatInt(*result.MaxKeys, 10))
}
nextUrl.RawQuery = queryItems.Encode()
po.MoreLink = nextUrl.String()
}
for _, dir := range result.CommonPrefixes {
name := path.Base(*dir.Prefix)
dirPath := "./" + name + "/"
po.Items = append(po.Items, Item{
Url: dirPath,
Name: name,
IsDir: true,
})
}
for _, obj := range result.Contents {
name := path.Base(*obj.Key)
itemPath := "./" + name
size := humanize.Bytes(uint64(*obj.Size))
timeAgo := humanize.Time(*obj.LastModified)
po.Items = append(po.Items, Item{
Name: name,
Key: *obj.Key,
Url: itemPath,
Size: size,
LastModified: timeAgo,
IsDir: false,
})
}
return po
}
// This is a lame ass default template - needs to get better
const defaultBrowseTemplate = `<!DOCTYPE html>
<html>
<body>
<ul>
{{- range .PageObj }}
<li>
{{- if .IsDir}}
<a href="{{html .Url}}">{{html .Name}}</a>
{{- else}}
<a href="{{html .Url}}">{{html .Name}}</a> Size: {{html .Size}} Last Modified: {{html .LastModified}}
{{- end}}
</li>
{{- end }}
</ul>
<p>number of items: {{ .Count }}</p>
{{- if .MoreLink }}
<a href="{{ html .MoreLink }}">more...</a>
{{- end }}
</body>
</html>`