 
     Backbone.sync 'create', photo,
       contentType: false # Prevent $.ajax from being smart
-      success: -> next()
+      success: ->
+            next()
+      error: ->
+            photo.set 'thumbsrc', 'img/error.gif'
+            next() # clear tmps anyway
       data: formdata
 
+# async waterfall of all the above
+makeThumbWorker = (photo , done) ->
+    async.waterfall [
+        (cb) -> readFile         photo, cb
+        (cb) -> makeThumbDataURI photo, cb
+        (cb) ->
+            delete photo.img
+            cb()
+    ], (err) ->
+        if err
+            photo.set
+                thumbsrc:'img/error.gif'
+                title: photo.get('title') + ' is ' + err
+        done(err)
 
-# delete all object attached to the photo
-# the photo is now backed by the server
-clearTemp = (photo, next) ->
-    delete photo.file
-    delete photo.img
-    delete photo.file_du
-    delete photo.thumb
-    delete photo.thumb_du
-    next()
+uploadWorker = (photo, done) ->
+    async.waterfall [
+        (cb) -> makeThumbBlob    photo, cb
+        (cb) -> upload           photo, cb
+        (cb) ->
+            # the photo is now backed by the server
+            # delete all object attached to the photo
+            delete photo.file
+            delete photo.thumb
+            delete photo.thumb_du
+            cb()
+    ], (err) ->
+        if err
+            photo.set
+                thumbsrc:'img/error.gif'
+                title: photo.get('title') + ' is ' + err
+        done(err)
 
 
-# async waterfall of all the above
-operation = (task , callback) ->
-    {photo, file} = task
-    photo.file = file
-    async.waterfall [
-        (cb) -> readFile photo         , cb
-        (cb) -> makeThumbDataURI photo , cb
-        (cb) -> makeThumbBlob photo    , cb
-        (cb) -> upload photo           , cb
-        (cb) -> clearTemp Photo        , cb
-    ], callback
+class PhotoProcessor
 
-# number of pictures being processed at any time
-concurrency = 3
+    # create thumbs 3 by 3
+    thumbsQueue: async.queue makeThumbWorker, 3
 
-queue = async.queue operation, concurrency
+    # upload 2 by 2
+    uploadQueue: async.queue uploadWorker, 2
 
-module.exports.process = (file, photo) ->
-    queue.push
-        file: file
-        photo: photo
-    , ->+    process: (photo) ->
+        @thumbsQueue.push photo, (err) =>
+            return console.log err if err
+            @uploadQueue.push photo, (err) =>
+                return console.log err if err
+
+module.exports = new PhotoProcessor()


# width and height of generated thumbs
MAX_WIDTH = MAX_HEIGHT = 100

# read the file from photo.file using a FileReader
# create photo.img : an Image
# next : callback when the file have been read

readFile = (photo, next) ->
    reader = new FileReader()
    photo.img = new Image()
    reader.readAsDataURL photo.file
    reader.onloadend = =>
        photo.file_du = reader.result
        photo.img.src = reader.result
        photo.img.onload = ->
            next()

    canvas.height = MAX_HEIGHT
    ctx = canvas.getContext '2d'
    ctx.drawImage photo.img, 0, 0, newWidth, newHeight
    photo.thumb_du = canvas.toDataURL photo.file.type

    # let the view update itself
    photo.set 'thumbsrc', photo.thumb_du

    next()

