Skip to content

Commit

Permalink
fixes #90, use user config directory for settings so binary can be up…
Browse files Browse the repository at this point in the history
…dated independently
  • Loading branch information
benkaiser committed Jan 19, 2016
1 parent e5daa50 commit 23af43e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
5 changes: 4 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 2 additions & 8 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
6 changes: 3 additions & 3 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
};
3 changes: 3 additions & 0 deletions electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
16 changes: 9 additions & 7 deletions library_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 23af43e

Please sign in to comment.