-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
151 lines (135 loc) · 4.33 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
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
142
143
144
145
146
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var swig = require('swig');
var http = require('http').Server(app);
var fs = require('fs');
var Datastore = require('nedb');
var ms = require('mediaserver');
var getFiles = require("./exts.js");
var io = require('socket.io')(http);
var mm = require('musicmetadata');
var readlineSync = require('readline-sync');
var open = require('open');
var db_audio = new Datastore({ filename: './db_audio', autoload: true });
var db_image = new Datastore({ filename: './db_image', autoload: true });
var db_video = new Datastore({ filename: './db_video', autoload: true });
var db_playlist = new Datastore({ filename: './db_playlist', autoload: true });
var user = readlineSync.question('user: ');
open('http://localhost:1997');
app.use(express.static('static'));
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
db_audio.ensureIndex({ fieldName: 'path', unique: true }, function (err) {
});
db_video.ensureIndex({ fieldName: 'path', unique: true }, function (err) {
});
db_image.ensureIndex({ fieldName: 'path', unique: true }, function (err) {
});
getFiles.find("/home/" + user + "/", function(type, file, name){
if(type === "audio"){
var parser = mm(fs.createReadStream(file), {duration: true}, function (err, tags) {
if (err) throw err;
if(tags.artist.length === 0){tags.artist = ["Unknown Artist"]};
if(tags.genre.length === 0){tags.genre = ["Unknown Genre"]};
if(tags.album == ""){tags.album = "Unknown Album"};
if(tags.year == ""){tags.year = "Unknown Year"};
if(tags.picture[0] !== undefined){
tags.picture[0].data = new Buffer(tags.picture[0].data, 'binary').toString('base64');
}
//console.log(tags);
var doc = {
'path' : file,
'title' : file.split("/")[file.split("/").length - 1].split('.')[0],
'album' : tags.album,
'artist' : tags.artist,
'year' : tags.year,
'genre' : tags.genre,
'duration' : Math.floor(tags.duration),
'pic' : tags.picture[0]
};
db_audio.insert(doc, function(err){
if(err){
if(err.errorType == 'uniqueViolated'){
console.log("asdf");
db_audio.update({path : doc.path}, doc, {}, function(err, numReplaced){
if(!err){
console.log("replaced object");
}
else console.log(err);
});
}
}
});
});
}
else if(type === "video"){
var doc = {
'path' : file,
'name' : name.split(".")[0],
'album' : file.split("/")[file.split("/").length - 2]
}
db_video.insert(doc, function(err){
if(err)console.log(err);
});
}
else if(type === "image"){
var doc = {
'path' : file,
'name' : name.split(".")[0],
'album' : file.split("/")[file.split("/").length - 2]
};
db_image.insert(doc, function(err){
if(err)console.log(err);
});
}
});
http.listen(1997, function(){
console.log("Started listening on port " + 1997);
});
app.get('/', function(req,res){
res.send(swig.renderFile('./templates/index.html'));
});
app.get('/play/:id',function(req,res){
var aud = db_audio.find({_id: req.params.id}, function(err,docs){
// We replaced all the event handlers with a simple call to readStream.pipe()
ms.pipe(req,res,docs[0].path);
console.log("requested file: " + docs[0].path);
});
});
io.on('connection', function (socket) {
console.log("socket io connection ");
db_audio.find({}).sort({ name: 1 }).exec(function (err, docs){
for(var i = 0; i < docs.length; i++){
try {
fs.accessSync(docs[i].path, fs.F_OK);
console.log("sending audio");
socket.emit('audio', docs[i]);
//console.log(docs[i]);
// Do something
} catch (e) {
console.log("error:");
console.log(e);
db_audio.remove({ _id: docs[i]._id }, {}, function (err, numRemoved) {
// numRemoved = 1
});
// It isn't accessible
}
}
socket.emit('audio_done');
});
db_video.find({}).sort({ name: 1 }).exec(function (err, docs){
for(var i = 0; i < docs.length; i++){
socket.emit('video', docs[i]);
console.log("sending video");
}
socket.emit('video_done');
});
db_image.find({}).sort({ name: 1 }).exec(function (err, docs){
for(var i = 0; i < docs.length; i++){
socket.emit('image', docs[i]);
console.log("sending image");
}
socket.emit('image_done');
});
});