Skip to content

Commit

Permalink
Merge pull request #18 from achingbrain/master
Browse files Browse the repository at this point in the history
Allows specifying a directory to the provider configuration
  • Loading branch information
johanfirebase committed Apr 15, 2014
2 parents 9056d30 + c0886c2 commit ffcb8ce
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ lib-cov
*.out
*.pid
*.gz
*.iml

pids
logs
results

node_modules
coverage
8 changes: 6 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ For further instructions check [mongoose-attachments](https://github.com/firebas
The following snippet extends PhotoSchema to use mongoose-attachments and locate all the uploads in public directory, perfectly suited for [Express.js](http://expressjs.com) applications:

var path = require('path');
var os = require('os');
var attachments = require('mongoose-attachments-localfs');

MySchema.plugin(attachments, {
directory: '/absolute/path/to/public/images',
directory: os.tmpdir(),
storage : {
providerName: 'localfs'
providerName: 'localfs',
options: {
directory: '/absolute/path/to/public/images'
}
},
properties: {
image: {
Expand Down
8 changes: 5 additions & 3 deletions lib/localfs-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var util = require('util');

function LocalfsStorage(options) {
attachments.StorageProvider.call(this, options);

this._options = options;
}
util.inherits(LocalfsStorage, attachments.StorageProvider);

Expand All @@ -52,8 +54,8 @@ LocalfsStorage.prototype.getUrl = function( path ){

LocalfsStorage.prototype.createOrReplace = function(attachment, callback) {
var self = this;
var dirname = path.join(path.dirname(attachment.path), attachment.style.name);
var existsSync = fs.existsSync || path.existsSync;
var dirname = path.join(this._options.directory || path.dirname(attachment.path), attachment.style.name);
var existsSync = fs.existsSync || path.existsSync;
if (!existsSync(dirname)) {
var pathParts = dirname.split(path.sep);
var p = "/";
Expand All @@ -69,7 +71,7 @@ LocalfsStorage.prototype.createOrReplace = function(attachment, callback) {
if (path.extname(basename).length == 0) {
basename = basename + "." + attachment.model.extension;
}
attachment.path = path.join(dirname, basename);
attachment.path = path.join(this._options.directory || path.dirname(attachment.path), basename);
fs.rename(attachment.filename, attachment.path, function(error) {
if (error) {
callback(error);
Expand Down
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@
"name": "mongoose-attachments-localfs",
"description": "Simple File System Storage Provider for mongoose-attachments",
"keywords": ["mongoose", "s3", "imagemagick", "uploads", "attachments"],
"contributors": [
"contributors": [
{
"name": "Chantal Ackermann (IT:Agenten GmbH)",
"url": "http://www.it-agenten.com"
}
}
],
"version": "0.1.0",
"homepage": "https://github.com/firebaseco/mongoose-attachments-localfs",
"homepage": "https://github.com/heapsource/mongoose-attachments-localfs",
"repository": {
"type": "git",
"url": "git://github.com/firebaseco/mongoose-attachments-localfs.git"
"url": "git://github.com/heapsource/mongoose-attachments-localfs.git"
},
"main": "index.js",
"scripts": {
"test": "istanbul cover ./node_modules/mocha/bin/_mocha"
},
"dependencies": {
"mongoose-attachments": "0.1.x"
},
"devDependencies": {
"mocha": "^1.18",
"should": "^3.2",
"mongoose": "^3.8",
"istanbul": "^0.2"
},
"engines": {
"node": "*"
}
Expand Down
29 changes: 29 additions & 0 deletions test/fixtures/StubSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var attachments = require(__dirname + '/../../lib/localfs-provider');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var os = require("os");

var StubSchema = new Schema({
name: {
type: String,
required: true
}
});

StubSchema.plugin(attachments, {
directory: os.tmpdir(),
storage: {
providerName: 'localfs'
},
properties: {
image: {
styles: {
image: {
'$format': 'jpg'
}
}
}
}
});

module.exports = mongoose.model('Foo', StubSchema);
32 changes: 32 additions & 0 deletions test/fixtures/StubSchemaWithOverriddenDirectory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var attachments = require(__dirname + '/../../lib/localfs-provider');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var os = require("os");

var StubSchema = new Schema({
name: {
type: String,
required: true
}
});

StubSchema.plugin(attachments, {
directory: os.tmpdir(),
storage: {
providerName: 'localfs',
options: {
directory: os.tmpdir() + '/localfs-test'
}
},
properties: {
image: {
styles: {
image: {
'$format': 'jpg'
}
}
}
}
});

module.exports = mongoose.model('Bar', StubSchema);
Binary file added test/fixtures/node_js_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions test/testLocalfs-provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var should = require('should');
var path = require('path');
var Foo = require(path.resolve(__dirname, './fixtures/StubSchema'));
var Bar = require(path.resolve(__dirname, './fixtures/StubSchemaWithOverriddenDirectory'));
var os = require('os');

describe('localfs', function() {
it('should move an attachment to a specified directory', function (done) {
var foo = new Foo();
foo.attach('image', {
path: path.resolve(__dirname, './fixtures/node_js_logo.png')
}, function(error) {
if(error) throw error;

foo.image.image.path.should.startWith(os.tmpdir());

done();
});
});

it('should use a separate directory for temporary files', function (done) {
var bar = new Bar();
bar.attach('image', {
path: path.resolve(__dirname, './fixtures/node_js_logo.png')
}, function(error) {
if(error) throw error;

// the Bar schema uses os.tmpdir() for imagemagick temporary files and
// os.tmpdir() + '/localfs-test' for the output directory
path.dirname(bar.image.image.path).should.not.equal(os.tmpdir());

done();
});
});
});

0 comments on commit ffcb8ce

Please sign in to comment.