-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
161 lines (128 loc) · 4.69 KB
/
server.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
package GAEImageServer
import (
"html/template"
"io"
"net/http"
"github.com/gorilla/mux"
"appengine"
"appengine/blobstore"
"appengine/urlfetch"
)
func serveError(c appengine.Context, w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, "Internal Server Error")
c.Errorf("%v", err)
}
var formTemplate = template.Must(template.New("root").Parse(formTemplateHTML))
const formTemplateHTML = `
<form action="{{.}}" method="POST" enctype="multipart/form-data">
Image: <input type="file" name="file"><br>
<input type="text" name="callbackurl" value="http://0.0.0.0:8080/callbacktest"> [callbackurl] Url to callback once the file is store in the blobstore<br>
<input type="text" name="entityId" value="myId"> [entityId] Id of the entity that is associated with the image. To be reused in future retrieve query<br>
<input type="text" name="extraparam1" value="val1"/>
<input type="text" name="extraparam2" value="val2"/> Any other post parameter will be passed to the [callbackurl] <br>
<input type="submit" name="submit" value="Submit">
</form>`
const callbackUrl = "callbackurl"
const entityId = "entityId"
func handleFormAction(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
vars := mux.Vars(r)
uploadURL, err := blobstore.UploadURL(c, "/"+vars["imgbaseurl"]+"/uploaded", nil)
if err != nil {
serveError(c, w, err)
return
}
w.Write([]byte(uploadURL.String()))
}
func handleForm(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
vars := mux.Vars(r)
c.Infof("In handleForm!! Before getting blobstoreURL", r.PostForm)
uploadURL, err := blobstore.UploadURL(c, "/"+vars["imgbaseurl"]+"/uploaded", nil)
if err != nil {
serveError(c, w, err)
return
}
c.Infof("In handleForm!! Before template processing", r.PostForm)
w.Header().Set("Content-Type", "text/html")
err = formTemplate.Execute(w, uploadURL)
if err != nil {
c.Errorf("%v", err)
}
}
func handleCallbackTest(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
c := appengine.NewContext(r)
c.Infof("callbackdefault received form: %v", r.PostForm)
w.WriteHeader(200)
}
func handleUploadComplete(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}
func handleUploadedInBlobStore(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// Create options to resize image
o := NewCompressionOptions(r)
originalProfile := ImgProfile{Name: _OriginalProfileName}
if originalProfile.retrieve(c) != nil {
// Set max size
o.Size = 300
// Set quality
o.Quality = 75
c.Warningf("No image profile named '%s'", _OriginalProfileName)
} else {
// Set max size from profile
o.Size = originalProfile.MaxSize
// Set Quality
o.Quality = originalProfile.Quality
}
// Get the resized blobs and other values
blobs, vals, err := ParseBlobs(o)
if err != nil {
serveError(c, w, err)
return
}
vars := mux.Vars(r)
file := blobs["file"]
if len(file) == 0 {
c.Errorf("no file uploaded")
http.Redirect(w, r, "/"+vars["imgbaseurl"]+"/", http.StatusFound)
return
}
blobstoreKey := string(file[0].BlobKey)
vals.Add("blobKey", blobstoreKey)
c.Infof("Vals: %v", vals)
// Store the Image Object
img := ImageInGAE{EntityId: vals.Get(entityId), ProfileName: _OriginalProfileName, BlobstoreKey: blobstoreKey}
img.store(c)
client := urlfetch.Client(c)
_, post_err := client.PostForm(vals.Get(callbackUrl), vals)
if post_err != nil {
serveError(c, w, post_err)
return
}
http.Redirect(w, r, "/"+vars["imgbaseurl"]+"/uploaded/complete", http.StatusFound)
}
func init() {
r := mux.NewRouter()
// uploading img
r.HandleFunc("/{imgbaseurl}/action", handleFormAction)
r.HandleFunc("/{imgbaseurl}/exampleForm", handleForm)
r.HandleFunc("/{imgbaseurl}/callbacktest", handleCallbackTest)
r.HandleFunc("/{imgbaseurl}/uploaded", handleUploadedInBlobStore)
r.HandleFunc("/{imgbaseurl}/uploaded/complete", handleUploadComplete)
// ImgProfile
r.HandleFunc("/{imgbaseurl}/imgProfiles", handleGetAllProfiles).Methods("GET")
r.HandleFunc("/{imgbaseurl}/imgProfile", handleImgProfileStore).Methods("POST")
r.HandleFunc("/{imgbaseurl}/imgProfile/{name}", handleImgProfileDelete).Methods("DELETE")
r.HandleFunc("/{imgbaseurl}/imgProfile/{name}", handleImgProfileGet).Methods("GET")
// ImageInGAR
r.HandleFunc("/{imgbaseurl}/image/{entityId}/{profileName}", handleGetImage).Methods("GET")
r.HandleFunc("/{imgbaseurl}/image/{entityId}/{profileName}", handleDeleteImage).Methods("DELETE")
r.HandleFunc("/{imgbaseurl}/image/{entityId}", handleDeleteImageAllProfile).Methods("DELETE")
// ImageTask
r.HandleFunc("/{imgbaseurl}/image/taskDelete", handleDeleteImageTask).Methods("POST")
http.Handle("/", r)
}