diff --git a/app.js b/app.js index 486da9c1..44cc4199 100644 --- a/app.js +++ b/app.js @@ -27,8 +27,11 @@ app.io.set('authorization', function(handshakeData, accept) { accept(null, true); }); +// fetch the config directory +app.set('configDir', process.env.configDir || __dirname); + // make sure the dbs directory is present -mkdirp(__dirname + '/dbs/covers', function() { +mkdirp(app.get('configDir') + '/dbs/covers', function() { // attach the db to the app require(__dirname + '/db.js')(app); diff --git a/config.js b/config.js index a756d2ca..208cba48 100644 --- a/config.js +++ b/config.js @@ -31,14 +31,8 @@ function Config(app) { // based on iTunes defaults https://support.apple.com/en-au/HT1391 var home_dir = getUserHome(); - // platform - if (home_dir.indexOf('C:') === 0) { - // windows - this.music_dir = path.join(home_dir, 'My Music'); - } else { - // Mac or Linux - this.music_dir = path.join(home_dir, 'Music'); - } + // this works on all platforms + this.music_dir = path.join(home_dir, 'Music'); // execute the two db calls in parrallel async.parallel([ diff --git a/db.js b/db.js index 3244dcd0..dcb6488f 100644 --- a/db.js +++ b/db.js @@ -2,7 +2,7 @@ var Datastore = require('nedb'); module.exports = function(app) { app.db = {}; - app.db.songs = new Datastore({ filename: __dirname + '/dbs/songs.db', autoload: true }); - app.db.playlists = new Datastore({ filename: __dirname + '/dbs/playlists.db', autoload: true }); - app.db.settings = new Datastore({ filename: __dirname + '/dbs/settings.db', autoload: true }); + app.db.songs = new Datastore({ filename: app.get('configDir') + '/dbs/songs.db', autoload: true }); + app.db.playlists = new Datastore({ filename: app.get('configDir') + '/dbs/playlists.db', autoload: true }); + app.db.settings = new Datastore({ filename: app.get('configDir') + '/dbs/settings.db', autoload: true }); }; diff --git a/electron.js b/electron.js index c086c8cf..f61bde23 100644 --- a/electron.js +++ b/electron.js @@ -20,6 +20,9 @@ electronApp.on('ready', function() { // disable the menu entirely mainWindow.setMenu(null); + // set the config directory in the users applicatin settings location + process.env.configDir = electronApp.getPath('userData'); + // init the server var app = require(__dirname + '/app.js'); diff --git a/library_functions.js b/library_functions.js index 0833b474..385e0b08 100644 --- a/library_functions.js +++ b/library_functions.js @@ -174,7 +174,7 @@ function findSong(relative_location, callback) { pic = result.picture[0]; pic.format = pic.format.replace(/[^a-z0-9]/gi, '_').toLowerCase(); song.cover_location = md5(pic.data) + '.' + pic.format; - filename = __dirname + '/dbs/covers/' + song.cover_location; + filename = app.get('configDir') + '/dbs/covers/' + song.cover_location; fs.exists(filename, function(exists) { if (!exists) { fs.writeFile(filename, pic.data, function(err) { @@ -326,9 +326,11 @@ exports.scanLibrary = function(hard) { // list with paths with music_dir removed var stripped = []; - for (var cnt = 0; cnt < list.length; cnt++) { - stripped.push(list[cnt].replace(app.get('config').music_dir, '')); - } + list.forEach(function(item) { + if (item) { + stripped.push(item.replace(app.get('config').music_dir, '')); + } + }); clearNotIn(stripped); if (!hard) { @@ -758,7 +760,7 @@ exports.sync_import = function(songs, url) { // is there a cover? if (songs[cnt].cover_location !== undefined) { - var cover_file_url = __dirname + '/dbs/covers/' + songs[cnt].cover_location; + var cover_file_url = app.get('configDir') + '/dbs/covers/' + songs[cnt].cover_location; request(url + '/cover/' + songs[cnt].cover_location).on('end', function() { // once the cover has finished transferring add the song to the database addSong(songs[cnt]); @@ -795,7 +797,7 @@ function saveID3(songData) { var options = {}; if (songData.cover_location) { // assign it in array format with 1 element - options.attachments = [path.join(app.get('root') + '/dbs/covers/' + songData.cover_location)]; + options.attachments = [path.join(app.get('configDir') + '/dbs/covers/' + songData.cover_location)]; } var destinationFile = path.join(app.get('config').music_dir, songData.location); @@ -816,7 +818,7 @@ function downloadCoverArt(url, callback) { request({url: url, encoding: null}, function(error, response, body) { // where are we storing the cover art? var cover_location = md5(body) + '.jpg'; - var filename = __dirname + '/dbs/covers/' + cover_location; + var filename = app.get('configDir') + '/dbs/covers/' + cover_location; // does it exist? fs.exists(filename, function(exists) { diff --git a/routes/index.js b/routes/index.js index 3bd50583..b64427c1 100644 --- a/routes/index.js +++ b/routes/index.js @@ -137,7 +137,7 @@ function sendSong(req, res) { function sendCover(req, res) { // if they passed the location of the cover, fetch it if (req.params.id.length > 0) { - res.sendFile(app.get('root') + '/dbs/covers/' + req.params.id); + res.sendFile(app.get('configDir') + '/dbs/covers/' + req.params.id); } else { res.sendFile(app.get('root') + '/static/images/unknown.png'); } @@ -374,7 +374,7 @@ function updateSongInfo(req) { // function to be called by both download file and file upload methods var process_cover = function(type, content_buffer) { var cover_filename = md5(content_buffer) + '.' + type; - var location = app.get('root') + '/dbs/covers/' + cover_filename; + var location = app.get('configDir') + '/dbs/covers/' + cover_filename; fs.exists(location, function(exists) { if (!exists) { fs.writeFile(location, content_buffer, function(err) {