mirror of
https://github.com/IrosTheBeggar/mStream.git
synced 2025-10-27 07:31:02 +00:00
105 lines
2.3 KiB
JavaScript
105 lines
2.3 KiB
JavaScript
// These functions will take in JSON arrays of song data and then save that dat to the DB
|
|
const loki = require('lokijs');
|
|
var filesdb;
|
|
var fileCollection;
|
|
|
|
var saveCounter = 0;
|
|
|
|
// Add a collection to the database
|
|
// const fileCollection = filesdb.addCollection('files');
|
|
|
|
exports.setup = function(dbPath, callback){
|
|
filesdb = new loki(dbPath);
|
|
|
|
filesdb.loadDatabase({}, function(err) {
|
|
if (err) {
|
|
console.log("error : " + err);
|
|
}
|
|
else {
|
|
// console.log("database loaded.");
|
|
}
|
|
|
|
fileCollection = filesdb.getCollection("files");
|
|
if (fileCollection === null) {
|
|
// first time run so add and configure collection with some arbitrary options
|
|
fileCollection = filesdb.addCollection("files");
|
|
}
|
|
|
|
callback()
|
|
});
|
|
}
|
|
|
|
exports.savedb = function(callback){
|
|
filesdb.saveDatabase(function(err) {
|
|
if (err) {
|
|
console.log("error : " + err);
|
|
}
|
|
else {
|
|
// console.log("database saved.");
|
|
}
|
|
callback()
|
|
});
|
|
}
|
|
|
|
exports.getVPathFiles = function(vpath, callback){
|
|
var results = fileCollection.find({ vpath: vpath });
|
|
if(!results){
|
|
results = [];
|
|
}
|
|
callback(results);
|
|
}
|
|
|
|
/**
|
|
* @param arrayOfSongs
|
|
* @param vpath
|
|
* @return Promise
|
|
*/
|
|
exports.insertEntries = function(arrayOfSongs, vpath){
|
|
return new Promise(function(resolve, reject) {
|
|
while(arrayOfSongs.length > 0) {
|
|
var song = arrayOfSongs.pop();
|
|
|
|
var doc = {
|
|
"title": song.title,
|
|
"artist": song.artist,
|
|
"year": song.year,
|
|
"artist": song.artist,
|
|
"album": song.album,
|
|
"filepath": song.filePath,
|
|
"format": song.format,
|
|
"track": song.track.no,
|
|
"disk": song.disk.no,
|
|
"filesize": song.filesize,
|
|
"modified": song.modified,
|
|
"created": song.created,
|
|
"hash": song.hash,
|
|
"albumArtFilename": song.albumArtFilename,
|
|
"vpath": vpath,
|
|
};
|
|
|
|
fileCollection.insert(doc);
|
|
|
|
saveCounter++;
|
|
if(saveCounter === 100){
|
|
saveCounter = 0;
|
|
filesdb.saveDatabase(function(err) {
|
|
if (err) {
|
|
console.log("error : " + err);
|
|
}
|
|
else {
|
|
console.log("database saved.");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
resolve();
|
|
});
|
|
}
|
|
|
|
|
|
exports.deleteFile = function(path, callback){
|
|
fileCollection.findAndRemove({'filePath': { '$eq' : path }});
|
|
callback();
|
|
}
|