Skip to content

Commit

Permalink
The uploadfs module now returns a function you must call to create an…
Browse files Browse the repository at this point in the history
… instance. You must explicitly pass that instance around if you want to share it among modules, etc. This avoids nasty surprises that occur when relying on 'require' to do it automatically and also gives you the option of multiple instances
  • Loading branch information
Tom Boutell committed Dec 30, 2012
1 parent 0125beb commit b0b5507
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ You can also remove a file if needed.

There is no API to retrieve information about previously uploaded files. This is intentional. Constantly manipulating directory information is much slower in the cloud than on a local filesystem and you should not become reliant on it. Your code should maintain its own database of file information if needed, for instance in a MongoDB collection.

## CHANGES IN 0.3.0

Starting in version 0.3.0, you must explicitly create an instance of uploadfs. This allows you to have more than one, separately configured instance, and it also avoids serious issues with modules not seeing the same instance automatically as they might expect. For more information see [Singletons in #node.js modules cannot be trusted, or why you can't just do var foo = require('baz').init()](http://justjs.com/posts/singletons-in-node-js-modules-cannot-be-trusted-or-why-you-can-t-just-do-var-foo-require-baz-init).

Existing code that isn't concerned with sharing uploadfs between multiple modules will only need a two line change to be fully compatible:

// CHANGE THIS
var uploadfs = require('uploadfs');

// TO THIS (note the extra parens)
var uploadfs = require('uploadfs')();

If you use uploadfs in multiple source code files, you'll need to pass your `uploadfs` object explicitly, much as you pass your Express `app` object when you want to add routes to it via another file.

## Requirements

You need:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uploadfs",
"version": "0.2.3",
"version": "0.3.0",
"description": "Store files in a web-accessible location via a simplified API. Can automatically scale images. Includes both S3-based and local filesystem-based backends with the most convenient features of each.",
"main": "uploadfs.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// depending on which backend you choose.

var express = require('express');
var uploadfs = require('./uploadfs.js');
var uploadfs = require('./uploadfs.js')();

// For the local backend
var uploadsPath = __dirname + '/public/uploads';
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var uploadfs = require('./uploadfs.js');
var uploadfs = require('./uploadfs.js')();
var fs = require('fs');
var request = require('request');
var _ = require('underscore');
Expand Down
24 changes: 14 additions & 10 deletions uploadfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ var fs = require('fs');
var rmRf = require('rimraf');
var imagemagick = require('node-imagemagick');

var tempPath;
var backend;
var imageSizes;
var orientOriginals = true;
module.exports = function() {
return new uploadfs();
}

var self = module.exports = {
init: function(options, callback) {
function uploadfs() {
var tempPath;
var backend;
var imageSizes;
var orientOriginals = true;
var self = this;
self.init = function(options, callback) {
if (!options.backend) {
return callback("backend must be specified");
}
Expand Down Expand Up @@ -50,7 +54,7 @@ var self = module.exports = {
}
},

copyIn: function(localPath, path, options, callback) {
self.copyIn = function(localPath, path, options, callback) {
if (typeof(options) === 'function') {
callback = options;
options = {};
Expand Down Expand Up @@ -96,7 +100,7 @@ var self = module.exports = {
* so that they will display properly in web browsers.
*/

copyImageIn: function(localPath, path, options, callback) {
self.copyImageIn = function(localPath, path, options, callback) {
if (typeof(options) === 'function') {
callback = options;
options = {};
Expand Down Expand Up @@ -237,11 +241,11 @@ var self = module.exports = {
}
},

getUrl: function(options, callback) {
self.getUrl = function(options, callback) {
return backend.getUrl(options, callback);
},

remove: function(path, callback) {
self.remove = function(path, callback) {
return backend.remove(path, callback);
}
};
Expand Down

0 comments on commit b0b5507

Please sign in to comment.