diff --git a/docs/cli_arguments.md b/docs/cli_arguments.md index d5524de..6e8621f 100644 --- a/docs/cli_arguments.md +++ b/docs/cli_arguments.md @@ -35,7 +35,7 @@ mstream -c /path/to/cert.pem -k /path/to/key.pem ``` ## User System -mStream can have a single user and guest. If the user is not set mStream will disable to the user system and anyone will be able to access the server +mStream can have a single user and guest. If the user is not set (default behaviour), mStream will permit unrestricted access to the system. ```shell # Set User diff --git a/modules/db-management/database-default-manager.js b/modules/db-management/database-default-manager.js index 9502b85..3bf219a 100644 --- a/modules/db-management/database-default-manager.js +++ b/modules/db-management/database-default-manager.js @@ -10,7 +10,7 @@ // "albumArtDir": "/album/art/dir" // } -const metadata = require('musicmetadata'); +const metadata = require('music-metadata'); const fs = require('fs'); const fe = require('path'); const crypto = require('crypto'); @@ -133,36 +133,41 @@ function parseFile(thisSong){ return; } - // Stores all data that needs to be added to DB - var songInfo; - - - - var readableStream = fs.createReadStream(thisSong); - var parser = metadata(readableStream, function (err, thisMetadata) { - readableStream.close(); - - if(err){ - // TODO: Do something - } - - songInfo = thisMetadata; - songInfo.filesize = filestat.size; - songInfo.created = filestat.birthtime.getTime(); - songInfo.modified = filestat.mtime.getTime(); - songInfo.filePath = thisSong; - songInfo.format = getFileType(thisSong); + // Parse the file for metadata and store it in the DB + return metadata.parseFile(thisSong).then(function (thisMetadata) { + var songInfo = thisMetadata.common; + songInfo.filesize = filestat.size; + songInfo.created = filestat.birthtime.getTime(); + songInfo.modified = filestat.mtime.getTime(); + songInfo.filePath = thisSong; + songInfo.format = getFileType(thisSong); + return songInfo; + }).then(function (songInfo) { + // Calculate unique DB ID + return calculateHash(thisSong, songInfo); + }).then(function (songInfo) { + // Stores metadata of song in the database + return dbRead.insertEntries([songInfo], loadJson.username) + }).then(function () { + // Continue with next file + parseFilesGenerator.next(); + }).catch(function (err) { + console.log("Warning: failed to parse file '%s': %s", thisSong, err.message); + }); +} +function calculateHash (thisSong, songInfo) { + return new Promise(function (resolve, reject) { // Handle album art - // TODO: handle cases where multiple images in metadata + // TODO: handle cases where multiple images in metadata var bufferString = false; var picFormat = false; - if(songInfo.picture[0]){ + if (songInfo.picture && songInfo.picture[0]) { bufferString = songInfo.picture[0].data.toString('utf8'); picFormat = songInfo.picture[0].format; // console.log(songInfo.picture); - }else if(false){ // TODO: Check if there is album art in base folder + } else if (false) { // TODO: Check if there is album art in base folder } @@ -189,10 +194,7 @@ function parseFile(thisSong){ } } - //console.log(songInfo); - dbRead.insertEntries([songInfo], loadJson.username, function(){ - parseFilesGenerator.next(); - }); + resolve(songInfo); }); readableStream2.pipe(hash); diff --git a/modules/db-write/database-default-sqlite.js b/modules/db-write/database-default-sqlite.js index ffd2e67..7f5d674 100644 --- a/modules/db-write/database-default-sqlite.js +++ b/modules/db-write/database-default-sqlite.js @@ -19,9 +19,12 @@ exports.getUserFiles = function(thisUser, callback){ }); } - - -exports.insertEntries = function(arrayOfSongs, username, callback){ +/** + * @param arrayOfSongs + * @param username + * @return Promise + */ +exports.insertEntries = function(arrayOfSongs, username){ var sql2 = "insert into items (title,artist,year,album,path,format, track, disk, user, filesize, file_modified_date, file_created_date, hash, album_art_file) values "; var sqlParser = []; @@ -70,8 +73,13 @@ exports.insertEntries = function(arrayOfSongs, username, callback){ sql2 = sql2.slice(0, -2); sql2 += ";"; - db.run(sql2, sqlParser, function() { - callback(); + return new Promise(function(resolve, reject) { + db.run(sql2, sqlParser, function(err) { + if(err) + reject(err); + else + resolve(); + }); }); } diff --git a/package.json b/package.json index 8b6dd32..57f7ca8 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "internal-ip": "^1.2.0", "jsonwebtoken": "^7.1.9", "lokijs": "^1.4.3", - "musicmetadata": "^2.0.3", + "music-metadata": "^0.7.14", "nat-upnp": "^1.1.0", "public-ip": "^2.0.1", "sqlite3": "3.1.8",