-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
264 lines (239 loc) · 6.64 KB
/
update.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package gogaecms
import (
"appengine"
"appengine/blobstore"
"appengine/datastore"
"appengine/memcache"
"appengine/urlfetch"
"archive/zip"
"bytes"
"encoding/gob"
"fmt"
"io/ioutil"
"mime"
"net/http"
"strconv"
"strings"
)
func loadDatas(reader *zip.Reader, w *http.ResponseWriter) *DataEntities {
newData := DataEntities{}
fmt.Fprintf(*w, "Loading datas:\n")
for _, f := range reader.File {
fileName := strings.SplitN(f.Name, "/", 2)[1]
if len(fileName) == 0 || f.FileInfo().IsDir() {
continue
}
if strings.HasPrefix(fileName, "data") {
rc, err := f.Open()
defer rc.Close()
if err != nil {
http.Error(*w, err.Error(), http.StatusInternalServerError)
continue
}
fmt.Fprintf(*w, "%s used as data.\n", fileName)
entities, err := loadCSV(rc)
if err != nil {
http.Error(*w, err.Error(), http.StatusInternalServerError)
continue
}
println("Data:", fileName)
var dataName = fileName[5 : len(fileName)-4]
newData[dataName] = entities
continue
}
}
return &newData
}
func updateHandler(w http.ResponseWriter, r *http.Request) {
// url https://github.com/olostan/test-website/zipball/master
c := appengine.NewContext(r)
client := urlfetch.Client(c)
resp, err := client.Get("https://github.com/olostan/test-website/zipball/master")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
dataReader := bytes.NewReader(data)
reader, err := zip.NewReader(dataReader, resp.ContentLength)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
//defer reader.Close()
err = removeAllContent(c, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
newData := loadDatas(reader, &w)
for _, f := range reader.File {
fileName := strings.SplitN(f.Name, "/", 2)[1]
fmt.Fprintf(w, "Importing '%s':", fileName)
if len(fileName) == 0 || f.FileInfo().IsDir() || strings.HasPrefix(fileName, "data") {
fmt.Fprintf(w, "Skip.\n")
continue
}
rc, err := f.Open()
defer rc.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
var content Content
buf := new(bytes.Buffer)
buf.ReadFrom(rc)
//if f.FileInfo().Size() > 1024*20 {
if !strings.HasSuffix(fileName,".html") {
content = Content{
Data: nil,
}
blobWriter, err := blobstore.Create(c, mime.TypeByExtension(fileName[strings.LastIndex(fileName, "."):]))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
if _, err = blobWriter.Write(buf.Bytes()); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
if err = blobWriter.Close(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
blobKey, err := blobWriter.Key()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
continue
}
content.BlobKey = string(blobKey)
} else {
content = Content{
Data: buf.Bytes(),
}
}
content.ETag = strconv.FormatUint(uint64(f.CRC32), 16)
//fmt.Fprintf(w,"[%s]\n", content)
key := datastore.NewKey(c, "Content", "/"+fileName, 0, nil)
_, err = datastore.Put(c, key, &content)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
fmt.Fprintf(w, "Ok.\n")
}
buff := new(bytes.Buffer)
enc := gob.NewEncoder(buff)
err = enc.Encode(&newData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dataStore := DataStoreRecord{buff.Bytes()}
_, err = datastore.Put(c, datastore.NewKey(c, "Data", "default", 0, nil), &dataStore)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
/*
func loadTemplates(c appengine.Context) (*template.Template, error) {
q := datastore.NewQuery("Content").
Filter("BlobKey =", "")
templates := template.New("root");
var contents []Content
if keys, err := q.GetAll(c, &contents); err != nil {
return nil,err
} else {
for i := range(contents) {
var k *datastore.Key = keys[i]
name := k.StringID()[1:]
_,err = templates.New(name).Parse(string(contents[i].Data[:]))
if err != nil {
return nil,err
}
}
return templates,nil
}
}
func loadGAEDatas(c appengine.Context) (DataEntities, error) {
//result := DataEntities{"test":{{"a","zxc"}}}
var result DataEntities
var key = datastore.NewKey(c, "Data","default",0, nil);
var dataStore DataStoreRecord
if err := datastore.Get(c, key, &dataStore); err != nil {
return nil, err
}
buff := bytes.NewBuffer(dataStore.Bytes)
dec := gob.NewDecoder(buff)
dec.Decode(&result)
return result, nil
}
func transformHandler(w http.ResponseWriter, r *http.Request) {
// url https://github.com/olostan/test-website/zipball/master
c := appengine.NewContext(r)
datas, err := loadGAEDatas(c);
fmt.Fprintln(w,"Transforming...");
err = transformTemplates(c,&datas);
if err!=nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
fmt.Fprintln(w,"Transformed");
}
func transformTemplates(c appengine.Context,datas *DataEntities) error {
println("Transforming templates:");
templates,err := loadTemplates(c);
if err!=nil { return err;}
q := datastore.NewQuery("Content").
Filter("BlobKey =", "")
var contents []Content
if keys, err := q.GetAll(c, &contents); err != nil {
return err
} else {
println("Loaded contents",len(contents))
for i := range(contents) {
var k *datastore.Key = keys[i]
name := k.StringID()[1:]
println("Transforming",name);
//_,err = templates.New(name).Parse(string(contents[i].Data[:]))
buf := new(bytes.Buffer)
templates.ExecuteTemplate(buf,name,datas);
if err != nil {
return err
}
contents[i].Data = buf.Bytes();
_, err := datastore.Put(c, k, &contents[i])
if err !=nil {
print(err);
return err;
}
}
}
memcache.Flush(c);
return nil;
}
*/
func removeAllContent(c appengine.Context, w http.ResponseWriter) error {
memcache.Flush(c)
q := datastore.NewQuery("Content").
Filter("BlobKey >", "")
var contents []Content
if _, err := q.GetAll(c, &contents); err != nil {
return err
}
blobKeys := make([]appengine.BlobKey, len(contents))
for c := range contents {
fmt.Fprintf(w, "Deleted file %s\n", contents[c].BlobKey)
blobKeys[c] = appengine.BlobKey(contents[c].BlobKey)
}
if err := blobstore.DeleteMulti(c, blobKeys); err != nil {
return err
}
keys, err := datastore.NewQuery("Content").KeysOnly().GetAll(c, nil)
if err != nil {
return err
}
fmt.Fprintf(w, "Deleted %d entities\n", len(keys))
err = datastore.DeleteMulti(c, keys)
return err
}