async.forEachOf(audios, function(audio, key, callback) {
Audio.getMetadata(audio.path, function(err, metadata) { // асинхронная
if (err) {
console.log(err);
return callback(err);
}
fullAudios.push(Audio.setMetadata(audio, metadata)); // return audio with metadata
callback();
});
}, function done(err) {
if (err) {
console.log(err);
}
console.log(fullAudios);
});
module.exports.getMetadates = async function getMetadates(audios) {
return await Promise.all(audios.filter((audio) => fs.existsSync(audio.path)).map(async function(audio) {
let stream = fs.createReadStream(audio.path);
let metadata = await getMetadataSync(stream);
return Audio.setMetadata(audio, metadata);
}));
}
async function getMetadataSync(file) {
return await new Promise ((resolve, reject) => {
musicMetadataReader(file, (err, metadata) => {
if (err) {
return reject(err);
}
if (metadata.picture.length > 0) {
metadata.picture[0].base64String = getDecodedPicture(metadata.picture[0]);
}
resolve(metadata);
});
});
}