-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (83 loc) · 3.09 KB
/
app.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
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
// eslint-disable-next-line
var colors = require('colors');
var config = require('./config.json');
var app = express();
app.use(bodyParser.text({type: 'text/html'}));
// GET carfi playlist folder
app.get('/carfi/', function(req, res) {
fs.readdir(config.musicFolderPath, function(err, musicFolder) {
var folderContents = '## Car-Fi Playlist ##\n';
for (var i in musicFolder) {
folderContents += musicFolder[i] + '\n';
}
folderContents += '\nTotal: ' + musicFolder.length + '\n'
+ 'Last updated: ' + new Date(Date.now() + 18000000).toLocaleString();
res.end(folderContents);
});
});
// GET carfi playlist folder
app.get('/carfi/car', function(req, res) {
fs.readFile('carfi.car', function(err, currentCarfiPlaylist) {
res.end(currentCarfiPlaylist);
});
});
var toDelete = function(contents) {
var playlistFiles = fs.readdirSync(config.musicFolderPath);
var deleteThose = '';
for (var song in contents) {
if (playlistFiles.indexOf(contents[song]) > -1 || contents[song] == ''
|| contents[song].indexOf('.mp3') === -1) {
console.log(contents[song] + ' found in files.'.green);
} else {
console.log('Delete: '.red + contents[song]);
deleteThose += contents[song] + '\n';
}
}
return deleteThose;
};
var toDownload = function(contents) {
var playlistFiles = fs.readdirSync(config.musicFolderPath);
var downloadThose = '';
for (var song in playlistFiles) {
if (contents.indexOf(playlistFiles[song]) > -1 || playlistFiles[song].indexOf('.mp3') === -1) {
console.log(playlistFiles[song] + ' already downloaded.'.grey);
} else {
console.log('Download: '.yellow + playlistFiles[song]);
downloadThose += playlistFiles[song] + '\n';
}
}
return downloadThose;
};
var saveCarFiPlaylist = function(contents) {
var currentCarfiPlaylist = '## What\'s in the car ##\n';
for (var song in contents) {
currentCarfiPlaylist += contents[song] + '\n';
}
currentCarfiPlaylist += '\nTotal: ' + contents.length + '\n'
+ 'Last updated: ' + new Date(Date.now() + 18000000).toLocaleString();
fs.writeFile('carfi.car', currentCarfiPlaylist, function(err) {
if (err) { return console.log(err); }
});
};
// POST carfi delete folder
app.post('/carfi/delete', function(req, res) {
var folderContents = req.body.split(';');
var deleteThose = toDelete(folderContents);
res.end(deleteThose);
});
// POST carfi delete folder
app.post('/carfi/download', function(req, res) {
var folderContents = req.body.split(';');
var downloadThose = toDownload(folderContents);
saveCarFiPlaylist(folderContents);
res.end(downloadThose);
});
app.use('/music', express.static('music'));
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Carfi app listening at http://%s:%s', host, port);
});