-
Notifications
You must be signed in to change notification settings - Fork 3
/
gcp.go
204 lines (174 loc) · 4.33 KB
/
gcp.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
package main
import (
"context"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"mime/multipart"
"time"
"cloud.google.com/go/storage"
"github.com/disintegration/imaging"
"github.com/rwcarlsen/goexif/exif"
uuid "github.com/satori/go.uuid"
)
// FileName for a saved image.
func FileName(format imaging.Format) string {
uid := uuid.NewV4().String()
d := time.Now().Format("2006-01-02-03-04-05")
switch format {
case imaging.JPEG:
return fmt.Sprintf("%s-%s.jpg", d, uid)
case imaging.PNG:
return fmt.Sprintf("%s-%s.png", d, uid)
case imaging.GIF:
return fmt.Sprintf("%s-%s.gif", d, uid)
default:
return fmt.Sprintf("%s-%s.png", d, uid)
}
}
// GetOrientation returns the orientation integer.
// See: https://github.com/disintegration/imaging/issues/30
func GetOrientation(f io.Reader) (ot int, err error) {
x, err := exif.Decode(f)
if err != nil {
return 1, nil
}
tag, err := x.Get(exif.Orientation)
if err != nil {
return 1, err
}
ot, _ = tag.Int(0)
return int(ot), nil
}
// GetDimensions grabs the width and height of an image in the cloud.
func GetDimensions(f io.Reader) (int, int, error) {
img, _, err := image.Decode(f)
if err != nil {
return 0, 0, err
}
bounds := img.Bounds()
return bounds.Max.X, bounds.Max.Y, nil
}
// RemoveFileFromGCP removes a file from GCP.
func RemoveFileFromGCP(name string) error {
Info("Requesting to delete %s", name)
ctx := context.Background()
err := App.bh.Object(name).Delete(ctx)
if err != nil {
Error("There was an error removing image from storage: %v", err)
}
return err
}
// Manipulate the image based on config and ot.
func Manipulate(f io.Reader, config ImageConfig, ot int) (image.Image, error) {
img, err := imaging.Decode(f)
if err != nil {
Error("error when decoding the image for imaging lib: %s", err)
return img, err
}
switch {
case ot == 2:
img = imaging.FlipH(img)
case ot == 3:
img = imaging.Rotate180(img)
case ot == 4:
img = imaging.FlipH(img)
img = imaging.Rotate180(img)
case ot == 5:
img = imaging.FlipV(img)
img = imaging.Rotate270(img)
case ot == 6:
img = imaging.Rotate270(img)
case ot == 7:
img = imaging.FlipV(img)
img = imaging.Rotate90(img)
case ot == 8:
img = imaging.Rotate90(img)
}
// Do image processing while at the same time writing to the cloud.
if config.Fill == true {
img = imaging.Fill(
img,
config.Width,
config.Height,
imaging.Center,
imaging.Lanczos,
)
} else {
img = imaging.Fit(img, config.Width, config.Height, imaging.Lanczos)
}
return img, nil
}
// Seek back to the beginning of the file. This should be called after each
// read.
func seekBack(f *multipart.File) error {
if _, err := (*f).Seek(0, 0); err != nil {
Error("error seeking back: %v", err)
return err
}
return nil
}
// UploadFile a file to GCP.
func UploadFile(config ImageConfig, fh *multipart.FileHeader) (*ImageConfig, error) {
mimeType := fh.Header.Get("Content-Type")
formats := map[string]imaging.Format{
"image/png": imaging.PNG,
"image/jpeg": imaging.JPEG,
"image/jpg": imaging.JPEG,
"image/gif": imaging.GIF,
}
format, exists := formats[mimeType]
if exists == false {
Info("imageup is uncertain of image type because the mimetype is %s. Assuming/casting as png.", mimeType)
format = imaging.PNG
mimeType = "image/png"
}
name := FileName(format)
publicURL := fmt.Sprintf(
"https://storage.googleapis.com/%s/%s",
GetEnv("BUCKET_ID", "default"),
name,
)
f, err := fh.Open()
if err != nil {
return nil, err
}
defer f.Close()
// Doesn't really matter if this fails. Doing our best to orientate.
ot, _ := GetOrientation(f)
seekBack(&f)
img, err := Manipulate(f, config, ot)
if err != nil {
return nil, err
}
obj := App.bh.Object(name)
w := obj.NewWriter(context.Background())
w.ACL = []storage.ACLRule{{
Entity: storage.AllUsers,
Role: storage.RoleReader,
}}
w.ContentType = mimeType
w.CacheControl = fmt.Sprintf(
"public, max-age=%s",
GetEnv("CACHE_MAX_AGE", "86400"),
)
if err := imaging.Encode(w, img, format); err != nil {
Error("error when writing to cloud")
w.Close()
return nil, err
}
// Close the connection.
if err := w.Close(); err != nil {
Error("error when closing connection")
return nil, err
}
return &ImageConfig{
FileName: name,
Name: config.Name,
Width: config.Width,
Height: config.Height,
URL: publicURL,
}, nil
}