-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
339 lines (268 loc) · 9.14 KB
/
app.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var express = require('express');
var http = require('http');
var path = require('path');
var config = require('config');
var log = require('libs/log')(module);
var sm = require('sitemap');
//var compression = require('compression');
//var paginate = require('express-paginate');
var resizeImage = require('middleware/resizeImage');
var Cover = require('models/cover').Cover;
var mongoose = require('libs/mongoose');
/* Crypto */
var crypto = require('crypto');
/* Upload */
var formidable = require('formidable');
var util = require('util');
var fs = require('fs-extra');
var app = express();
app.use(express.compress());
app.engine('ejs', require('ejs-locals'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
//app.use(express.favicon());
//app.use(qt.static(__dirname + '/'));
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.cookieParser());
app.use(require('middleware/getInfo'));
app.use(app.router);
require('routes')(app);
app.use(express.static(path.join(__dirname, 'public')));
app.use(function (err, req, res, next) {
// NODE_ENV = 'production'
if (app.get('env') == 'development') {
var errorHandler = express.errorHandler();
errorHandler(err, req, res, next);
} else {
res.send(500);
}
});
// development only
/*
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
var routes = require('./routes');
var user = require('./routes/user');
app.get('/', routes.index);
app.get('/users', user.list);
app.use(function(req, res, next) {
if (req.url == '/forbidden') {
next(new Error("wops, denied"));
} else {
next();
}
});
app.use(function(req, res, next) {
if (req.url == '/test') {
res.end("Test");
} else {
next();
}
});
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
*/
//app.locals(require('express-pagination'));
/*app.get('/users', function(req, res, next) {
// This example assumes you've previously defined `Users`
// as `var Users = db.model('Users')` if you are using `mongoose`
// and that you've added the Mongoose plugin `mongoose-paginate`
// to the Users model via `User.plugin(require('mongoose-paginate'))`
Users.paginate({}, req.query.page, req.query.limit, function(err, pageCount, users, itemCount) {
if (err) return next(err)
res.format({
html: function() {
res.render('users', {
users: users,
pageCount: pageCount,
itemCount: itemCount
})
},
json: function() {
// inspired by Stripe's API response for list objects
res.json({
object: 'list',
has_more: paginate.hasNextPages(req)(pageCount),
data: users
})
}
})
})
})*/
app.use(function(req, res) {
res.status(404);
res.render("404");
});
/*app.get('*//*', function(req, res, next) {
if (req.headers.host.match(/^www/) !== null ) {
res.redirect(301, 'http://' + req.headers.host.replace(/^www\./, '') + req.url);
} else {
next();
}
});*/
/*app.use(function(req, res, next) {
if(req.url.substr(-1) == '/' && req.url.length > 1)
res.redirect(301, req.url.slice(0, -1));
else
next();
});*/
/*app.get('/covers', function (req, res, next) {
Cover.find({}, function (err, covers) {
if (err) return next(err);
res.json(covers);
});
});
app.get('/device', function (req, res, next) {
res.render('device');
});*/
function paginateCovers (current, index, n, res, next) {
Cover.paginate({}, index, n, function (error, pageCount, paginatedResults, itemCount) {
if (error) console.log(error);
if (current < pageCount) {
var nextUrl = "/page/".concat((parseInt(current)+1).toString());
}
/*app.render('index', {brand: "Cover Me", next: nextUrl, cvrs: paginatedResults}, function(err, html) {
page = html;
});
console.log(page);
res.set('Content-Type', 'text/html');
res.send(page);
*/
res.render('page',{brand: "Cover Me", next: nextUrl, cvrs: paginatedResults});
next();
});
}
app.get('/robots.txt', function (req, res) {
res.type('text/plain');
res.send("User-agent: *\nDisallow: /page\nDisallow: /search\nDisallow: /auto\nSitemap: http://cvrme.com/sitemap.xml");
});
var sitemap = sm.createSitemap ({
hostname: 'http://cvrme.com',
cacheTime: 600000, // 600 sec - cache purge period
urls: [
{ url: ''},
{ url: '/upload'},
{ url: '/about'},
{ url: '/legal'}
]
});
app.get('/sitemap.xml', function(req, res) {
sitemap.toXML( function (xml) {
res.header('Content-Type', 'application/xml');
res.send( xml );
});
});
app.get('/', function (req, res, next) {
var currentPage = 1;
Cover.find({})
.limit(72)
.exec(function (err, covers) {
if (err) return next(err);
res.render('index', {brand: "Cover Me", next: "/page/2", cvrs: covers});
});
//var coverData ="";m
/*function getCovers (fn) {
}
getCovers (function (covers) {
console.log(covers);
});*/
/*var coverData = Cover.find({});
console.log(coverData);*/
//res.render('index', {brand: "Cover Me", next: 2, cvrs: coverData})
//paginateCovers(currentPage, currentPage, 72, res, next);
/*res.json(coverData);
res.end();*/
});
app.get('/page/:page', function (req, res, next) {
var currentPage = parseInt(req.params.page);
var currentIndex = currentPage + 8;
//paginateCovers(currentPage, currentIndex, 8, res, next);
Cover.paginate({}, currentIndex, 8, function (error, pageCount, paginatedResults, itemCount) {
if (error) console.log(error);
if (currentPage < pageCount) {
var nextUrl = "/page/".concat((parseInt(currentPage)+1).toString());
}
res.render('page',{brand: "Cover Me", next: nextUrl, cvrs: paginatedResults});
next();
});
});
app.get('/search', function (req, res, next) {
Cover.search({ query: req.query.q, size:50}, {hydrate: false}, function (err, results) {
res.render('search', {searchResults: results.hits.hits});
});
});
app.get('/auto', function (req, res, next) {
Cover.find().or([{artist: new RegExp('^'+req.query.q, "i")},{album: new RegExp('^'+req.query.q, "i")}]).sort({'artist': 1}).exec(function(err, covers) {
if (err) return next(err);
var artists = new Array
for (var i=0; i<covers.length; i++) {
artists[i]=covers[i].artist + " - " + covers[i].album;
}
res.send(artists);
});
});
/*app.get('/add', function (req, res, next) {
var coverData= {artist: 'ras', album: 'rasdva', year: '2008', sPicture: 'sm-rasdva.jpg', bPicture: 'rasdva.jpg'}
var cover = new mongoose.models.Cover(coverData);
cover.save();
res.send(coverData)
});*/
app.get('/upload', function (req, res){
res.render('upload');
});
app.post('/upload', function (req, res){
var form = new formidable.IncomingForm();
var artist;
var album;
var year;
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
artist = fields.artist;
album = fields.album;
year = fields.year;
console.log(fields)
});
form.on('end', function(fields, files) {
/* Temporary location of our uploaded file */
var temp_path = this.openedFiles[0].path;
/* The file name of the uploaded file */
//var file_name = this.openedFiles[0].name;
var random = Math.random().toString();
var file_name = crypto.createHash('sha1').update(this.openedFiles[0].name + random).digest('hex') + '.jpg';
/* Location where we want to copy the uploaded file */
//var covers_dir = 'public/img/';
//var cover_name = artist + ' - ' + album;
var new_location = 'uploads/';
fs.copy(temp_path, new_location + file_name, function(err) {
if (err) {
console.error(err);
} else {
console.log("success!");
resizeImage(600, new_location + file_name, "img");
resizeImage(300, new_location + file_name, "small");
}
});
var coverData = { artist: artist, album: album, year: year, sPicture: "small/" + file_name, bPicture: "img/" + file_name };
var cover = new mongoose.models.Cover(coverData);
cover.save();
});
});
app.get('/about', function (req, res){
res.render('about');
});
app.get('/legal', function (req, res){
res.render('legal');
});
http.createServer(app).listen(config.get('port'), function () {
log.info('Express server listening on port ' + config.get('port'));
});