diff --git a/modules/db-management/database-default-manager.js b/modules/db-management/database-default-manager.js index d40aa5a..82a876e 100644 --- a/modules/db-management/database-default-manager.js +++ b/modules/db-management/database-default-manager.js @@ -10,12 +10,7 @@ // "albumArtDir": "/album/art/dir" // } -const metadata = require('music-metadata'); -const fs = require('fs'); -const fe = require('path'); -const crypto = require('crypto'); - - +// Parse input JSON try{ var loadJson = JSON.parse(process.argv[process.argv.length-1], 'utf8'); }catch(error){ @@ -23,60 +18,62 @@ try{ process.exit(); } -// console.log(loadJson); +// Libraries +const metadata = require('music-metadata'); +const fs = require('fs'); +const fe = require('path'); +const crypto = require('crypto'); +// Setup DB layer +// The DB functions are dcoupled from this so they can easily be swapped out const dbRead = require('../db-write/database-default-'+loadJson.dbSettings.type+'.js'); if(loadJson.dbSettings.type == 'sqlite'){ dbRead.setup(loadJson.dbSettings.dbPath); } - -const parseFilesGenerator = rescanAllDirectories(loadJson.userDir); -parseFilesGenerator.next(); - -var globalCurrentFileList = {}; - +// Global Vars +var globalCurrentFileList = {}; // Map of file paths to metadata var listOfFilesToParse = []; var listOfFilesToDelete = []; +// Start the generator +const parseFilesGenerator = rescanAllDirectories(loadJson.userDir); +parseFilesGenerator.next(); - +// Scan the directory for new, modified, and deleted files function *rescanAllDirectories(directoryToScan){ - // Scan the directory for new, modified, and deleted files + // Pull filelist from DB yield pullFromDB(); - // console.log(globalCurrentFileList); - - // Loop through current files + // Loop through current files and compare them to the files pulled from the DB recursiveScan(directoryToScan); - //console.log(listOfFilesToParse); - + // Delete Files for (var i=0; i < listOfFilesToDelete.length; i++) { yield deleteFile(listOfFilesToDelete[i]); } - + // Delete all remaining files + for (var file in globalCurrentFileList) { + yield deleteFile(file); + } + // Parse and add files to DB for (var i=0; i < listOfFilesToParse.length; i++) { yield parseFile(listOfFilesToParse[i]); } - - // Exit process.exit(0); } +// Get all files form DB and add to globalCurrentFileList function pullFromDB(){ dbRead.getUserFiles(loadJson, function(rows){ - // console.log(rows); - for(var s of rows){ globalCurrentFileList[s.path] = s; } - + // Go to next step parseFilesGenerator.next(); }); } - function recursiveScan(dir, fileTypesArray){ var files = fs.readdirSync( dir ); @@ -96,7 +93,6 @@ function recursiveScan(dir, fileTypesArray){ // Make sure this is in our list of allowed files var extension = getFileType(files[i]); var fileTypesArray = ["mp3", "flac", "wav", "ogg", "aac", "m4a"]; - if (fileTypesArray.indexOf(extension) === -1 ) { continue; } @@ -105,7 +101,6 @@ function recursiveScan(dir, fileTypesArray){ if (!(filepath in globalCurrentFileList)){ // if not parse new file, add it to DB, and continue listOfFilesToParse.push(filepath); - // yield parseFile(filepath); continue; } diff --git a/modules/db-read/database-public-sqlite.js b/modules/db-read/database-public-sqlite.js index 99376b3..95b04ae 100644 --- a/modules/db-read/database-public-sqlite.js +++ b/modules/db-read/database-public-sqlite.js @@ -58,7 +58,7 @@ exports.setup = function (mstream, dbSettings){ db.run(playlistSql, function() {}); - // Finish and test this + // Metadata lookup mstream.post('/db/metadata', function (req, res){ var relativePath = req.body.filepath; var fullpath = fe.join(req.user.musicDir, relativePath); diff --git a/modules/db-write/database-default-sqlite.js b/modules/db-write/database-default-sqlite.js index 345db93..fd015d8 100644 --- a/modules/db-write/database-default-sqlite.js +++ b/modules/db-write/database-default-sqlite.js @@ -99,7 +99,6 @@ exports.purgeDB = function(){ exports.deleteFile = function(path, user, callback){ let sql = "DELETE FROM items WHERE path = ? AND user = ?;"; db.run(sql, [path, user], function() { - console.log('ITS DONE'); callback(); }); } diff --git a/public/js/api2.js b/public/js/api2.js index e0c7ad9..a8332ee 100644 --- a/public/js/api2.js +++ b/public/js/api2.js @@ -136,7 +136,7 @@ var MSTREAMAPI = (function () { // Special helper function MSTREAMPLAYER.addSongWizard = function(filepath, metadata, lookupMetadata){ // Escape filepath - filepath = filepath.replace("#", "%23"); + escapedFilepath = filepath.replace("#", "%23"); var url = mstreamModule.currentServer.host + filepath; if(mstreamModule.currentServer.vPath){ @@ -149,7 +149,7 @@ var MSTREAMAPI = (function () { var newSong = { url: url, - filepath: filepath, + filepath: escapedFilepath, metadata: metadata };