-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (113 loc) · 3.78 KB
/
index.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
var fs = require('fs');
var child_process = require('child_process');
var express = require('express');
var morgan = require('morgan');
var multer = require('multer');
var bodyParser = require('body-parser');
var pg = require('pg');
var mkdirp = require('mkdirp');
var app = express();
/**
* File upload configuration of multer.
* Targt dir is './uploads' and file limit size 10MB.
*/
var upload = multer({
dest: './uploads/',
limits: {
fieldSize: '10MB'
}
}).single('file');
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev', {immediate: true}));
app.use(bodyParser.urlencoded({extended: false}));
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
response.render('pages/index');
});
app.get('/upload-debug', function(req, res) {
res.render('pages/upload-debug');
});
app.post('/upload', upload, function(req, res, next) {
var username = req.body.username;
var filter = req.body.filter;
var now = new Date();
var tmpPath = req.file.path;
var targetPath = './uploads/' + username + '/' + now.getTime() +
'-' + req.file.originalname;
var convertedPath = './uploads/' + username + '/' + 'converted-' +
now.getTime() + '-' + req.file.originalname + '.mp3';
mkdirp('./uploads/' + username, function(err, result) {
if (err) {
throw err;
}
fs.rename(tmpPath, targetPath, function(err) {
if (err) {
throw err;
}
res.send("File uploaded to " + targetPath);
child = child_process.execFile('python',
['./convert.py', targetPath, convertedPath, filter],
function(err, stdout, stderr) {
if (err) {
throw err;
}
console.log(stdout);
fs.unlink(targetPath, function(err) {
if (err) {
throw err;
}
});
});
});
});
});
app.get('/converted/:username/:filename', function(req, res, next) {
var filename = req.params.filename;
var username = req.params.username;
console.log("Download " + filename);
res.download('./uploads/' + username + '/' + filename);
});
function compareUserTime(u1, u2) {
var pattern = /converted-([0-9]+)-.+\.mp3/;
var time1 = pattern.exec(u1.filename);
var time2 = pattern.exec(u2.filename);
if (time1 != null && time2 != null) {
if (time1[1] > time2[1]) {
return -1;
} else {
return 1;
}
}
return 0;
}
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
String.prototype.startsWith = function(prefix) {
return this.indexOf(prefix) === 0;
}
app.get('/list', function(req, res, next) {
fs.readdir('./uploads', function(err, users) {
var ret = [];
for (var i = 0; i < users.length; i++) {
var userDir = fs.statSync('./uploads/' + users[i]);
if (userDir.isDirectory()) {
var username = users[i];
var filenames = fs.readdirSync('./uploads/' + users[i]);
for (var j = 0; j < filenames.length; j++) {
if (!filenames[j].endsWith("xml") && filenames[j].startsWith("converted")) {
ret.push({user: username, filename: filenames[j]});
}
}
}
}
ret.sort(compareUserTime);
res.json({users: ret});
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});