-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
119 lines (97 loc) · 2.38 KB
/
router.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
var options = require('./config.js').options;
// REST服务
var restify = require('restify');
var server = restify.createServer();
exports.startServer = function(port, callback) {
server.listen(port ? port : options.port, callback);
};
exports.stopServer = function(callback) {
server.close(callback);
};
var moment = require('moment');
var randomstring = require("randomstring");
var store = require('./store.js');
//------------------------------------------
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.get(/\/static\/?.*/, restify.serveStatic({
directory: __dirname,
default: 'index.html'
}));
server.get('/info', function(req, res, next) {
res.send({
version: options.version
});
return next();
});
server.get('/qiniu', function(req, res, next) {
var params = req.params;
var action = params['action'];
if (action == 'config') {
res.send(options.ueditorConfig);
} else if (action == 'listimage' || action == 'listfile') {
var start = parseInt(params['start'] || 0);
var size = parseInt(params['size'] || 10);
var storeParams = {
prefix: action == 'listimage' ? 'image/' : 'file/',
start: start,
limit: size
}
store.listQiniu(storeParams, function(err, ret) {
res.send(ret);
return next();
})
} else {
res.send();
}
return next();
});
server.post('/qiniu', function(req, res, next) {
var params = req.params;
var action = params['action'];
var key = '/' + moment().format('YYYYMMDD') + '/' + (+new Date()) + randomstring.generate(6);
switch (action) {
case 'uploadvideo':
key = 'video' + key;
break;
case 'uploadfile':
key = 'file' + key;
break;
default:
key = 'image' + key;
break;
}
if (action == 'uploadimage' || action == 'uploadvideo' || action == 'uploadfile') {
var file = req.files['upfile'];
if (!file) {
res.send();
return next();
}
if (action == 'uploadfile') {
key += '/' + file.name;
}
var storeParams = {
key: key,
filePath: file.path,
fileName: file.name
}
store.fileToQiniu(storeParams, function(err, ret) {
res.send(ret);
return next();
})
} else if (action == 'uploadscrawl') {
var data = req.params['upfile'];
if (!data) {
res.send();
return next();
}
var storeParams = {
key: key,
data: new Buffer(data, 'base64')
}
store.dataToQiniu(storeParams, function(err, ret) {
res.send(ret);
return next();
})
}
});