/
main.go
174 lines (143 loc) · 3.3 KB
/
main.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
package main
import (
"bytes"
"flag"
"log"
"path/filepath"
"reflect"
"sort"
"strings"
"text/template"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var t *template.Template
var (
bucket string
rawBucket = flag.String("bucket", "", "generate for this bucket")
)
func init() {
flag.Parse()
bucket = *rawBucket
if len(bucket) > 4 && bucket[:5] == "s3://" {
bucket = bucket[5:]
}
prefix := "tools/cdn-indexer/"
name := filepath.Join(prefix, "index.html.tmpl")
t = template.Must(template.New("index.html.tmpl").Funcs(template.FuncMap{
"filterZero": func(v interface{}) interface{} {
if v == reflect.Zero(reflect.TypeOf(v)).Interface() {
return ""
}
return v
},
}).ParseFiles(name))
}
type fileType string
const (
directoryType fileType = "directory"
genericType = "generic"
)
type cdnFile struct {
Name string
Type fileType
Size int64
Modified time.Time
}
// Listings are sorted lexicographically, with directories coming before
// files.
type sortCDNFile []cdnFile
func (d sortCDNFile) Len() int { return len(d) }
func (d sortCDNFile) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (d sortCDNFile) Less(i, j int) bool {
if d[i].Type != d[j].Type {
return d[i].Type == directoryType
}
return strings.Compare(d[i].Name, d[j].Name) < 0
}
func main() {
sess, err := session.NewSession()
if err != nil {
log.Println("failed to create session,", err)
return
}
svc := s3.New(sess)
params := &s3.ListObjectsV2Input{
Bucket: aws.String(bucket), // Required
}
listings := make(map[string][]cdnFile)
err = svc.ListObjectsV2Pages(params, func(page *s3.ListObjectsV2Output, lastPage bool) bool {
for _, o := range page.Contents {
f := cdnFile{
Name: filepath.Base(*o.Key),
Type: genericType,
Size: *o.Size,
Modified: *o.LastModified,
}
// index pages shouldn't list themselves.
if f.Name == "index.html" {
continue
}
dir := filepath.Dir(*o.Key)
listings[dir] = append(listings[dir], f)
}
return true
})
if err != nil {
log.Fatal(err)
}
// Add dirs into their parent listings
for dir := range listings {
for {
f := cdnFile{
Name: filepath.Base(dir),
Type: directoryType,
}
if f.Name == "." {
break
}
dir = filepath.Dir(dir)
found := false
for _, o := range listings[dir] {
if o.Name == f.Name {
found = true
break
}
}
if !found {
listings[dir] = append(listings[dir], f)
}
}
}
now := time.Now()
for dir, listing := range listings {
if dir == "." {
dir = "/"
}
sort.Sort(sortCDNFile(listing))
buf := &bytes.Buffer{}
err = t.Execute(buf, struct {
Dir string
Files []cdnFile
Timestamp time.Time
}{Dir: dir, Files: listing, Timestamp: now})
if err != nil {
log.Fatal(err)
}
dirPath := dir
params := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(filepath.Join(dirPath, "index.html")),
Body: bytes.NewReader(buf.Bytes()),
ContentType: aws.String("text/html"),
CacheControl: aws.String("public, max-age=300"),
}
_, err := svc.PutObject(params)
if err != nil {
log.Fatal(err)
}
log.Printf("Wrote /%s\n", filepath.Join(dirPath, "index.html"))
}
}