Fixed # bug and DB bug where some files would not delete

This commit is contained in:
IrosTheBeggar 2017-10-12 23:41:26 -04:00
parent 0f960f70fc
commit 05235fe560
4 changed files with 27 additions and 33 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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();
});
}

View File

@ -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
};