Created
July 8, 2015 15:44
-
-
Save sgimeno/f8326099d16d79592cbd to your computer and use it in GitHub Desktop.
Snippet to upload pictures using cordova plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//You will need the following cordov plugins | |
// + https://github.com/apache/cordova-plugin-file-transfer | |
// + https://github.com/apache/cordova-plugin-file | |
angular.module('someModule', []) | |
.service('cordovaService', function($window, $document, $q, $timeout) { | |
// Source https://github.com/mgcrea/angular-cordova/blob/master/src/angular-cordova.js | |
var self = this; | |
this.cordova = $window.cordova || {}; | |
this.device = $window.device; | |
var deferredReady = $q.defer(); | |
this.$ready = deferredReady.promise; | |
var _document = $document[0]; | |
_document.addEventListener('deviceready', function() { | |
deferredReady.resolve($window.device); | |
}); | |
this.isReady = function() { | |
return this.device && this.device.available; | |
}; | |
}) | |
.factory('picturesService', function($q, cordovaService){ | |
var getPicture = function(cameraOptions, blobKey){ | |
var deferred = $q.defer(); | |
function onSuccess(imageData) { | |
deferred.resolve({fileName: blobKey, imageURI: imageData}); | |
} | |
function onFail(message) { | |
deferred.reject(message); | |
console.log('Failed because: ' + message); | |
} | |
navigator.camera.getPicture(onSuccess, onFail, cameraOptions); | |
return deferred.promise; | |
}; | |
var uploadPicture = function(fileName, imageURI, policyData) { | |
//TODO: The url should be server generated as the server already | |
//knows the bucket name and zone and, thus, we don't need to | |
//leak this detail to the client | |
var url = encodeURI(policyData.endpoint.href); | |
var deferred = $q.defer(); | |
var ft = new FileTransfer(), | |
options = new FileUploadOptions(); | |
options.fileKey = "file"; | |
options.fileName = fileName; | |
options.mimeType = "image/jpeg"; | |
options.chunkedMode = false; | |
options.params = { | |
"key": policyData.key, | |
"AWSAccessKeyId": policyData.AWSAccessKeyId, | |
"acl": policyData.acl, | |
"policy": policyData.policy, | |
"signature": policyData.signature | |
}; | |
options.headers = { | |
"Connection": "close" | |
}; | |
ft.upload(imageURI, url, | |
function (e) { | |
var path = url + '/' + options.params.key; | |
deferred.resolve({href: path, key: options.params.key}); | |
}, | |
function (e) { | |
deferred.reject(e); | |
console.log('Upload failed:', e); | |
}, options); | |
return deferred.promise; | |
}; | |
var cleanTmpPictures = function(picture){ | |
var deferred = $q.defer(); | |
function onSuccess() { | |
console.log("Camera cleanup success."); | |
deferred.resolve(); | |
} | |
function onFail(message) { | |
console.log('Failed to clean tmp pictures because: ' + message); | |
deferred.resolve(message); | |
} | |
navigator.camera.cleanup( onSuccess, onFail ); | |
return deferred.promise; | |
}; | |
return { | |
getPicture: function(cameraOptions){ | |
return cordovaService.$ready | |
.then(getPicture.bind(null, cameraOptions)); | |
}, | |
uploadPicture: function(fileName, imageURI, policyData){ | |
return cordovaService.$ready | |
.then(uploadPicture.bind(null, fileName, imageURI, policyData)); | |
//Gives an error on ACER A3-A10: Invalid action | |
//.then(cleanTmpPictures); | |
} | |
}; | |
}) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment