-
Notifications
You must be signed in to change notification settings - Fork 1
/
imgProfile.go
176 lines (129 loc) · 4.24 KB
/
imgProfile.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
package GAEImageServer
import (
"encoding/json"
"net/http"
"sort"
"github.com/gorilla/mux"
"github.com/dbenque/goAppengineToolkit/datastoreEntity"
"appengine"
"appengine/datastore"
)
const _OriginalProfileName = "original"
type ImgProfile struct {
Name string `json:"name"`
Quality int `json:"quality" datastore:",noindex"`
MaxSize int `json:"maxSize" datastore:",noindex"`
}
type ImgProfileNameSet struct {
Index sort.StringSlice `json:"index"` // Index of profiles
//Index []string `json:"index"` // Index of profiles
}
// retrieve the index of profiles (initialize the key if nil)
func GetImgProfileNameSet(context appengine.Context) ImgProfileNameSet {
key := datastore.NewKey(context, "ImgProfileNameSet", "ImgProfileNameSet", 0, nil)
allProfiles := ImgProfileNameSet{}
if err := datastore.Get(context, key, &allProfiles); err != nil {
(context).Infof("No profile could be retrieved")
return ImgProfileNameSet{}
}
return allProfiles
}
func (p *ImgProfile) GetKey() string {
return p.Name
}
func (p *ImgProfile) GetKind() string {
return "ImgProfile"
}
func (p *ImgProfile) store(context appengine.Context) error {
if err := datastoreEntity.Store(context, p); err != nil {
return err
}
// Update profile index
keyIndex := datastore.NewKey(context, "ImgProfileNameSet", "ImgProfileNameSet", 0, nil)
allProfilesStruct := GetImgProfileNameSet(context)
// avoid dupe insertion
var found bool = false
for _, value := range allProfilesStruct.Index {
found = value == p.Name
if found {
break
}
}
if !found {
allProfilesStruct.Index = sort.StringSlice(append(allProfilesStruct.Index, p.Name)[0:])
allProfilesStruct.Index.Sort()
if _, err := datastore.Put(context, keyIndex, &allProfilesStruct); err != nil {
(context).Errorf("Can't store index, %v", err)
}
}
(context).Infof("Profile stored: %v", *p)
return nil
}
func (p *ImgProfile) remove(context appengine.Context) error {
if err := datastoreEntity.Delete(context, p); err != nil {
return err
}
// Update profile index
keyIndex := datastore.NewKey(context, "ImgProfileNameSet", "ImgProfileNameSet", 0, nil)
allProfilesStruct := GetImgProfileNameSet(context)
i := allProfilesStruct.Index.Search(p.Name)
if i != len(allProfilesStruct.Index) {
allProfilesStruct.Index = sort.StringSlice(append(allProfilesStruct.Index[:i], allProfilesStruct.Index[i+1:]...)[0:]) // remove element at i and create a StringSlice with remaining items
allProfilesStruct.Index.Sort()
if _, err := datastore.Put(context, keyIndex, &allProfilesStruct); err != nil {
(context).Errorf("Can't store index, %v", err)
}
}
(context).Infof("Profile removed: %v", p.Name)
return nil
}
func (p *ImgProfile) retrieve(context appengine.Context) error {
return datastoreEntity.Retrieve(context, p)
}
func handleImgProfileGet(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
c := appengine.NewContext(r)
profile := ImgProfile{Name: vars["name"]}
if err := profile.retrieve(c); err != nil {
w.WriteHeader(http.StatusNoContent)
return
}
profileJson, _ := json.Marshal(profile)
w.Header().Set("Content-Type", "application/json")
w.Write(profileJson)
}
func handleImgProfileStore(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
decoder := json.NewDecoder(r.Body)
var profile ImgProfile
err := decoder.Decode(&profile)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
profile.store(c)
w.WriteHeader(http.StatusOK)
}
func handleImgProfileDelete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
c := appengine.NewContext(r)
profile := ImgProfile{Name: vars["name"]}
if profile.Name == _OriginalProfileName {
w.WriteHeader(http.StatusNotAcceptable)
return
}
profile.remove(c)
// Clean all associated image
q := datastore.NewQuery(imageKind).Filter("ProfileName =", vars["name"])
if err := CreateTasksToDeleteImages(c, q, vars["imgbaseurl"]); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func handleGetAllProfiles(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
profilesJson, _ := json.Marshal(GetImgProfileNameSet(c).Index)
w.Header().Set("Content-Type", "application/json")
w.Write(profilesJson)
}