From 7cda70f35819ec447ee8bef054cdb4e9205a7fb2 Mon Sep 17 00:00:00 2001 From: Paul Sori Date: Fri, 26 Oct 2018 17:09:59 -0400 Subject: [PATCH] Check for null playlist values, don't scan until after boot --- modules/db-management/database-master.js | 161 +++++++++++------------ modules/db-read/database-public-loki.js | 43 ++++-- mstream.js | 6 +- 3 files changed, 114 insertions(+), 96 deletions(-) diff --git a/modules/db-management/database-master.js b/modules/db-management/database-master.js index 3fc0290..e32e980 100644 --- a/modules/db-management/database-master.js +++ b/modules/db-management/database-master.js @@ -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); } -} +} \ No newline at end of file diff --git a/modules/db-read/database-public-loki.js b/modules/db-read/database-public-loki.js index 9af71e6..619e1da 100644 --- a/modules/db-read/database-public-loki.js +++ b/modules/db-read/database-public-loki.js @@ -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 } }, { diff --git a/mstream.js b/mstream.js index b4a20f9..2a9a33a 100755 --- a/mstream.js +++ b/mstream.js @@ -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); }); }