-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
145 lines (114 loc) · 3.94 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Created by Anwender on 05.04.2017.
*/
//Todo: Song hochladen koennen
//Todo: Songinfos editieren koennen
//Todo: Now Playing sollte im Menue angezeigt werden, nicht in der aktuellen view
//Todo: Playlists
var player = require("./Player.js");
console.log("Player Imported");
var songbase = require("./Songbase.js");
songbase.load();
songbase.checkNewSongs();
//songbase.save();
console.log(songbase.getSongList());
var express = require('express');
var http = require('http').Server(app);
var app = express();
var multer = require('multer')
var upload = multer({dest: 'uploads/'})
// For debug
var morgan = require('morgan');
app.use(morgan('dev'));
// End debug
app.post('/songUpload', upload.single('song'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
console.log("upload: " + req.file.originalname);
var clientID = req.body.clientID;
console.log("Client id: " + clientID);
songbase.importUploadedSong(req.file.path, req.file.originalname, function (song) {
io.to(clientID).emit('event:songImported', song);
});
res.send("ok");
});
app.get('/api/v01/saveEditedSong', function (req, res, next) {
//console.log(req);
var songChanges = req.query;
if (songChanges) {
var song = songbase.getSongByID(songChanges.id);
if (songChanges.title != null && songChanges.title !== "") {
song.title = songChanges.title;
}
if (songChanges.artist != null && songChanges.artist !== "") {
song.artist = songChanges.artist;
}
if (songChanges.album != null && songChanges.album !== "") {
song.album = songChanges.album;
}
songbase.overrideSong(song);
res.send("ok");
} else {
res.status(400);
res.send("error");
}
});
app.get('/songlist.json', function (req, res) {
res.send(songbase.getSongList());
});
app.get('/api/v01/getSongQueue', function (req, res) {
console.log(player.getQueue());
res.send(player.getQueue());
});
app.get('/api/v01/getSongById', function (req, res) {
var id = req.query.id;
if (id != null) {
var song = songbase.getSongByID(id);
res.send(song);
} else {
res.send(null);
}
});
app.get('/api/v01/getCurrentSong', function (req, res) {
res.send(player.getCurrentSong());
});
app.use('/', express.static('app'));
var playSong = function (song) {
console.log("Add Song to now playing: " + song.title);
player.addSong(song);
};
var server = app.listen(8000, function () {
console.log("Express server listening on port %d ", server.address().port);
});
var io = require('socket.io')(server);
player.setNextSongCallback(function (song) {
song = song || null;
console.log("event: " + song);
io.emit('nowPlaying', song);
});
player.setQueueChangeCallback(function (queue) {
// console.log(queue);
io.emit('event:songQueueChanged', queue);
});
io.on('connection', function (socket) {
socket.on('command:addToQueue', function (songID) {
var song = songbase.getSongByID(songID);
// console.log('Play Song ' + songID+ ': '+song.title);
playSong(song);
});
socket.on('command:reorderSongInQueue', function (songPositions) {
if (songPositions != null && songPositions.oldQueueID != null && songPositions.newQueueID != null) {
console.log("change queue position old: " + songPositions.oldQueueID + " new : " + songPositions.newQueueID);
player.changeQueuePosition(songPositions.oldQueueID, songPositions.newQueueID);
}
});
socket.on('command:removeFromQueue', function (queueID) {
console.log("remove from queue: " + queueID);
player.removeSongFromQueue(queueID);
});
socket.on('disconnect', function () {
console.log('user disconnected!');
});
io.emit('nowPlaying', player.getCurrentSong());
console.log('a user connected');
});