Skip to content
Browse files

fixes #1, #2, #3

  • Loading branch information...
1 parent b2dd722 commit 36420facac76cb4d4823dc5c7cf3c4f5d6920733 @madhums committed Apr 27, 2012
Showing with 122 additions and 46 deletions.
  1. +6 −0 History.md
  2. +111 −43 lib/imager.js
  3. +5 −3 package.json
View
6 History.md
@@ -1,4 +1,10 @@
+0.0.5 / 2012-04-24
+==================
+
+ * support local file uploads #3, support multiple file uploads #4
+ * rename repo
+
0.0.4 / 2012-03-04
==================
View
154 lib/imager.js
@@ -3,6 +3,7 @@ var formidable = require('formidable')
, im = require('imagemagick')
, fs = require('fs')
, path = require('path')
+ , mime = require('mime')
, cloudfiles = require('cloudfiles')
var config_file = '../imager-example.json'
@@ -20,7 +21,9 @@ var Imager = exports = module.exports = function Imager(options) {
config_file = (typeof options['config_file'] === 'undefined') ? config_file : options['config_file']
storage_type = options['storage']
this.loadConfig(config_file)
+ this.localFiles = false
+ // throw an error if the rackspace auth settings are not defined
if ( typeof (config['storage']['rs']) == 'undefined' )
throw new TypeError("No storage defined in Imager config")
@@ -32,80 +35,138 @@ var Imager = exports = module.exports = function Imager(options) {
Imager.prototype = {
- url: function(file, version) {
+ url: function(file, version, callback) {
var _file = this.genFileName( file, version )
- return this[storage_type + 'Url'](_file)
+ return this[storage_type + 'Url'](_file, callback)
}
, rsUrl: function(file, callback) {
+ // authenticate your rackspace account
client.setAuth(function() {
+ // get the container
client.getContainer(container, true, function (err, c) {
- if (err) throw err
- callback(c.cdnUri, file)
+ // if there is any error, pass the error to the callback
+ if (err) callback(err)
+ // pass the url to the callback
+ callback(null, c.cdnUri+'/'+file)
})
})
}
, genFileName: function(file, version) {
return version + '_' + file
- }
+}
- , remove: function(file, callback, _scope) {
+ , remove: function(files, callback, _scope) {
scope = (arguments.length == 3) ? _scope : undefined
+ var self = this
- var _variants = this.setupVariants()
- , _resize = _variants['resize']
- , _crop = _variants['crop']
-
- for(version in _resize) {
- this[storage_type + 'Remove']( this.genFileName( file, version ), function(err) {
- callback(err)
- })
+ if (typeof(files) === 'string') {
+ // remove the single file
+ removeVariants(files)
+ }
+ else if (Array.isArray(files)) {
+ // remove all the files in the given array
+ try {
+ files.forEach(function (file) {
+ removeVariants(file)
+ })
+ }
+ catch (e) {
+ callback(e)
+ }
+ }
+ else {
+ // throw an error
+ callback(new Error('You should pass a single file or an array of files'))
}
- for(version in _crop) {
- this[storage_type + 'Remove']( this.genFileName( file, version ), function(err) {
- callback(err)
- })
+ function removeVariants (file) {
+ var _variants = self.setupVariants()
+ , _resize = _variants['resize']
+ , _crop = _variants['crop']
+
+ // remove each variant specified in the resize settings
+ for(version in _resize) {
+ self[storage_type + 'Remove']( self.genFileName( file, version ), function(err) {
+ if (err) throw err
+ })
+ }
+
+ // remove each variant specified in the crop settings
+ for(version in _crop) {
+ self[storage_type + 'Remove']( self.genFileName( file, version ), function(err) {
+ if(err) throw err
+ })
+ }
}
- return callback(null)
+
+ callback(null)
}
- , rsRemove: function(file, callback) {
+ , rsRemove: function(file) {
+ // authenticate your rackspace account
client.setAuth(function() {
+ // get the container
client.getContainer(container, true, function (err, c) {
+ // remove the file from container
c.removeFile(file, function(err, res){
- callback(err)
+ if (err) throw err
+ console.log('removed')
+ // callback(err)
})
})
})
}
, upload: function(req, res, callback, _scope) {
- scope = (arguments.length == 4) ? _scope : undefined
-
var _this = this
- , form = new formidable.IncomingForm()
, files = []
- , new_file = undefined
-
- if (req.files == undefined) {
- form.parse(req, function (err, fields, files) {
- parsedForm(files)
- })
+ // allow passing of a single file OR an array of files
+ if (typeof req === 'string' || Array.isArray(req)) {
+ this.localFiles = true
+ _scope = callback
+ callback = res
+ scope = (arguments.length == 3) ? _scope : undefined
+ // create the files array.
+ files = typeof req === 'string' ? req.split() : req
+ parsedForm(files)
}
else {
- parsedForm(req.files)
+ scope = (arguments.length == 4) ? _scope : undefined
+ var _this = this
+ , form = new formidable.IncomingForm()
+ , files = []
+ , new_file = undefined
+
+ if (req.files == undefined) {
+ // parse the form if it isn't parsed yet
+ form.parse(req, function (err, fields, files) {
+ parsedForm(files)
+ })
+ }
+ else {
+ // send the files for processing
+ parsedForm(req.files)
+ }
}
function parsedForm (files) {
+
var keys = Object.keys(files)
, k = keys.shift()
+ // if files are uploaded with multiple attr
+ if (Array.isArray(files[k])) {
+ files = files[k]
+ keys = Object.keys(files)
+ k = keys.shift()
+ }
+
processImage(files[k])
function processImage(file) {
- if (file.name != '') {
+ if (file.name != '' || this.localFiles) {
_this.makeVariants(file, function(err, file){
var key = keys.shift()
if (typeof key != 'undefined')
@@ -127,6 +188,7 @@ Imager.prototype = {
}
}
}
+
}
, rsSetup: function(options) {
@@ -135,18 +197,19 @@ Imager.prototype = {
}
, makeVariants: function(file, callback) {
-
var _this = this
, _variants = this.setupVariants()
, _resize = _variants["resize"] || JSON.parse( JSON.stringify({"null" : "null"}) )
, _crop = _variants["crop"] || JSON.parse( JSON.stringify({"null" : "null"}) )
, new_file = Math.round(new Date().getTime())
- , ext = this.setExtension(file['type'])
+ , type = this.localFiles ? mime.lookup(file) : file['type']
+ , filePath = this.localFiles ? file : file['path']
+ , ext = this.setExtension(type)
new_file += ext
-
uploadedFiles.push(new_file)
+
if (Object.keys(_resize).length == 0 && Object.keys(_crop).length == 0)
throw new Error("No variants specified in the config file")
else if (Object.keys(_resize).length > 0 && Object.keys(_crop).length > 0) {
@@ -182,7 +245,7 @@ Imager.prototype = {
_this.imAction('im.crop', file, _this.genFileName( new_file, prefix ) , _crop[prefix], function(err){
i++
if (i == Object.keys(_crop).length ) {
- fs.unlink(file['path'])
+ // fs.unlink(path)
callback(err, new_file)
}
})
@@ -205,6 +268,7 @@ Imager.prototype = {
})
function addFileToContainer(err, c) {
+ if (err) callback(err)
var options = {
remote: dfile
, headers: {
@@ -218,10 +282,10 @@ Imager.prototype = {
}
function onUploadToContainer(err, uploaded) {
- if (err) throw err
+ if (err) callback(err)
if (uploaded) {
- fs.unlink(sfile)
- callback(err)
+ callback(err) //fs.unlink(sfile)
+
}
}
@@ -242,13 +306,16 @@ Imager.prototype = {
if (size == "null") {
return callback(null)
}
+
var _this = this
, dfile = prefix
- , tfile = (storage_type == 'dir') ? uploaddir + dfile : file['path'] + prefix
+ , filePath = this.localFiles ? file : file['path']
+ , type = this.localFiles ? mime.lookup(file) : file['type']
+ , tfile = (storage_type == 'dir') ? uploaddir + dfile : filePath + prefix
- eval(action)( this.imOptions(file, tfile, size), function(err, stdout, stderr){
+ eval(action)( this.imOptions(file, tfile, size), function(err, stdout, stderr){
if (storage_type == 'rs') {
- _this.pushToRS(tfile, dfile, file['type'], function(err){
+ _this.pushToRS(tfile, dfile, type, function(err){
return callback(err)
})
}
@@ -275,8 +342,9 @@ Imager.prototype = {
, imOptions: function(file, tfile, size) {
var _size = size.split('x')
+ , filePath = this.localFiles ? file : file['path']
return options = {
- srcPath: file['path'],
+ srcPath: filePath,
dstPath: tfile,
width: _size[0],
height: _size[1],
View
8 package.json
@@ -2,20 +2,22 @@
"author": "Madhusudhan Srinivasa <madhums8@gmail.com> (http://twitter.com/madhums)",
"name": "imager",
"description": "Easy way to resize, crop and upload images to Rackspace cloudfiles",
- "version": "0.0.4",
- "keywords": ["upload", "image", "imagemagick", "rackspace", "cloudfiles", "resize", "file"],
+ "version": "0.0.5",
+ "keywords": ["upload", "image", "imagemagick", "rackspace", "cloudfiles", "resize", "file", "crop"],
"homepage": "https://github.com/madhums/node-imager",
"repository": {
"type": "git",
"url": "git://github.com/madhums/node-imager.git"
},
+ "main": "index",
"engines": {
"node": ">= 0.4.1 < 0.7.0"
},
"dependencies": {
"formidable": "1.0.7",
"imagemagick": "0.1.2",
- "cloudfiles": "0.3.3"
+ "cloudfiles": "0.3.3",
+ "mime": "1.2.5"
},
"devDependencies": {}
}

0 comments on commit 36420fa

Please sign in to comment.
Something went wrong with that request. Please try again.