router.post('/api/auth/sign-up', async (ctx, next) => {
const user = new UserModel({
['info.name']: ctx.request.body.username,
email: ctx.request.body.email,
password: ctx.request.body.password
});
await user
.save()
.catch(err => {
ctx.throw(403, "Cannot create user or user is already created!");
});
ctx.body = user;
await next();
});
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');
"dialogues" : {
"58f9e5ff408da70504fede05" : [ // <---- человек по-переписке
{
"id" : ObjectId("590880f1c612893834123438") // <---- id сообщения, здесь могут быть дополнительные
}, // параметры. Например, скрыл ли его пользователь.
...
],
"58f9fcb6794ac63bf44bd61c" : [
{
"id" : ObjectId("5908a3470d59a45a64715bb2")
},
...
]
}
dialogues: { }
User.findByIdAndUpdate(
userId,
{$push: {['dialogues.' + message.recipient]: {id: message._id}} },
{ safe: true, upsert: true },
function(err, user) {
if (err) console.log(err);
}
);