mirror of
https://github.com/IrosTheBeggar/mStream.git
synced 2025-10-27 07:31:02 +00:00
Check for null playlist values, don't scan until after boot
This commit is contained in:
parent
8e884bd77e
commit
7cda70f358
@ -3,17 +3,81 @@ const fe = require('path');
|
||||
const mstreamReadPublicDB = require('../db-read/database-public-loki.js');
|
||||
const winston = require('winston');
|
||||
|
||||
var bootScanGenerator;
|
||||
var isScanning = false;
|
||||
|
||||
function scanIt(directory, vpath, program, callback) {
|
||||
var parseFlag = false;
|
||||
|
||||
// Prepare JSON load for forked process
|
||||
const jsonLoad = {
|
||||
directory: directory,
|
||||
vpath: vpath,
|
||||
dbSettings: program.database_plugin,
|
||||
albumArtDir: program.albumArtDir,
|
||||
skipImg: program.database_plugin.skipImg ? true : false,
|
||||
saveInterval: program.database_plugin.saveInterval ? program.database_plugin.saveInterval : 250,
|
||||
pause: program.database_plugin.pause ? program.database_plugin.pause : false
|
||||
}
|
||||
|
||||
const forkedScan = child.fork(fe.join(__dirname, 'database-default-manager.js'), [JSON.stringify(jsonLoad)], { silent: true });
|
||||
winston.info(`File scan started on ${jsonLoad.directory}`);
|
||||
forkedScan.stdout.on('data', (data) => {
|
||||
try {
|
||||
const parsedMsg = JSON.parse(data, 'utf8');
|
||||
winston.info(`File scan message: ${parsedMsg.msg}`);
|
||||
// TODO: Ideally, if there are no changes to the DB we should not be reloading it. Ideally...
|
||||
if(parsedMsg.loadDB === true) {
|
||||
parseFlag = true;
|
||||
mstreamReadPublicDB.loadDB();
|
||||
}
|
||||
} catch (error) {
|
||||
winston.info(`File scan message: ${data}`);
|
||||
return;
|
||||
}
|
||||
});
|
||||
forkedScan.stderr.on('data', (data) => {
|
||||
winston.error(`File scan error: ${data}`);
|
||||
});
|
||||
forkedScan.on('close', (code) => {
|
||||
isScanning = false;
|
||||
if(parseFlag === false) {
|
||||
mstreamReadPublicDB.loadDB();
|
||||
}
|
||||
winston.info(`File scan completed with code ${code}`);
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
function* bootScan(program) {
|
||||
// Loop through folders
|
||||
for (let vpath in program.folders) {
|
||||
yield scanIt( program.folders[vpath].root, vpath, program, () => {
|
||||
bootScanGenerator.next();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function runScan(program) {
|
||||
// Check that scan is not already in progress
|
||||
if (isScanning === true) {
|
||||
return { error: true, message: 'Scan in Progress' }; // Need to return a status
|
||||
}
|
||||
|
||||
isScanning = true;
|
||||
bootScanGenerator = bootScan(program);
|
||||
bootScanGenerator.next();
|
||||
return { error: false, message: 'Scan Started' };
|
||||
}
|
||||
|
||||
|
||||
exports.setup = function (mstream, program) {
|
||||
// Load in API endpoints
|
||||
mstreamReadPublicDB.setup(mstream, program);
|
||||
|
||||
// Var that keeps track of DB scans going on
|
||||
var isScanning = false;
|
||||
|
||||
// Get db status
|
||||
mstream.get('/db/status', function (req, res) {
|
||||
mstream.get('/db/status', (req, res) => {
|
||||
// Get number of files in DB
|
||||
mstreamReadPublicDB.getNumberOfFiles(req.user.vpaths, function (numOfFiles) {
|
||||
mstreamReadPublicDB.getNumberOfFiles(req.user.vpaths, (numOfFiles) => {
|
||||
res.json({
|
||||
totalFileCount: numOfFiles,
|
||||
dbType: 'default',
|
||||
@ -23,87 +87,16 @@ exports.setup = function (mstream, program) {
|
||||
});
|
||||
|
||||
// Scan library
|
||||
mstream.get('/db/recursive-scan', function (req, res) {
|
||||
runScan();
|
||||
mstream.get('/db/recursive-scan', (req, res) => {
|
||||
runScan(program);
|
||||
res.status((scan.error === true) ? 555 : 200).json({ status: scan.message });
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function scanIt(directory, vpath, callback) {
|
||||
var parseFlag = false;
|
||||
|
||||
// Prepare JSON load for forked process
|
||||
const jsonLoad = {
|
||||
directory: directory,
|
||||
vpath: vpath,
|
||||
dbSettings: program.database_plugin,
|
||||
albumArtDir: program.albumArtDir,
|
||||
skipImg: program.database_plugin.skipImg ? true : false,
|
||||
saveInterval: program.database_plugin.saveInterval ? program.database_plugin.saveInterval : 250,
|
||||
pause: program.database_plugin.pause ? program.database_plugin.pause : false
|
||||
}
|
||||
|
||||
const forkedScan = child.fork(fe.join(__dirname, 'database-default-manager.js'), [JSON.stringify(jsonLoad)], { silent: true });
|
||||
winston.info(`File scan started on ${jsonLoad.directory}`);
|
||||
forkedScan.stdout.on('data', (data) => {
|
||||
try {
|
||||
const parsedMsg = JSON.parse(data, 'utf8');
|
||||
winston.info(`File scan message: ${parsedMsg.msg}`);
|
||||
// TODO: Ideally, if there are no changes to the DB we should not be reloading it. Ideally...
|
||||
if(parsedMsg.loadDB === true) {
|
||||
parseFlag = true;
|
||||
mstreamReadPublicDB.loadDB();
|
||||
}
|
||||
} catch (error) {
|
||||
winston.info(`File scan message: ${data}`);
|
||||
return;
|
||||
}
|
||||
});
|
||||
forkedScan.stderr.on('data', (data) => {
|
||||
winston.error(`File scan error: ${data}`);
|
||||
});
|
||||
forkedScan.on('close', (code) => {
|
||||
isScanning = false;
|
||||
if(parseFlag === false) {
|
||||
mstreamReadPublicDB.loadDB();
|
||||
}
|
||||
winston.info(`File scan completed with code ${code}`);
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Scan on startup
|
||||
function* bootScan() {
|
||||
// Loop through list of users
|
||||
for (let vpath in program.folders) {
|
||||
|
||||
yield scanIt( program.folders[vpath].root, vpath, () => {
|
||||
bootScanGenerator.next();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var bootScanGenerator;
|
||||
function runScan() {
|
||||
// Check that scan is not already in progress
|
||||
if (isScanning === true) {
|
||||
return { error: true, message: 'Scan in Progress' }; // Need to return a status
|
||||
}
|
||||
|
||||
// Lock user
|
||||
isScanning = true;
|
||||
|
||||
bootScanGenerator = bootScan();
|
||||
bootScanGenerator.next();
|
||||
|
||||
return { error: false, message: 'Scan Started' };
|
||||
}
|
||||
|
||||
runScan();
|
||||
exports.runAfterBoot = function (program) {
|
||||
runScan(program);
|
||||
|
||||
if (program.database_plugin.interval) {
|
||||
setInterval(() => runScan(), program.database_plugin.interval * 60 * 60 * 1000);
|
||||
setInterval(() => runScan(program), program.database_plugin.interval * 60 * 60 * 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -5,7 +5,7 @@ const winston = require('winston');
|
||||
// Loki Collections
|
||||
var filesdb;
|
||||
var fileCollection;
|
||||
var playlistColection;
|
||||
var playlistCollection;
|
||||
|
||||
function getAllArtistsForUser(user) {
|
||||
var artists = [];
|
||||
@ -62,10 +62,10 @@ function loadDB() {
|
||||
fileCollection = filesdb.getCollection('files');
|
||||
|
||||
// Initialize playlsits collection
|
||||
playlistColection = filesdb.getCollection('playlists');
|
||||
if (!playlistColection) {
|
||||
playlistCollection = filesdb.getCollection('playlists');
|
||||
if (!playlistCollection) {
|
||||
// first time run so add and configure collection with some arbitrary options
|
||||
playlistColection = filesdb.addCollection("playlists");
|
||||
playlistCollection = filesdb.addCollection("playlists");
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -138,7 +138,11 @@ exports.setup = function (mstream, program) {
|
||||
return res.status(500).json({ error: 'Missing Params' });
|
||||
}
|
||||
|
||||
playlistColection.insert({
|
||||
if(!playlistCollection) {
|
||||
return res.status(500).json({ error: 'Playlist DB Not Initiated' });
|
||||
}
|
||||
|
||||
playlistCollection.insert({
|
||||
name: req.body.playlist,
|
||||
filepath: req.body.song,
|
||||
user: req.user.username,
|
||||
@ -157,7 +161,12 @@ exports.setup = function (mstream, program) {
|
||||
if (!req.body.lokiid){
|
||||
return res.status(500).json({ error: 'Missing Params' });
|
||||
}
|
||||
playlistColection.findAndRemove({ '$loki': req.body.lokiid });
|
||||
|
||||
if (!playlistCollection){
|
||||
return res.status(500).json({ error: 'Playlist DB Not Initiated' });
|
||||
}
|
||||
|
||||
playlistCollection.findAndRemove({ '$loki': req.body.lokiid });
|
||||
res.json({ success: true });
|
||||
filesdb.saveDatabase(err => {
|
||||
if (err) {
|
||||
@ -168,11 +177,15 @@ exports.setup = function (mstream, program) {
|
||||
|
||||
// Save playlists
|
||||
mstream.post('/playlist/save', (req, res) => {
|
||||
if (!playlistCollection){
|
||||
return res.status(500).json({ error: 'Playlist DB Not Initiated' });
|
||||
}
|
||||
|
||||
const title = req.body.title;
|
||||
const songs = req.body.songs;
|
||||
|
||||
// Delete existing playlist
|
||||
playlistColection.findAndRemove({
|
||||
playlistCollection.findAndRemove({
|
||||
'$and': [{
|
||||
'user': { '$eq': req.user.username }
|
||||
}, {
|
||||
@ -183,7 +196,7 @@ exports.setup = function (mstream, program) {
|
||||
|
||||
while (songs.length > 0) {
|
||||
const song = songs.shift();
|
||||
playlistColection.insert({
|
||||
playlistCollection.insert({
|
||||
name: title,
|
||||
filepath: song,
|
||||
user: req.user.username,
|
||||
@ -207,7 +220,7 @@ exports.setup = function (mstream, program) {
|
||||
function getPlaylists(username) {
|
||||
const playlists = [];
|
||||
|
||||
const results = playlistColection.find({ 'user': { '$eq': username } });
|
||||
const results = playlistCollection.find({ 'user': { '$eq': username } });
|
||||
const store = [];
|
||||
for (let row of results) {
|
||||
if (store.indexOf(row.name) === -1) {
|
||||
@ -220,10 +233,14 @@ exports.setup = function (mstream, program) {
|
||||
|
||||
// Load a playlist
|
||||
mstream.post('/playlist/load', (req, res) => {
|
||||
if (!playlistCollection){
|
||||
return res.status(500).json({ error: 'Playlist DB Not Initiated' });
|
||||
}
|
||||
|
||||
const playlist = req.body.playlistname;
|
||||
const returnThis = [];
|
||||
|
||||
const results = playlistColection.find({
|
||||
const results = playlistCollection.find({
|
||||
'$and': [{
|
||||
'user': { '$eq': req.user.username }
|
||||
}, {
|
||||
@ -260,10 +277,14 @@ exports.setup = function (mstream, program) {
|
||||
|
||||
// Delete playlist
|
||||
mstream.post('/playlist/delete', (req, res) => {
|
||||
if (!playlistCollection){
|
||||
return res.status(500).json({ error: 'Playlist DB Not Initiated' });
|
||||
}
|
||||
|
||||
const playlistname = req.body.playlistname;
|
||||
|
||||
// Delete existing playlist
|
||||
playlistColection.findAndRemove({
|
||||
playlistCollection.findAndRemove({
|
||||
'$and': [{
|
||||
'user': { '$eq': req.user.username }
|
||||
}, {
|
||||
|
||||
@ -7,6 +7,8 @@ const fs = require('fs');
|
||||
const fe = require('path');
|
||||
const bodyParser = require('body-parser');
|
||||
|
||||
const dbStuff = require('./modules/db-management/database-master.js');
|
||||
|
||||
exports.logit = function (msg) { /* Nothing. This is for electron */ }
|
||||
|
||||
exports.addresses = {
|
||||
@ -181,7 +183,7 @@ exports.serveit = function (program) {
|
||||
// File Explorer API
|
||||
require('./modules/file-explorer.js').setup(mstream, program);
|
||||
// Load database
|
||||
require('./modules/db-management/database-master.js').setup(mstream, program);
|
||||
dbStuff.setup(mstream, program);
|
||||
// Transcoder
|
||||
// require("./modules/ffmpeg.js").setup(mstream, program);
|
||||
// Scrobbler
|
||||
@ -225,5 +227,7 @@ exports.serveit = function (program) {
|
||||
winston.error('Port Forwarding Failed. The server is running but you will have to configure your own port forwarding');
|
||||
}
|
||||
}
|
||||
|
||||
dbStuff.runAfterBoot(program);
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user