constructor(recipeDetails: Details = { title: "", description: "", ingredients: [] }) {
for (let key in recipeDetails) {
(this[key as keyof Details] as Details[keyof Details]) = recipeDetails[key as keyof Details];
}
}
interface Details { title?: string, description?: string, ingredients?: RecipeIngredient[]}
module.exports = {
entry: './main.js',
output: {
filename: 'build.js'
},
module: {
rules: [{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: ['es2015', 'react']
}
}
}]
},
resolve: {
modules: ['node_modules'],
extensions: [".js", ".json", ".jsx", ".css"]
}
}
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);
});
});
}
new Buffer(metadata.picture[0].data).toString('base64')
<Buffer ff d8 ff e1 00 a1 45 78 69 66 00 00 49 00 08 00 00 4a 00 00 00 32 01 02 00 ... >
"data:image/" + picture.format + ";base64," + new Buffer(picture.data).toString('base64');