Skip to content

Commit

Permalink
Add propertyName to the attachment's defaultUrl
Browse files Browse the repository at this point in the history
- allows multiple attachments with the same styles

closes #1
  • Loading branch information
jonstorer committed May 13, 2014
1 parent a2cc464 commit ab93c9a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ var plugin = function(schema, options) {
var finishConversion = function(styleFilePath, atts, cb) {
var ext = path.extname(styleFilePath);
var filenameId = options.filenameId ? selfModel[options.filenameId] : selfModel.id;
var storageStylePath = '/' + options.directory + '/' + [filenameId,styleName].join( options.idAsDirectory ? "/":"-") + ext;
var storageStylePath = [ options.directory, propertyName, [ filenameId, styleName + ext].join( options.idAsDirectory ? '/':'-') ].join('/');
if(storageStylePath[0] != '/'){ storageStylePath = '/' = storageStylePath; }

fs.stat(styleFilePath, function(err, stats) {
if(err) return cb(err);
Expand Down
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
"author": "Firebase.co <npm@firebase.co> (http://www.firebase.co/)",
"name": "mongoose-attachments",
"description": "Mongoose.js Attachments plugin. Supports ImageMagick styles",
"keywords": ["mongoose", "s3", "imagemagick", "uploads", "attachments"],
"keywords": [
"mongoose",
"s3",
"imagemagick",
"uploads",
"attachments"
],
"version": "0.1.0",
"homepage": "https://github.com/firebaseco/mongoose-attachments",
"repository": {
Expand All @@ -11,12 +17,18 @@
},
"main": "index.js",
"scripts": {
"test": "make test"
"test": "npm run specs && node ./test/testFindImageMagickFormats.js",
"specs": "./node_modules/.bin/mocha test/**/*.spec.js"
},
"dependencies": {
"async": "0.1.x",
"imagemagick": "0.1.x"
},
"devDependencies": {
"mongoose": "^3.8.9",
"mocha": "^1.18.2",
"chai": "^1.9.1"
},
"engines": {
"node": "*"
}
Expand Down
41 changes: 41 additions & 0 deletions test/defaultUrl.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var mongoose = require('mongoose');
var expect = require('chai').expect;
var fs = require('fs');
var plugin = require('../lib/attachments');

var fakeProvider = function(){};
fakeProvider.prototype.getUrl = function(path){
return path;
};
fakeProvider.prototype.createOrReplace = function(attachment, next){
attachment.defaultUrl = this.getUrl(attachment.path);
next(null, attachment);
};

plugin.registerStorageProvider('fakeProvider', fakeProvider);

UserSchema = new mongoose.Schema({ });
UserSchema.plugin(plugin, {
directory: process.cwd() + '/public/users',
storage: { providerName: 'fakeProvider', options: { } },
properties: {
profile: { styles: { original: { } } },
avatar: { styles: { original: { } } }
}
});
var User = mongoose.model('User', UserSchema);

describe('path', function(){
it('adds the propertyName in the attached image path', function(done){
var user = new User({});
var path = { path: process.cwd() + '/test/fixtures/mongodb.png' };
user.attach('profile', path, function(err){
user.attach('avatar', path, function(err){
expect(user.avatar.original.defaultUrl).to.not.eql(user.profile.original.defaultUrl);
expect(user.avatar.original.defaultUrl).to.include('users/avatar/' + user.id + '-original.png');
expect(user.profile.original.defaultUrl).to.include('users/profile/' + user.id + '-original.png');
done();
})
});
});
});
Binary file added test/fixtures/mongodb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ab93c9a

Please sign in to comment.