-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
114 lines (86 loc) · 2.64 KB
/
server.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
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const request = require('request');
const fs = require('fs');
const os = require('os');
const path = require('path');
const multer = require('multer');
const PromiseFtp = require('promise-ftp');
const mailer = require("nodemailer");
const app = express();
app.set('view engine', 'ejs');
const port = 8000;
app.use(multer({ dest: 'uploads/'}).single('mp3payload'));
app.get('/api/file', function(req, res) {
res.render('index')
})
app.post('/api/file', (req, res, next) => {
var upload = multer({
// storage: storage,
fileFilter: function(req, file, callback) {
const ext = path.extname(file.originalname)
if (ext !== '.mp3') {
return callback(res.end('Only mp3s are allowed'), null)
}
callback(null, true)
}
}).single('mp3payload');
const audioLocation = req.body.destination
const title = req.body.title;
upload(req, res, function(err) {
console.log(audioLocation, title)
ftpService(res, req.file.path, audioLocation, title )
})
})
app.post('/upload', (req, res) => {
const saveTo = path.join('songs', path.basename(req.body.filename+ID()+'.mp3'))
request({uri: req.body.url})
.pipe(fs.createWriteStream(saveTo))
.on('close', () => {
res.send('File save complete');
});
console.log(req.body);
});
app.listen(port, () => {
console.log('We are live on ' + port);
});
//helpers
const ID = () => '_' + Math.random().toString(36).substr(2, 4);
const ftpcreds = {
host: 'fillup',
user: 'fillup',
pass: 'fillup'
}
const ftpService = (res, filepath, folderLocation, title) => {
const ftp = new PromiseFtp();
ftp.connect(
{
host: ftpcreds.host,
user: ftpcreds.user,
password: ftpcreds.pass
}).then( (serverMessage) => {
// console.log('Server message: '+serverMessage);
return ftp.list('/gidiradio/uploader');
}).then((list) => {
console.log('Directory listing:');
list.map((item) => console.log(item.name))
const destinationFile = filepath.replace('uploads/','');
console.log('destination', destinationFile)
console.log('filepath', filepath)
console.log('folderLocation', folderLocation)
;
const finalDestination = `gidiradio/${folderLocation}/${title}${destinationFile}.mp3`
console.log(finalDestination)
ftp.put(filepath, finalDestination, (err)=> {
console.log(err)
err !== 'undefined' ? res.send('done') : console.log(err)
// err === 'undefined' ? console.log(err) : res.send('done')
})
return ftp.list();
})
};
const cleanString= (name) => {
name = name.replace(/\s+/gi, '-');
return name.replace(/[|&;$%@"<>()+,]/g, "");
}