Created
May 29, 2015 14:52
-
-
Save lardissone/08712dcff73ccdccd025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = exports = function relativeImageUrl(schema, options) { | |
var relField = options.relativeUrlField || 'relUrl', | |
field = {}; | |
field[relField] = String; | |
schema.add(field); | |
schema.pre('save', function(next) { | |
var photoField = options.absoluteUrlField || 'large', | |
removeStr = process.env.PWD + '/client', | |
absUrl = this.photo[photoField].url; | |
this[relField] = absUrl.replace(removeStr, ''); | |
next(); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var mongoose = require('mongoose'), | |
crate = require('mongoose-crate'), | |
localFS = require('mongoose-crate-localfs'), | |
GraphicsMagic = require('mongoose-crate-gm'), | |
relativeUrl = require('../../components/mongoose-relative-url'), | |
path = require('path'), | |
Schema = mongoose.Schema; | |
var PhotoSchema = new Schema({ | |
author: String, | |
author_email: String, | |
message: String, | |
date: { type: Date, default: Date.now }, | |
active: Boolean | |
}); | |
PhotoSchema.plugin(crate, { | |
storage: new localFS({ | |
directory: path.join(process.env.PWD, 'client/uploads/photos') | |
}), | |
fields: { | |
photo: { | |
processor: new GraphicsMagic({ | |
tmpDir: '/tmp', | |
formats: ['JPEG', 'GIF', 'PNG'], | |
transforms: { | |
original: {}, | |
small: { | |
resize: '320x200', | |
format: '.jpg' | |
}, | |
medium: { | |
resize: '640x480', | |
format: '.jpg' | |
}, | |
large: { | |
resize: '1920x1080', // '1024x768', | |
format: '.jpg' | |
} | |
} | |
}) | |
} | |
} | |
}); | |
// Plugin usage example | |
PhotoSchema.plugin(relativeUrl, { | |
absoluteUrlField: 'large', | |
relativeUrlField: 'visualizerPhoto' | |
}); | |
module.exports = mongoose.model('Photo', PhotoSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment