Permalink
Browse files

add functionality to remove files, other control flow changes

  • Loading branch information...
madhums committed Mar 4, 2012
1 parent 3f9acd2 commit c5ffb1f3bab7b5933228cab4c031f626714f2526
Showing with 94 additions and 40 deletions.
  1. +94 −40 lib/imager.js
View
@@ -13,6 +13,7 @@ var config_file = '../imager-example.json'
, storage_type = undefined
, scope = undefined
, cdnUri = undefined
+ , uploadedFiles = []
var Imager = exports = module.exports = function Imager(options) {
@@ -36,8 +37,13 @@ Imager.prototype = {
return this[storage_type + 'Url'](_file)
}
- , rsUrl: function(file) {
- return client.url( file )
+ , rsUrl: function(file, callback) {
+ client.setAuth(function() {
+ client.getContainer(container, true, function (err, c) {
+ if (err) throw err
+ callback(c.cdnUri, file)
+ })
+ })
}
, genFileName: function(file, version) {
@@ -66,8 +72,12 @@ Imager.prototype = {
}
, rsRemove: function(file, callback) {
- client.removeFile(file, function(err, res){
- callback(err)
+ client.setAuth(function() {
+ client.getContainer(container, true, function (err, c) {
+ c.removeFile(file, function(err, res){
+ callback(err)
+ })
+ })
})
}
@@ -77,28 +87,46 @@ Imager.prototype = {
var _this = this
, form = new formidable.IncomingForm()
, files = []
- , fields = []
, new_file = undefined
- form
- .on('field', function(field, value) {
- fields.push([field, value])
- })
- .on('file', function(field, file) {
- files.push([field, file])
- new_file = file
- })
- .on('end', function() {
- files.forEach(function (file) {
- if (file[1].name != '') {
- _this.makeVariants(file[1], function(err, file){
- callback(err, file, cdnUri, res)
- })
- }
- })
+ if (req.files == undefined) {
+ form.parse(req, function (err, fields, files) {
+ parsedForm(files)
})
+ }
+ else {
+ parsedForm(req.files)
+ }
- form.parse(req)
+ function parsedForm (files) {
+ var keys = Object.keys(files)
+ , k = keys.shift()
+
+ processImage(files[k])
+
+ function processImage(file) {
+ if (file.name != '') {
+ _this.makeVariants(file, function(err, file){
+ var key = keys.shift()
+ if (typeof key != 'undefined')
+ processImage(files[key])
+ else {
+ callback(err, uploadedFiles, cdnUri, res)
+ uploadedFiles = []
+ }
+ })
+ }
+ else {
+ var key = keys.shift()
+ if (typeof key != 'undefined')
+ processImage(files[key])
+ else {
+ callback(null, uploadedFiles, cdnUri, res)
+ uploadedFiles = []
+ }
+ }
+ }
+ }
}
, rsSetup: function(options) {
@@ -117,24 +145,50 @@ Imager.prototype = {
new_file += ext
- var i = 0
- for(prefix in _resize) {
- this.imAction('im.resize', file, this.genFileName( new_file, prefix ) , _resize[prefix], function(err){
- i++
- if (i == Object.keys(_resize).length ) {
- i = 0
- for(prefix in _crop) {
- _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'])
- callback(err, new_file)
- }
- })
- }
- }
+ 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) {
+ resizeImage(function () {
+ cropImage()
})
}
+ else if (Object.keys(_resize).length > 0 && Object.keys(_crop).length == 0)
+ resizeImage()
+ else
+ cropImage()
+
+
+ function resizeImage(cb) {
+ var i = 0
+ if (Object.keys(_resize).length > 0) {
+ for(prefix in _resize) {
+ _this.imAction('im.resize', file, _this.genFileName( new_file, prefix ) , _resize[prefix], function(err){
+ i++
+ if (i == Object.keys(_resize).length && Object.keys(_crop).length == 0)
+ callback(err, new_file)
+ else if (i == Object.keys(_resize).length && Object.keys(_crop).length > 0)
+ cb()
+ })
+ }
+ }
+ }
+
+ function cropImage(cb) {
+ var i = 0
+ if (Object.keys(_crop).length > 0) {
+ for(prefix in _crop) {
+ _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'])
+ callback(err, new_file)
+ }
+ })
+ }
+ }
+ }
}
, setupVariants: function() {
@@ -150,7 +204,7 @@ Imager.prototype = {
client.getContainer(container, true, addFileToContainer)
})
- function addFileToContainer(err, container) {
+ function addFileToContainer(err, c) {
var options = {
remote: dfile
, headers: {
@@ -159,7 +213,7 @@ Imager.prototype = {
}
, local: sfile }
- cdnUri = container.cdnUri
+ cdnUri = c.cdnUri
client.addFile(container, options, onUploadToContainer)
}

0 comments on commit c5ffb1f

Please sign in to comment.