-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
204 lines (189 loc) · 5.35 KB
/
main.js
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
var apejs = require('apejs.js')
var googlestore = require('googlestore.js')
var select = require('select.js')
var mustache = require('./common/mustache.js')
var gdrive = require('./gdrive.js')
var blobstore = require('./blobstore.js')
var images = require('./images.js')
var url = require('./url.js')
var blobService = blobstore.service
var blobInfoFactory = blobstore.blobInfoFactory
apejs.urls = {
'/': {
get: function(req, res) {
var html = mustache.to_html(render('skins/index.html'), {}, {})
print(res).html(html)
}
},
'/tests': {
get: function(req, res) {
// array of modules with tests we wanna test
var tests = [
//images,
url
]
for(var i in tests) {
var curTest = tests[i]
for(var x in curTest.tests) {
// run the test function
curTest.tests[x]()
}
}
}
},
'/read-headers': {
get: function(req, res) {
var headerNames = req.getHeaderNames()
while(headerNames.hasMoreElements()) {
var headerName = headerNames.nextElement()
print(res).html(''+headerName+': '+ req.getHeader(headerName) +'<br>')
}
}
},
'/fileupload': {
get: function(req, res) {
var html = mustache.to_html(render('skins/fileupload.html'), {}, {})
print(res).html(html)
}
},
'/get-files': {
get: function(req, res) {
var files = [];
select('file')
.find()
.sort('createdAt', 'DESC')
.limit(9)
.each(function(id) {
var obj = this
if(this.blobKeyString) {
try {
var blobKey = new BlobKey(this.blobKeyString)
var options = ServingUrlOptions.Builder.withBlobKey(blobKey)
var url = images.service.getServingUrl(options)
obj.thumbUrl = ''+url+'=s500'
} catch(e) {
// it's not an image, it's a file!
obj.thumbUrl = 'http://cdn1.iconfinder.com/data/icons/CrystalClear/128x128/mimetypes/unknown.png'
}
} else if(this.url) { // it's a url
obj.thumbUrl = '/rest/file/datastore/' + id
}
files.push(obj)
})
print(res).json(files)
}
},
'/submit-link': {
post: function(req, res) {
var p = param(req)
var u = p('url')
var title = p('title')
var thumbUrl = images.getThumb(u)
if(!thumbUrl) { // maybe service is down or URL is invalid
return Err(res, 'Image download from service didn\'t work')
}
// let's download the image and save it to datastore
// (easier than blobstore)
var bytes = url.fetchUrl(thumbUrl)
select('file')
.add({
createdAt: new java.util.Date(),
url: u,
title: title,
image: new Blob(bytes),
thumbUrl: thumbUrl
})
}
},
/**
* This service loads data from a Google Drive.
* Shouldn't be a Public API
*/
'/get-gdrive-files': {
get: function(req, res) {
var g = new gdrive
print(res).html(g.createShortUrl())
}
},
/**
* REST API for fileupload
*/
'/rest/file/url': {
get: function(req, res) {
var url = blobService.createUploadUrl("/rest/file")
print(res).json(''+url)
}
},
'/rest/file': {
post: function(req, res) {
var blobs = blobService.getUploadedBlobs(req)
var blobKey = blobs.get('file')
var blobKeyString = blobKey.getKeyString()
// add it to datastore
var e = googlestore.entity('file', {
'createdAt': new java.util.Date(),
'blobKeyString': blobKeyString
})
googlestore.put(e)
res.sendRedirect("/rest/file/" + blobKeyString + "/meta")
}
},
'/rest/file/(.*)/meta': {
get: function(req, res, matches) {
var key = matches[1]
var blobKey = new BlobKey(key)
var info = blobInfoFactory.loadBlobInfo(blobKey);
var ret = [{
name: ''+info.getFilename(),
size: ''+info.getSize(),
url: "/rest/file/" + key
}]
print(res).json(ret)
}
},
'/rest/file/datastore/(.*)': {
get: function(req, res, matches) {
var key = googlestore.createKey('file', parseInt(matches[1], 10))
var file = googlestore.get(key)
var image = file.getProperty('image')
res.setContentType('image/jpg');
res.getOutputStream().write(image.getBytes());
}
},
'/rest/file/(.*)': {
get: function(req, res, matches) {
var key = matches[1]
var blobKey = new BlobKey(key)
var blobInfo = blobInfoFactory.loadBlobInfo(blobKey)
res.setHeader("Content-Disposition", "attachment; filename=" + blobInfo.getFilename())
blobService.serve(blobKey, res)
}
}
};
// simple syntax sugar
function print(response) {
response.setCharacterEncoding("UTF-8");
return {
html: function(html) {
if(response == null) return;
response.setContentType("text/html");
response.getWriter().println(html);
},
json: function(j) {
if(response == null) return;
var jsonString = JSON.stringify(j);
response.setContentType("application/json");
response.getWriter().println(jsonString);
}
};
}
function param(request) {
return function(par) {
var p = request.getParameter(par);
if(p) return p;
else return false;
}
}
function Err(res, msg) {
res.sendError(res.SC_BAD_REQUEST, msg)
}