-
Notifications
You must be signed in to change notification settings - Fork 12
/
photo.go
239 lines (194 loc) · 5.88 KB
/
photo.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"crypto/rsa"
"fmt"
"log"
"net/url"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudfront/sign"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/globocom/gothumbor"
)
type RescaledPhoto struct {
Key string
BaseUrl *url.URL
}
type ImgixRescaledPhoto struct {
*RescaledPhoto
}
// for use with thumbor as a basic setup, URL signing mandatory.
// quick implementation available at: https://github.com/APSL/docker-thumbor
// be warned - it's a resource hungry beast.
type ThumborRaw struct {
*RescaledPhoto
Secret string
}
// for use with thumbor backed by AWS lambda with *cloudfront* URL signing (on unsafe thumbor urls)
// see: https://docs.aws.amazon.com/solutions/latest/serverless-image-handler/welcome.html
type ThumborCloudfront struct {
*RescaledPhoto //it's distinct enough (doesn't have a need for thumbor url signing)
AWSCloudfrontKeyPairId string //required for URL signing
AWSCloudfrontPrivateKey *rsa.PrivateKey //required for URL signing
}
type S3Photo struct {
Key string
BucketName string
awsSession *session.Session
}
type ImageProxy struct {
*S3Photo
ImageProxy string
}
type Renderable interface {
Slug() string
GetPhotoForWidth(int) string
GetThumbnailForWidthAndHeight(int, int) string
}
func (p *RescaledPhoto) Slug() string {
parts := strings.Split(p.Key, "/")
return parts[len(parts)-1]
}
func (p *ImgixRescaledPhoto) GetPhotoForWidth(w int) string {
keyPathUrl, err := url.Parse(p.Key)
if err != nil {
log.Print(err)
return ""
}
fullUrl := p.BaseUrl.ResolveReference(keyPathUrl)
queryValues := fullUrl.Query()
queryValues.Add("w", fmt.Sprint(w))
fullUrl.RawQuery = queryValues.Encode()
return fullUrl.String()
}
func (p *ImgixRescaledPhoto) GetThumbnailForWidthAndHeight(w, h int) string {
keyPathUrl, err := url.Parse(p.Key)
if err != nil {
log.Print(err)
return ""
}
fullUrl := p.BaseUrl.ResolveReference(keyPathUrl)
queryValues := fullUrl.Query()
queryValues.Add("w", fmt.Sprint(w))
queryValues.Add("max-h", fmt.Sprint(h))
queryValues.Add("fit", "crop")
queryValues.Add("crop", "faces")
fullUrl.RawQuery = queryValues.Encode()
return fullUrl.String()
}
func (p *ThumborRaw) GetPhotoForWidth(w int) string {
thumborOptions := gothumbor.ThumborOptions{Width: w, Smart: true}
thumborPath, err := gothumbor.GetCryptedThumborPath(p.Secret, p.Key, thumborOptions)
if err != nil {
log.Print(err)
return ""
}
parsedPath, err := url.Parse(thumborPath)
fullUrl := p.BaseUrl.ResolveReference(parsedPath)
return fullUrl.String()
}
func (p *ThumborRaw) GetThumbnailForWidthAndHeight(w, h int) string {
thumborOptions := gothumbor.ThumborOptions{Width: w, Height: h, Smart: true}
thumborPath, err := gothumbor.GetCryptedThumborPath(p.Secret, p.Key, thumborOptions)
if err != nil {
log.Print(err)
return ""
}
parsedPath, err := url.Parse(thumborPath)
fullUrl := p.BaseUrl.ResolveReference(parsedPath)
return fullUrl.String()
}
func (p *ThumborCloudfront) SignCloudfrontURL(path string) string {
parsedPath, err := url.Parse(path)
if err != nil {
log.Printf("Failed to parse URL for signing, err: %s\n", err.Error())
return ""
}
fullUrl := p.BaseUrl.ResolveReference(parsedPath)
// now sign for cloudfront
signer := sign.NewURLSigner(p.AWSCloudfrontKeyPairId, p.AWSCloudfrontPrivateKey)
signedURL, err := signer.Sign(fullUrl.String(), time.Now().Add(1*time.Hour))
if err != nil {
log.Printf("Failed to sign url, err: %s\n", err.Error())
return ""
}
return signedURL
}
func (p *ThumborCloudfront) GetPhotoForWidth(w int) string {
// get thumbor path without signing
thumborOptions := gothumbor.ThumborOptions{Width: w, Smart: true}
thumborPath, err := gothumbor.GetThumborPath(p.Key, thumborOptions)
if err != nil {
log.Print(err)
return ""
}
return p.SignCloudfrontURL(thumborPath)
}
func (p *ThumborCloudfront) GetThumbnailForWidthAndHeight(w, h int) string {
thumborOptions := gothumbor.ThumborOptions{Width: w, Height: h, Smart: true}
thumborPath, err := gothumbor.GetThumborPath(p.Key, thumborOptions)
if err != nil {
log.Print(err)
return ""
}
return p.SignCloudfrontURL(thumborPath)
}
func (p *S3Photo) Slug() string {
parts := strings.Split(p.Key, "/")
return parts[len(parts)-1]
}
func (p *S3Photo) GetPhotoForWidth(w int) string {
req, _ := s3.New(p.awsSession).GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(p.BucketName),
Key: aws.String(p.Key),
})
signedUrl, err := req.Presign(24 * time.Hour)
if err != nil {
log.Printf("Unable to sign URL for S3Photo. Error: %s\n", err.Error())
return ""
}
return signedUrl
}
func (p *S3Photo) GetThumbnailForWidthAndHeight(w, h int) string {
return p.GetPhotoForWidth(w)
}
func (p *ImageProxy) Slug() string {
parts := strings.Split(p.Key, "/")
return parts[len(parts)-1]
}
func (p *ImageProxy) GetPhotoForWidth(w int) string {
req, _ := s3.New(p.awsSession).GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(p.BucketName),
Key: aws.String(p.Key),
})
signedUrl, err := req.Presign(24 * time.Hour)
if err != nil {
log.Printf("Unable to sign URL for S3Photo. Error: %s\n", err.Error())
return ""
}
queryUrl, err := url.Parse(signedUrl)
if err != nil {
log.Printf("Unable to sign URL %s for S3Photo. Error: %s\n", queryUrl.String(), err.Error())
return ""
}
return fmt.Sprintf("%s/%dx/%s", strings.TrimRight(p.ImageProxy, "/"), w, signedUrl)
}
func (p *ImageProxy) GetThumbnailForWidthAndHeight(w, h int) string {
return p.GetPhotoForWidth(w)
}
/*
Used when we can't get the photo required, and have to return something, for example in methods used by templates
*/
type ErrorPhoto struct {
}
func (p *ErrorPhoto) Slug() string {
return ""
}
func (p *ErrorPhoto) GetPhotoForWidth(w int) string {
return ""
}
func (p *ErrorPhoto) GetThumbnailForWidthAndHeight(w, h int) string {
return ""
}