Merge pull request #38 from Borewit/promise-based-parsing

Promise based storing and parsing of metadata.
This commit is contained in:
Paul 2017-09-16 17:22:55 -04:00 committed by GitHub
commit 77927686dc
4 changed files with 44 additions and 34 deletions

View File

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

View File

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

View File

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

View File

@ -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",