Auto update DB during scan + bug fixes

This commit is contained in:
IrosTheBeggar 2017-10-14 17:15:13 -04:00
parent be469ded02
commit 3d97e536d2
5 changed files with 97 additions and 61 deletions

View File

@ -91,12 +91,14 @@ exports.setup = function(mstream, program){
albumArtDir: program.albumArtDir
}
const forkedScan = child.fork( fe.join(__dirname, 'database-default-manager.js'), [JSON.stringify(jsonLoad)]);
const forkedScan = child.fork( fe.join(__dirname, 'database-default-manager.js'), [JSON.stringify(jsonLoad)], {silent: true});
// TODO: Get data back from process and store it for the status API call
// forkedScan.stdout.on('data', (data) => {
// console.log(`stdout: ${data}`);
// });
forkedScan.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
mstreamReadPublicDB.loadDB();
});
// forkedScan.stderr.on('data', (data) => {
// console.log(`stderr: ${data}`);
// });

View File

@ -46,17 +46,30 @@ function getFileType(filename){
}
exports.getNumberOfFiles = function(username, callback){
if(fileCollection === null){
callback(0)
return;
}
var results = fileCollection.count({ 'user': username })
callback(results)
}
exports.setup = function (mstream, dbSettings){
// Metadata lookup
mstream.post('/db/metadata', function (req, res){
if(fileCollection !== null){
res.json({"filepath":relativePath, "metadata":{}});
return;
}
var relativePath = req.body.filepath;
var fullpath = fe.join(req.user.musicDir, relativePath);
var result = fileCollection.findOne({'filepath': fullpath});
if(!result){
res.json({"filepath":relativePath, "metadata":{}});
return;
}
res.json({
"filepath":relativePath,
"metadata":{
@ -157,6 +170,8 @@ exports.setup = function (mstream, dbSettings){
'name' : { '$eq' : playlistname}
}]
});
res.json({success: true});
});
// TODO: Re-implment search
@ -167,10 +182,13 @@ exports.setup = function (mstream, dbSettings){
mstream.get('/db/artists', function (req, res) {
var artists = {"artists":[]};
var results = fileCollection.find({'user' : { '$eq' : req.user.username}});
for(row of results){
if(artists.artists.indexOf(row.artist) === -1) {
artists.artists.push(row.artist)
if(fileCollection !== null){
var results = fileCollection.find({'user' : { '$eq' : req.user.username}});
for(row of results){
if(artists.artists.indexOf(row.artist) === -1) {
artists.artists.push(row.artist)
}
}
}
@ -179,40 +197,41 @@ exports.setup = function (mstream, dbSettings){
mstream.post('/db/artists-albums', function (req, res) {
var albums = {"albums":[]};
var results = fileCollection.find({
'$and': [{
'user' : { '$eq' : req.user.username}
},{
'artist' : { '$eq' : req.body.artist}
}]
});
if(fileCollection !== null){
var results = fileCollection.find({
'$and': [{
'user' : { '$eq' : req.user.username}
},{
'artist' : { '$eq' : req.body.artist}
}]
});
var store = [];
var store = [];
for(row of results){
if(store.indexOf(row.album) === -1) {
albums.albums.push({
name: row.album,
album_art_file: row.albumArtFilename
});
store.push(row.album);
for(row of results){
if(store.indexOf(row.album) === -1) {
albums.albums.push({
name: row.album,
album_art_file: row.albumArtFilename
});
store.push(row.album);
}
}
}
res.json(albums);
});
// TODO: DOESNT HANDLE USERS
mstream.get('/db/albums', function (req, res) {
var albums = {"albums":[]};
if(fileCollection !== null){
var results = fileCollection.find({'user' : { '$eq' : req.user.username}});
var store = [];
var results = fileCollection.find({'user' : { '$eq' : req.user.username}});
var store = [];
for(row of results){
if(store.indexOf(row.album) === -1) {
albums.albums.push({name: row.album, album_art_file: row.albumArtFilename})
store.push(row.album);
for(row of results){
if(store.indexOf(row.album) === -1) {
albums.albums.push({name: row.album, album_art_file: row.albumArtFilename})
store.push(row.album);
}
}
}
@ -220,32 +239,34 @@ exports.setup = function (mstream, dbSettings){
});
mstream.post('/db/album-songs', function (req, res) {
var results = fileCollection.find({
'$and': [{
'user' : { '$eq' : req.user.username}
},{
'album' : { '$eq' : req.body.album}
}]
});
var songs = [];
if(fileCollection !== null){
var results = fileCollection.find({
'$and': [{
'user' : { '$eq' : req.user.username}
},{
'album' : { '$eq' : req.body.album}
}]
});
for(row of results){
var relativePath = fe.relative(req.user.musicDir, row.filepath);
relativePath = relativePath.replace(/\\/g, '/');
for(row of results){
var relativePath = fe.relative(req.user.musicDir, row.filepath);
relativePath = relativePath.replace(/\\/g, '/');
songs.push({
"filepath": relativePath,
"metadata": {
"hash": row.hash,
"artist": row.artist,
"album": row.album,
"track": row.track,
"title": row.title,
"year": row.year,
"album-art": row.albumArtFilename,
"filename": fe.basename( row.filepath )
}
})
songs.push({
"filepath": relativePath,
"metadata": {
"hash": row.hash,
"artist": row.artist,
"album": row.album,
"track": row.track,
"title": row.title,
"year": row.year,
"album-art": row.albumArtFilename,
"filename": fe.basename( row.filepath )
}
})
}
}
res.json(songs);
});

View File

@ -3,6 +3,8 @@ const loki = require('lokijs');
const filesdb = new loki('files.db');
var fileCollection;
var saveCounter = 0;
// Add a collection to the database
// const fileCollection = filesdb.addCollection('files');
@ -73,6 +75,18 @@ exports.insertEntries = function(arrayOfSongs, username){
"user": username,
};
fileCollection.insert(doc);
saveCounter++;
if(saveCounter === 100){
saveCounter = 0;
filesdb.saveDatabase(function(err) {
if (err) {
console.log("error : " + err);
}
else {
console.log("database saved.");
}
});
}
}
resolve();

View File

@ -6,7 +6,7 @@ const publicIp = require('public-ip');
const semver = require('semver')
const superagent = require('superagent');
const currentVer = '0.5.1';
const currentVer = '0.6.0';
var apiKey;
const ddnsDomain = 'https://ddns.mstream.io';
let appIcon = null;

View File

@ -17,6 +17,7 @@
"license": "GPL-3.0",
"dependencies": {
"archiver": "^2.0.3",
"auto-launch": "^5.0.1",
"body-parser": "^1.15.1",
"commander": "^2.9.0",
"express": "^4.13.4",
@ -26,15 +27,13 @@
"mkdirp": "^0.5.1",
"music-metadata": "^0.8.4",
"nat-upnp": "^1.1.0",
"portscanner": "^2.1.1",
"public-ip": "^2.0.1",
"superagent": "^3.5.2",
"uuid": "^3.0.1",
"ws": "^1.1.1"
},
"devDependencies": {
"electron": "1.6.2",
"electron-rebuild": "^1.5.7",
"auto-launch": "^5.0.1",
"portscanner": "^2.1.1",
"superagent": "^3.5.2"
"electron": "1.6.15"
}
}