-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
165 lines (146 loc) · 3.96 KB
/
list.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
// Copyright (C) 2021 The Takeout Authors.
//
// This file is part of Takeout.
//
// Takeout is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// Takeout is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
// more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Takeout. If not, see <https://www.gnu.org/licenses/>.
package bucket
import (
"fmt"
"net/url"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/defsub/takeout/config"
)
type Bucket struct {
config *config.BucketConfig
s3 *s3.S3
}
type Object struct {
Key string
Path string // Key modified by rewrite rules
ETag string
Size int64
LastModified time.Time
}
func OpenAll(buckets []config.BucketConfig) ([]*Bucket, error) {
var list []*Bucket
for i := range buckets {
b, err := Open(buckets[i])
if err == nil {
return list, err
}
list = append(list, b)
}
return list, nil
}
func OpenMedia(buckets []config.BucketConfig, mediaType string) ([]Bucket, error) {
var list []Bucket
for i := range buckets {
if buckets[i].Media != mediaType {
continue
}
b, err := Open(buckets[i])
if err != nil {
return list, err
}
list = append(list, *b)
}
return list, nil
}
// Connect to the configured S3 bucket.
// Tested: Wasabi, Backblaze, Minio
func Open(config config.BucketConfig) (*Bucket, error) {
creds := credentials.NewStaticCredentials(
config.AccessKeyID,
config.SecretAccessKey, "")
s3Config := &aws.Config{
Credentials: creds,
Endpoint: aws.String(config.Endpoint),
Region: aws.String(config.Region),
S3ForcePathStyle: aws.Bool(true)}
session, err := session.NewSession(s3Config)
bucket := &Bucket{
s3: s3.New(session),
config: &config,
}
return bucket, err
}
func (b *Bucket) List(lastSync time.Time) (objectCh chan *Object, err error) {
objectCh = make(chan *Object)
go func() {
defer close(objectCh)
var continuationToken *string
continuationToken = nil
for {
req := s3.ListObjectsV2Input{
Bucket: aws.String(b.config.BucketName),
Prefix: aws.String(b.config.ObjectPrefix)}
if continuationToken != nil {
req.ContinuationToken = continuationToken
}
resp, err := b.s3.ListObjectsV2(&req)
if err != nil {
break
}
for _, obj := range resp.Contents {
if obj.LastModified != nil &&
obj.LastModified.After(lastSync) {
objectCh <- &Object{
Key: *obj.Key,
Path: b.Rewrite(*obj.Key),
ETag: *obj.ETag,
Size: *obj.Size,
LastModified: *obj.LastModified,
}
}
}
if !*resp.IsTruncated {
break
}
continuationToken = resp.NextContinuationToken
}
}()
return
}
// Generate a presigned url which expires based on config settings.
func (b *Bucket) Presign(key string) *url.URL {
req, _ := b.s3.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(b.config.BucketName),
Key: aws.String(key)})
urlStr, _ := req.Presign(b.config.URLExpiration)
url, _ := url.Parse(urlStr)
return url
}
func (b *Bucket) Rewrite(path string) string {
result := path
for _, rule := range b.config.RewriteRules {
re := regexp.MustCompile(rule.Pattern)
matches := re.FindStringSubmatch(result)
if matches != nil {
result = rule.Replace
for i := range matches {
result = strings.ReplaceAll(result, fmt.Sprintf("$%d", i), matches[i])
}
}
}
if result != path {
fmt.Printf("rewrite %s -> %s\n", path, result)
}
return result
}