-
Notifications
You must be signed in to change notification settings - Fork 1
/
imageInGAE.go
210 lines (162 loc) · 5.19 KB
/
imageInGAE.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
package GAEImageServer
import (
"image"
_ "image/gif"
"image/jpeg"
_ "image/png"
"math"
"net/http"
"github.com/gorilla/mux"
"appengine"
"appengine/blobstore"
"github.com/dbenque/goAppengineToolkit/datastoreEntity"
"appengine/datastore"
"appengine/taskqueue"
)
const imageKind = "image"
type ImageInGAE struct {
EntityId string `json:"entityId"`
ProfileName string `json:"profileName"`
BlobstoreKey string `json:"blobstoreKey" datastore:",noindex"`
}
func (p *ImageInGAE) GetKey() string {
return p.EntityId + ":" + p.ProfileName
}
func (p *ImageInGAE) GetKind() string {
return imageKind
}
func (p *ImageInGAE) store(context appengine.Context) error {
return datastoreEntity.Store(context, p)
}
func (p *ImageInGAE) remove(context appengine.Context) error {
if err := p.retrieve(context); err == nil {
if err := blobstore.Delete(context, appengine.BlobKey(p.BlobstoreKey)); err != nil {
context.Infof("Error while removing in blobstore: %v", *p)
}
// Due to BlobStore Appengine Bug: https://code.google.com/p/googleappengine/issues/detail?id=6849
query := datastore.NewQuery("__BlobFileIndex__").Filter("blob_key =", appengine.BlobKey(p.BlobstoreKey)).KeysOnly()
var values []interface{}
if keys, err := query.GetAll(context, &values); err == nil && len(keys) == 1 {
datastore.Delete(context, keys[0])
}
}
return datastoreEntity.Delete(context, p)
}
func (p *ImageInGAE) retrieve(context appengine.Context) error {
if p.ProfileName == "" {
p.ProfileName = _OriginalProfileName
}
return datastoreEntity.Retrieve(context, p)
}
func handleDeleteImageAllProfile(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
vars := mux.Vars(r)
q := datastore.NewQuery(imageKind).Filter("EntityId =", vars["entityId"])
if err := CreateTasksToDeleteImages(c, q, vars["imgbaseurl"]); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func CreateTasksToDeleteImages(context appengine.Context, q *datastore.Query, baseurl string) error {
t := q.Run(context)
for {
var p ImageInGAE
_, err := t.Next(&p)
if err == datastore.Done {
break // No further entities match the query.
}
if err != nil {
(context).Errorf("fetching next Image: %v", err)
break
}
t := taskqueue.NewPOSTTask("/"+baseurl+"/image/taskDelete", map[string][]string{"entityId": {p.EntityId}, "profileName": {p.ProfileName}})
if _, err := taskqueue.Add((context), t, ""); err != nil {
return err
}
}
return nil
}
func handleDeleteImageTask(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
r.ParseForm()
img := ImageInGAE{EntityId: r.PostFormValue("entityId"), ProfileName: r.PostFormValue("profileName")}
img.remove(c)
w.WriteHeader(http.StatusOK)
}
func handleDeleteImage(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
c := appengine.NewContext(r)
img := ImageInGAE{EntityId: vars["entityId"], ProfileName: vars["profileName"]}
img.remove(c)
w.WriteHeader(http.StatusOK)
}
func handleGetImage(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
c := appengine.NewContext(r)
img := ImageInGAE{EntityId: vars["entityId"], ProfileName: vars["profileName"]}
if err := img.retrieve(c); err != nil {
// check if the image exist with the "original" profile
img.ProfileName = _OriginalProfileName
if err := img.retrieve(c); err != nil {
c.Infof("Error: TODO serve a default image")
w.WriteHeader(http.StatusNoContent)
return
}
// need to resize and serve
img.createResizedImage(c, vars["profileName"])
}
// Serve Image
blobstore.Send(w, appengine.BlobKey(img.BlobstoreKey))
}
func (p *ImageInGAE) createResizedImage(context appengine.Context, targetProfileName string) error {
targetProfile := ImgProfile{Name: targetProfileName}
if err := targetProfile.retrieve(context); err != nil {
return err
}
reader := blobstore.NewReader(context, appengine.BlobKey(p.BlobstoreKey))
img, _, err := image.Decode(reader)
if err != nil {
return err
}
if targetProfile.MaxSize > 0 && (img.Bounds().Max.X > targetProfile.MaxSize || img.Bounds().Max.Y > targetProfile.MaxSize) {
size_x := img.Bounds().Max.X
size_y := img.Bounds().Max.Y
if size_x > targetProfile.MaxSize {
size_x_before := size_x
size_x = targetProfile.MaxSize
size_y = int(math.Floor(float64(size_y) * float64(float64(size_x)/float64(size_x_before))))
}
if size_y > targetProfile.MaxSize {
size_y_before := size_y
size_y = targetProfile.MaxSize
size_x = int(math.Floor(float64(size_x) * float64(float64(size_y)/float64(size_y_before))))
}
img = Resize(img, img.Bounds(), size_x, size_y)
}
// JPEG options
o := &jpeg.Options{Quality: targetProfile.Quality}
// get writerurn e
writer, err := blobstore.Create(context, "image/jpeg")
if err != nil {
return err
}
// Write to Blobstore
if err := jpeg.Encode(writer, img, o); err != nil {
_ = writer.Close()
return err
}
// close writer
if err := writer.Close(); err != nil {
return err
}
newKey, err := writer.Key()
if err != nil {
return err
}
// All good, need to update the image object and store it
p.BlobstoreKey = string(newKey)
p.ProfileName = targetProfileName
p.store(context)
return nil
}