-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (71 loc) · 2.6 KB
/
script.js
File metadata and controls
75 lines (71 loc) · 2.6 KB
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
var express = require('express'),
_ = require('lodash'),
multer = require('multer'),
bodyParser = require('body-parser'),
upload = multer({dest:'uploads/'}),
fs = require('fs'),
util = require('util');
var app = express();
/**/
app.set('view engine','jade');
app.use(bodyParser.json());
app.use(function(error,req,res,next){
if (error instanceof SyntaxError){
res.status(400).json({"error":"Could not decode request: JSON parsing failed"});
}else{
next();
}
});
app.all('/',upload.single('file'),function(req,res,next){
var send = {"error":"Could not decode request: JSON parsing failed"};
var errJSON = {"error":"Could not decode request: JSON parsing failed"};
var data = {};
if(typeof req.file != "undefined"){
fs.exists(req.file.path,function(exists){
fs.readFile(req.file.path,'utf8',function(err,readFile){
if (err) res.status(400).type('application/json').json(send);
jsonParser(readFile,res,req,errJSON);
fs.unlink(req.file.path,function(err){
if(err) throw err;
console.log("removed the temp json file");
});
});
});
}else if(typeof req.body != 'undefined'){
jsonParser(req.body,res,req,errJSON);
}else{
res.status(400).type('application/json').json(send);
}
});
function jsonParser(json,res,req,errJSON){
try{
console.log('json');
json = (typeof json == 'string')? JSON.parse(json):json;
if(typeof json.payload != 'undefined'){
var mustkey = ["image","slug","title"];
var response = [];
var i =0;
for(item in json.payload){
var pick = _.pick(json.payload[item],mustkey);
var cur = response.length;
if(Object.keys(pick).length>0 && json.payload[i].drm == true && json.payload[i].episodeCount>0){
response[cur] = pick;
if(typeof response[cur].image!="undefined" && typeof response[cur].image.showImage!="undefined"){
response[cur].image = response[cur].image.showImage;
}
}
i++;
}
res.json({response:response});
}else{
res.status(400).type('application/json').json(errJSON);
}
}catch(e){
res.json(errJSON);
}
}
var server = app.listen(9002, function () {
var host = server.address().address;
var port = server.address().port;
console.log('mi9 app listening at http://%s:%s', host, port);
});