Skip to content

Instantly share code, notes, and snippets.

@ekmartin
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekmartin/a1fd70734095e7dd7b54 to your computer and use it in GitHub Desktop.
Save ekmartin/a1fd70734095e7dd7b54 to your computer and use it in GitHub Desktop.
var path = require('path');
var saveImage = function(article, image, fn) {
var ending = path.extname(image.originalFilename);
var unionPath = __dirname + '/../../public/images/unions/' + article.union;
var newPath = unionPath + '/' + article._id + ending;
var newPathCropped = unionPath + '/' + article._id + '_cropped' + ending;
mkdirp(unionPath, function(err) {
if (err) return fn(err);
gm(image.path)
.resize(500) // Resize to a width of 500px
.noProfile()
.write(newPathCropped, function(err) {
if (err) return fn(err);
article.imageCropped = newPathCropped;
fs.rename(image.path, newPath, function(err) {
if (err) return fn(err);
article.image = newPath;
article.imageName = image.originalFilename;
return fn(null, article);
});
});
});
};
exports.create = function(req, res) {
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
var parsedFields = {};
_.forOwn(fields, function(value, key) {
parsedFields[key] = value[0];
});
if (parsedFields.event) {
parsedFields.start = moment(parsedFields.start.slice(1, parsedFields.start.length-1)).toDate();
parsedFields.end = moment(parsedFields.end.slice(1, parsedFields.end.length-1)).toDate();
}
var article = new Article(parsedFields);
article.union = req.params.union;
function saveSend(err, article) {
if (err) console.log('Handle error.');
article.save(function(err) {
res.send(201, article);
});
}
if (!_.isEmpty(files)) {
saveImage(article, files.file[0], saveSend);
}
else saveSend(null, article);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment