Mongoose-Attachments is an attachments plugin for Mongoose.js. It handles ImageMagick transformations for the following providers:
You're reading the documentation for the next release of Mongoose-Attachments, which should be 0.1.0. The current stable release is 0.0.4.
Currently, Mongoose-Attachments is undergoing restructuring as we are moving the different storage providers into submodules. If you plan to use 0.0.4, do make sure that you use the [documentation for 0.0.4] (https://github.com/heapsource/mongoose-attachments/blob/v0.0.4/README.md).
Note: Mongoose-Attachments is bundled with each provider.
The following example extends the 'Post' model to use attachments with a property called 'image' and three different styles.
var mongoose = require('mongoose');
var attachments = require('mongoose-attachments-aws2js');
var PostSchema = new mongoose.Schema({
title: String,
description: String
});
PostSchema.plugin(attachments, {
directory: 'achievements',
storage: {
providerName: 'aws2js',
options: {
key: '<key>',
secret: '<secret>',
bucket: '<bucket>'
}
},
properties: {
image: {
styles: {
original: {
// keep the original file
},
small: {
transform: function(image) {
return image
.resize(150, 150)
;
}
},
medium: {
transform: function(image) {
return image
.resize(120, 120)
;
}
},
medium_jpg: {
options: {
format: 'jpg' // this one changes the format of the image to jpg
}
}
}
}
}
});
var Post = mongoose.model('Post', PostSchema);
Assuming that the HTML form sent a file in a field called 'image':
app.post('/upload', function(req, res, next) {
var post = new mongoose.model('Post')();
post.title = req.body.title;
post.description = req.body.description;
post.attach('image', req.files.image, function(err) {
if(err) return next(err);
post.save(function(err) {
if(err) return next(err);
res.send('Post has been saved with file!');
});
})
});
var post = new mongoose.model('Post')();
post.title = 'Title of the Post';
post.description = 'Description of the Post';
post.attach('image', {
path: '/path/to/the/file.png'
}, function(err) {
if(err) return next(err);
post.save(function(err) {
if(err) return next(err);
console.log('Post has been Saved with file');
});
})
With mongoose-attachments-localfs.
var path = require('path');
var attachments = require('mongoose-attachments-localfs');
MySchema.plugin(attachments, {
directory: '/absolute/path/to/public/images',
storage : {
providerName: 'localfs'
},
properties: {
image: {
styles: {
original: {
// keep the original file
},
thumb: {
options: {
format: 'jpg'
},
transform: function(image) {
return image
.thumbnail(100, 100)
.gravity('center')
.extend(100, 100)
}
},
detail: {
options: {
format: 'jpg'
},
transform: function(image) {
return image
.resize(400, 400, '>')
}
}
}
}
}
});
MySchema.virtual('detail_img').get(function() {
return path.join('detail', path.basename(this.image.detail.path));
});
MySchema.virtual('thumb_img').get(function() {
return path.join('thumb', path.basename(this.image.thumb.path));
});
The URL to the images would then be http://<your host>/<mount path>/images
prepended to the value of MyModel.detail_img
and MyModel.thumb_img
.
When mongoose-attachments is used with images, it can provide basic information for each one of the specified styles:
Example:
{
"dims" : {
"w" : 120,
"h" : 103
},
"depth" : 8,
"format" : "PNG",
"oname" : "dragon.png",
"mtime" : ISODate("2012-05-22T06:21:53Z"),
"ctime" : ISODate("2012-05-22T06:21:53Z"),
"size" : 26887,
"path" : "/achievements/4fbaaa31db8cec0923000019-medium.png",
"defaultUrl" : "http://gamygame-dev.s3.amazonaws.com/achievements/4fbaaa31db8cec0923000019-medium.png"
}
Transformations are achieved using the gm library.
styles: {
original: {
// no transformations
}
}
More information about 'blur' at the [ImageMagick website] http://www.imagemagick.org/script/command-line-options.php#blur
You can specify a format option to change the format of the output.
Example:
styles: {
as_jpeg: {
options: {
format: 'jpg'
}
}
}
Note: DO NOT include the dot in the extension.
There are two possibilities to define which file formats should be supported:
- white list (default)
- formats listed with certain flags by
convert -list format
The default white list contains:
- PNG
- GIF
- TIFF
- JPEG
To add a format call the following method before using the plugin in the mongoose schema:
attachments.registerDecodingFormat('BMP');
Copyright (c) 2011-2013 Firebase.co - http://firebase.co
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.