2017-03-24 22:05:51 +01:00
|
|
|
// requirements
|
2017-03-24 22:11:22 +01:00
|
|
|
const path = require('path');
|
|
|
|
const async = require('async');
|
|
|
|
const fse = require('fs-extra');
|
2017-03-27 22:29:44 +02:00
|
|
|
const recursive = require('recursive-readdir');
|
2017-03-24 22:05:51 +01:00
|
|
|
const progress = require('progress');
|
|
|
|
|
|
|
|
// create path for target file
|
2017-03-27 22:29:44 +02:00
|
|
|
function getPathByMetadata(source, output, metadata, callback) {
|
2017-03-24 22:05:51 +01:00
|
|
|
// define directory
|
|
|
|
let filePath = path.normalize(output);
|
|
|
|
if (metadata.albumartist && metadata.albumartist.length > 0) {
|
|
|
|
let tmp;
|
|
|
|
const artistCount = metadata.albumartist.length;
|
|
|
|
for (let counter = 0; counter < artistCount; counter++) {
|
|
|
|
if (counter > 0) {
|
|
|
|
tmp += ' - ' + metadata.albumartist[counter];
|
|
|
|
} else {
|
|
|
|
tmp = metadata.albumartist[counter];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filePath = path.join(filePath, tmp);
|
|
|
|
} else {
|
|
|
|
filePath = path.join(filePath, metadata.artist[0]);
|
|
|
|
}
|
|
|
|
if (metadata.album) {
|
|
|
|
filePath = path.join(filePath, metadata.album);
|
|
|
|
}
|
|
|
|
// define filename
|
|
|
|
let fileName = '';
|
|
|
|
if (metadata.disk.no) {
|
|
|
|
fileName += frontFill(metadata.disk.no, '0', 2);
|
|
|
|
}
|
|
|
|
if (metadata.track.no) {
|
|
|
|
if (fileName) {
|
|
|
|
fileName += '-' + frontFill(metadata.track.no, '0', 2) + ' ';
|
|
|
|
} else {
|
|
|
|
fileName += frontFill(metadata.track.no, '0', 2) + ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (metadata.artist) {
|
|
|
|
fileName += metadata.artist[0] + ' - ';
|
|
|
|
}
|
|
|
|
if (metadata.title) {
|
|
|
|
fileName += metadata.title;
|
|
|
|
}
|
|
|
|
// append extension
|
2017-03-27 22:29:44 +02:00
|
|
|
fileName += path.extname(source);
|
2017-03-24 22:05:51 +01:00
|
|
|
// join directory and name
|
|
|
|
callback(null, path.join(filePath, fileName));
|
2017-03-24 22:56:05 +01:00
|
|
|
}
|
2017-03-24 22:05:51 +01:00
|
|
|
|
|
|
|
// create target directory and move the file
|
2017-03-24 22:56:05 +01:00
|
|
|
function moveFile(source, target, callback) {
|
2017-03-24 22:05:51 +01:00
|
|
|
async.series([
|
|
|
|
function (asyncCallback) {
|
|
|
|
fse.mkdirs(path.dirname(target), asyncCallback);
|
|
|
|
},
|
|
|
|
function (asyncCallback) {
|
|
|
|
fse.move(source, target, asyncCallback);
|
|
|
|
}
|
|
|
|
], function (err, results) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
2017-03-24 22:56:05 +01:00
|
|
|
}
|
2017-03-24 22:05:51 +01:00
|
|
|
|
|
|
|
// fill a string beginning from the front
|
2017-03-24 22:56:05 +01:00
|
|
|
function frontFill(string, fill, length) {
|
2017-03-24 22:05:51 +01:00
|
|
|
while (string.toString().length < length) {
|
|
|
|
string = fill + string;
|
|
|
|
}
|
|
|
|
return string;
|
2017-03-24 22:56:05 +01:00
|
|
|
}
|
2017-03-24 22:05:51 +01:00
|
|
|
|
2017-03-27 22:29:44 +02:00
|
|
|
// list files in directory
|
|
|
|
function readDirRecursive(where, extension, callback) {
|
|
|
|
if (extension.indexOf('.') !== 0) {
|
|
|
|
extension = '.' + extension;
|
|
|
|
}
|
|
|
|
recursive(where, [ignoreFilter], callback);
|
|
|
|
function ignoreFilter(file, stats) {
|
|
|
|
return !stats.isDirectory() && extension.indexOf(path.extname(file)) === -1;
|
2017-03-24 22:05:51 +01:00
|
|
|
}
|
2017-03-24 22:56:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// api
|
2017-03-27 22:29:44 +02:00
|
|
|
exports.getPathByMetadata = getPathByMetadata;
|
2017-03-24 22:56:05 +01:00
|
|
|
exports.moveFile = moveFile;
|
|
|
|
exports.frontFill = frontFill;
|
2017-03-27 22:29:44 +02:00
|
|
|
exports.readDirRecursive = readDirRecursive;
|