From 666e8a3f7c41d83a9df47f049822fdc02873a70e Mon Sep 17 00:00:00 2001 From: Paul Sori Date: Tue, 2 Oct 2018 18:41:06 -0400 Subject: [PATCH] more logging --- modules/db-management/database-master.js | 18 +++++++------- modules/db-read/database-public-loki.js | 22 ++++++++--------- modules/file-explorer.js | 30 ++++++++++++------------ 3 files changed, 36 insertions(+), 34 deletions(-) diff --git a/modules/db-management/database-master.js b/modules/db-management/database-master.js index 0beaf7f..ceb8b7e 100644 --- a/modules/db-management/database-master.js +++ b/modules/db-management/database-master.js @@ -1,6 +1,8 @@ const child = require('child_process'); const fe = require('path'); const mstreamReadPublicDB = require('../db-read/database-public-loki.js'); +require('../logger').init(); +const winston = require('winston'); exports.setup = function (mstream, program) { // Load in API endpoints @@ -23,7 +25,7 @@ exports.setup = function (mstream, program) { // Scan library mstream.get('/db/recursive-scan', function (req, res) { - var scan = runScan(); + runScan(); res.status((scan.error === true) ? 555 : 200).json({ status: scan.message }); }); @@ -32,7 +34,7 @@ exports.setup = function (mstream, program) { var parseFlag = false; // Prepare JSON load for forked process - var jsonLoad = { + const jsonLoad = { directory: directory, vpath: vpath, dbSettings: program.database_plugin, @@ -43,30 +45,30 @@ exports.setup = function (mstream, program) { } const forkedScan = child.fork(fe.join(__dirname, 'database-default-manager.js'), [JSON.stringify(jsonLoad)], { silent: true }); - console.log(`File scan started at ${Date.now()}`); + winston.info(`File scan started on ${jsonLoad.directory}`); forkedScan.stdout.on('data', (data) => { try { - var parsedMsg = JSON.parse(data, 'utf8'); - console.log(`stdout: ${parsedMsg.msg}`); + 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) { - console.log(`stdout: ${data}`); + winston.info(`File scan message: ${data}`); return; } }); forkedScan.stderr.on('data', (data) => { - console.log(`stderr: ${data}`); + winston.error(`File scan error: ${data}`); }); forkedScan.on('close', (code) => { isScanning = false; if(parseFlag === false) { mstreamReadPublicDB.loadDB(); } - console.log(`file scan completed with code ${code} at ${Date.now()}`); + winston.info(`File scan completed with code ${code}`); callback(); }); } diff --git a/modules/db-read/database-public-loki.js b/modules/db-read/database-public-loki.js index 010a019..6ceb58b 100644 --- a/modules/db-read/database-public-loki.js +++ b/modules/db-read/database-public-loki.js @@ -1,10 +1,10 @@ const fe = require('path'); -const crypto = require('crypto'); -// These functions will take in JSON arrays of song data and then save that dat to the DB const loki = require('lokijs'); -var filesdb; +require('../logger').init(); +const winston = require('winston'); // Loki Collections +var filesdb; var fileCollection = null; var playlistColection; @@ -87,7 +87,7 @@ function getAllAlbumsForUser(user) { function loadDB() { filesdb.loadDatabase({}, function (err) { if (err) { - console.log("error : " + err); + winston.error(`DB Load Error : ${err}`); } // Get files collection @@ -184,7 +184,7 @@ exports.setup = function (mstream, program) { // Save the DB filesdb.saveDatabase(function (err) { if (err) { - console.log("error : " + err); + winston.error(`DB Save Error : ${err}`); } }); }); @@ -219,7 +219,7 @@ exports.setup = function (mstream, program) { // Save the DB filesdb.saveDatabase(function (err) { if (err) { - console.log("error : " + err); + winston.error(`DB Save Error : ${err}`); } }); }); @@ -401,8 +401,8 @@ exports.setup = function (mstream, program) { res.status(500).json({ error: 'Bad input data' }); } - var rating = req.body.rating; - var pathInfo = program.getVPathInfo(req.body.filepath); + const rating = req.body.rating; + const pathInfo = program.getVPathInfo(req.body.filepath); if (pathInfo === false) { res.status(500).json({ error: 'Could not find file' }); return; @@ -413,7 +413,7 @@ exports.setup = function (mstream, program) { return; } - var result = fileCollection.findOne({ 'filepath': pathInfo.fullPath }); + const result = fileCollection.findOne({ 'filepath': pathInfo.fullPath }); if (!result) { res.status(500).json({ error: 'File not found in DB' }); return; @@ -423,9 +423,9 @@ exports.setup = function (mstream, program) { fileCollection.update(result); res.json({ success: true }); - filesdb.saveDatabase(function (err) { + filesdb.saveDatabase(err => { if (err) { - console.log("error : " + err); + winston.error(`DB Save Error : ${err}`); } }); }); diff --git a/modules/file-explorer.js b/modules/file-explorer.js index 368b96b..412e04c 100644 --- a/modules/file-explorer.js +++ b/modules/file-explorer.js @@ -1,6 +1,8 @@ const Busboy = require("busboy"); const fs = require("fs"); const fe = require("path"); +require('./logger').init(); +const winston = require('winston'); const masterFileTypesArray = ["mp3", "flac", "wav", "ogg", "aac", "m4a"]; @@ -24,7 +26,7 @@ exports.setup = function(mstream, program) { busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { const saveTo = fe.join(pathInfo.fullPath, filename); - console.log(`Uploading File: ${saveTo}`); + winston.info(`Uploading from ${req.user.username} to: ${saveTo}`); file.pipe(fs.createWriteStream(saveTo)); }); @@ -37,8 +39,8 @@ exports.setup = function(mstream, program) { // parse directories mstream.post("/dirparser", function(req, res) { - var directories = []; - var filesArray = []; + const directories = []; + const filesArray = []; // Return vpaths if no path is given if (req.body.dir === "" || req.body.dir === "/") { @@ -51,8 +53,8 @@ exports.setup = function(mstream, program) { return res.json({ path: "/", contents: directories }); } - var directory = req.body.dir; - let pathInfo = program.getVPathInfo(directory); + const directory = req.body.dir; + const pathInfo = program.getVPathInfo(directory); if (pathInfo == false) { res.status(500).json({ error: "Could not find file" }); return; @@ -64,10 +66,8 @@ exports.setup = function(mstream, program) { return; } - var path = pathInfo.fullPath; - // Make sure it's a directory - if (!fs.statSync(path).isDirectory()) { + if (!fs.statSync(pathInfo.fullPath).isDirectory()) { res.status(500).json({ error: "Not a directory" }); return; } @@ -81,12 +81,12 @@ exports.setup = function(mstream, program) { } // get directory contents - var files = fs.readdirSync(path); + const files = fs.readdirSync(pathInfo.fullPath); // loop through files for (let i = 0; i < files.length; i++) { try { - var stat = fs.statSync(fe.join(path, files[i])); + var stat = fs.statSync(fe.join(pathInfo.fullPath, files[i])); } catch (error) { // Bad file, ignore and continue continue; @@ -100,7 +100,7 @@ exports.setup = function(mstream, program) { }); } else { // Handle Files - var extension = getFileType(files[i]); + const extension = getFileType(files[i]); if ( fileTypesArray.indexOf(extension) > -1 && masterFileTypesArray.indexOf(extension) > -1 @@ -122,13 +122,13 @@ exports.setup = function(mstream, program) { }); // Format directory string for return value - directory = directory.replace(/\\/g, "/"); - if (directory.slice(-1) !== "/") { - directory += "/"; + const returnDirectory = directory.replace(/\\/g, "/"); + if (returnDirectory.slice(-1) !== "/") { + returnDirectory += "/"; } // Send back combined list of directories and mp3s - res.json({ path: directory, contents: directories.concat(filesArray) }); + res.json({ path: returnDirectory, contents: directories.concat(filesArray) }); }); function getFileType(filename) {