no message
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
const Images = require('../modules/images');
|
||||
const Logger = require('../modules/logger');
|
||||
const Mongoose = require('mongoose');
|
||||
|
||||
const MessageSchema = new Mongoose.Schema({
|
||||
@@ -7,6 +9,44 @@ const MessageSchema = new Mongoose.Schema({
|
||||
"isUser" : { type: Boolean, default: false, required: true, index: true }
|
||||
});
|
||||
|
||||
MessageSchema.pre('findOneAndUpdate', function (next) {
|
||||
var message = this;
|
||||
|
||||
if (message.image && typeof message.image === 'object') {
|
||||
Images.saveMessageImage(message.image, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
message.image = filename;
|
||||
next();
|
||||
}
|
||||
})
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
MessageSchema.pre('save', function (next) {
|
||||
var message = this;
|
||||
|
||||
if (message.image && typeof message.image === 'object') {
|
||||
Images.saveMessageImage(message.image, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
message.image = filename;
|
||||
next();
|
||||
}
|
||||
})
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
const MessageModel = Mongoose.model('messages', MessageSchema);
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
const fs = require('fs');
|
||||
const Images = require('../modules/images');
|
||||
const Messages = require('../models/message');
|
||||
const Mongoose = require('mongoose');
|
||||
const ShortId = require('shortid');
|
||||
|
||||
const ATTACHMENT_STORE = '../images';
|
||||
const ATTACHMENT_STORE_PROFILE = '/profile';
|
||||
const ATTACHMENT_STORE_MESSAGE = '/message';
|
||||
const ATTACHMENT_SUFFIX_DETAIL = '_detail';
|
||||
const ATTACHMENT_SUFFIX_THUMBNAIL = '_thumbnail';
|
||||
|
||||
const ProfileSchema = new Mongoose.Schema({
|
||||
"order" : { type: Number, default: 0 },
|
||||
@@ -34,6 +27,88 @@ const ProfileSchema = new Mongoose.Schema({
|
||||
"messages" : [ { type: Messages.schema } ]
|
||||
});
|
||||
|
||||
ProfileSchema.pre('findOneAndUpdate', function (next) {
|
||||
var cnt = 0;
|
||||
|
||||
if (this.details && this.details.pic) {
|
||||
var pic = this.details.pic
|
||||
cnt = cnt + (typeof pic.detail === 'object' ? 1 : 0) + (typeof pic.thumb === 'object' ? 1 : 0);
|
||||
}
|
||||
|
||||
if (cnt > 0) {
|
||||
if (typeof pic.detail === 'object') {
|
||||
Images.saveProfileDetailImage(pic.detail, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
pic.detail = filename;
|
||||
cnt -= 1;
|
||||
if (cnt === 0) next();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof pic.thumb === 'object') {
|
||||
Images.saveProfileThumbnailImage(pic.thumb, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
pic.thumb = filename;
|
||||
cnt -= 1;
|
||||
if (cnt === 0) next();
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
ProfileSchema.pre('save', function (next) {
|
||||
var cnt = 0;
|
||||
|
||||
if (this.details && this.details.pic) {
|
||||
var pic = this.details.pic
|
||||
cnt = cnt + (typeof pic.detail === 'object' ? 1 : 0) + (typeof pic.thumb === 'object' ? 1 : 0);
|
||||
}
|
||||
|
||||
if (cnt > 0) {
|
||||
if (typeof pic.detail === 'object') {
|
||||
Images.saveProfileDetailImage(pic.detail, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
pic.detail = filename;
|
||||
cnt -= 1;
|
||||
if (cnt === 0) next();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof pic.thumb === 'object') {
|
||||
Images.saveProfileThumbnailImage(pic.thumb, (err, filename) => {
|
||||
if (err) {
|
||||
Logger.error('[MessageSchema.pre(save)] There was an error processing the message image. [' + err + ']', { error: err });
|
||||
}
|
||||
|
||||
if (filename) {
|
||||
pic.thumb = filename;
|
||||
cnt -= 1;
|
||||
if (cnt === 0) next();
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
const ProfileModel = Mongoose.model('profiles', ProfileSchema);
|
||||
|
||||
module.exports = {
|
||||
|
||||
56
modules/images.js
Normal file
56
modules/images.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const fs = require('fs');
|
||||
const ShortId = require('shortid');
|
||||
|
||||
const ATTACHMENT_STORE = '../images';
|
||||
const ATTACHMENT_STORE_PROFILE = '/profile';
|
||||
const ATTACHMENT_STORE_MESSAGE = '/message';
|
||||
const ATTACHMENT_SUFFIX_DETAIL = '_detail';
|
||||
const ATTACHMENT_SUFFIX_THUMBNAIL = '_thumbnail';
|
||||
|
||||
function generateFilename (filename, type = 'detail') {
|
||||
var re = /(?:\.([^.]+))?$/;
|
||||
var ext = re.exec(filename)[1];
|
||||
return shortid.generate() + (type === 'thumbnail' ? ATTACHMENT_SUFFIX_THUMBNAIL : ATTACHMENT_SUFFIX_DETAIL) + "." + ext;
|
||||
}
|
||||
|
||||
function processImage (data, context, type, callback) {
|
||||
var folder = ATTACHMENT_STORE + (context === 'profile' ? ATTACHMENT_STORE_PROFILE : ATTACHMENT_STORE_MESSAGE);
|
||||
var filename = generateFilename(data.imageFilename, type);
|
||||
|
||||
var data_url = data.image;
|
||||
var matches = data_url.match(/^data:.+\/(.+);base64,(.*)$/);
|
||||
var base64_data = matches[2];
|
||||
var buffer = new Buffer(base64_data, 'base64');
|
||||
|
||||
saveImage(folder + '/' + filename, buffer, callback);
|
||||
}
|
||||
|
||||
function saveImage (filename, data, callback = function(){}) {
|
||||
fs.writeFile(filename, data, function (err, stat) {
|
||||
if (err) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
callback(null, filename.substring(2));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
save: (data, filename, callback = function(){}) {
|
||||
saveImage(filename, data, callback);
|
||||
},
|
||||
|
||||
saveMessageImage: (data, callback) => {
|
||||
processImage(data, 'message', 'detail', callback);
|
||||
},
|
||||
|
||||
saveProfileDetailImage: (data, callback) => {
|
||||
processImage(data, 'profile', 'detail', callback);
|
||||
},
|
||||
|
||||
saveProfileThumbnailImage: (data, callback) => {
|
||||
processImage(data, 'profile', 'thumbnail', callback);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user