-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploader.js
42 lines (37 loc) · 1.29 KB
/
uploader.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
//uploader
var http = require('http');
var formidable = require('formidable');
var static = require('node-static');
var file = new static.Server('.');
var fs = require('fs');
const { exec } = require('child_process');
http.createServer(function (req, res) {
if(req.url == '/fileupload'){
var form = new formidable.IncomingForm();
form.parse(req,function(err, fields, files){
if(err) console.log(err);
// console.log(files);
let audio = files.audioFile;
var oldpath = audio.path;
let ext = String(audio.type).split('/')[1];
let timestamp = (new Date()).getTime();
let newpath = `audio/${timestamp}`;
let ffmpeg = `ffmpeg.exe -i ${oldpath} -b:a 128k ${newpath}.mp3`;
// fs.rename(oldpath, newpath+ext, function(err){
// if(err) throw err;
// });
exec(ffmpeg, (err, stdout, stderr) => {
if (err) {
console.log(err);
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
res.write(newpath);
res.end();
});
});
}else{
file.serve(req, res);
}
}).listen(8080);
console.log('file uploader started at port 8080');