From 6aeafe51bddee7f4042bf5b4f5af2a40577b847f Mon Sep 17 00:00:00 2001 From: Nico Prat Date: Thu, 2 Jun 2016 13:43:44 +0200 Subject: [PATCH 1/2] Allows offline Pictures are inserted in local collection, before checking connection and signing with Cloudinary API. You can try in your browser console: Meteor.connection.disconnect(); // Then try to upload a file Cloudinary.collection.find().fetch(); // Returns the local picture object Meteor.connection.reconnect() // If no error in callback, the upload begins // else, the picture is removed from the local collection --- client/functions.coffee | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/client/functions.coffee b/client/functions.coffee index d458fdf..2e0113b 100644 --- a/client/functions.coffee +++ b/client/functions.coffee @@ -63,6 +63,18 @@ Cloudinary = reader.readAsDataURL file _upload_file: (file, ops={}, callback) -> + # Create collection document ID + collection_id = Random.id() + + # Set fields + fields = _.extend ops.fields || {}, + _id: collection_id + status: 'waiting' + preview: file + + # Insert local preview + Cloudinary.collection.insert fields + Meteor.call "c.sign", ops, (error,result) -> if not error # Build form @@ -72,23 +84,13 @@ Cloudinary = form_data.append "file",file - # Create collection document ID - collection_id = Random.id() - # Send data - Cloudinary.xhr = new XMLHttpRequest() - - # Set fields - fields = _.extend ops.fields || {}, - _id: collection_id - status: 'uploading' - preview: file - - Cloudinary.collection.insert fields + Cloudinary.xhr = new XMLHttpRequest() Cloudinary.xhr.upload.addEventListener "progress", (event) -> Cloudinary.collection.update _id:collection_id, $set: + status: 'uploading' loaded:event.loaded total:event.total percent_uploaded: Math.floor ((event.loaded / event.total) * 100) @@ -132,6 +134,7 @@ Cloudinary = Cloudinary.xhr.send form_data else + Cloudinary.collection.remove collection_id return callback and callback error,null From 9faccfbbb04a169ad9a62cf7107b0ccd1736bfc4 Mon Sep 17 00:00:00 2001 From: Nico Prat Date: Sat, 2 Jul 2016 16:01:34 +0200 Subject: [PATCH 2/2] Allows offline upload Stores file in local collection before signing Cloudinary request. Local documents are deleted if signing fails. If it succeed (now or at next connection), everything resume normally. --- client/functions.coffee | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/client/functions.coffee b/client/functions.coffee index 8f55046..7e32246 100644 --- a/client/functions.coffee +++ b/client/functions.coffee @@ -67,8 +67,21 @@ Cloudinary = reader.readAsDataURL file _upload_file: (file, ops={}, callback) -> + # Create collection document ID + collection_id = Random.id() + + # Set fields + fields = _.extend ops.fields || {}, + _id: collection_id + status: 'waiting' + preview: file + + # Insert local preview + Cloudinary.collection.insert fields + Meteor.call "c.sign", ops, (error,result) -> if error + Cloudinary.collection.remove collection_id return callback and callback error,null # Build form @@ -78,20 +91,13 @@ Cloudinary = form_data.append "file",file - # Create collection document ID - collection_id = Random.id() - # Send data Cloudinary.xhr = new XMLHttpRequest() - Cloudinary.collection.insert - _id:collection_id - status:"uploading" - preview:file - Cloudinary.xhr.upload.addEventListener "progress", (event) -> - Cloudinary.collection.update _id:collection_id, + Cloudinary.collection.update collection_id, $set: + status: 'uploading' loaded:event.loaded total:event.total percent_uploaded: Math.floor ((event.loaded / event.total) * 100) @@ -100,7 +106,7 @@ Cloudinary = Cloudinary.xhr.addEventListener "load", -> if Cloudinary.xhr.status < 400 response = JSON.parse @response - Cloudinary.collection.upsert collection_id, + Cloudinary.collection.update collection_id, $set: status:"complete" percent_uploaded: 100 @@ -109,7 +115,7 @@ Cloudinary = callback and callback null,response else response = JSON.parse @response - Cloudinary.collection.upsert collection_id, + Cloudinary.collection.update collection_id, $set: status:"error" response: response @@ -118,7 +124,7 @@ Cloudinary = Cloudinary.xhr.addEventListener "error", -> response = JSON.parse @response - Cloudinary.collection.upsert collection_id, + Cloudinary.collection.update collection_id, $set: status:"error" response: response @@ -126,7 +132,7 @@ Cloudinary = callback and callback response,null Cloudinary.xhr.addEventListener "abort", -> - Cloudinary.collection.upsert collection_id, + Cloudinary.collection.update collection_id, $set: status:"aborted"