Skip to content

Instantly share code, notes, and snippets.

@nate-strauser
Created November 2, 2018 19:04
Show Gist options
  • Save nate-strauser/34756d27c5571c81c2e9d9cf20648759 to your computer and use it in GitHub Desktop.
Save nate-strauser/34756d27c5571c81c2e9d9cf20648759 to your computer and use it in GitHub Desktop.
uploadImageCordova = function (callback, progressCallback, localPreviewCallback) {
var fail = function (error) {
callback(error);
};
var cameraSuccess = function (imageURI) {
if (_.isFunction(localPreviewCallback)) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
// console.log('image URI');
// console.log(imageURI);
var imageURL = imageURI;
var rootToURL = fileSystem.root.toURL();
var rootToInternalURL = fileSystem.root.toInternalURL();
imageURL = imageURL.replace(rootToURL, rootToInternalURL);
// console.log('preview url');
// console.log(imageURL);
localPreviewCallback(imageURL);
});
}
var options = {
mimeType: 'image/jpeg',
fileName: "image-upload-" + Meteor.userId() + "-" + Date.now() + ".jpg",
params: {
"UPLOADCARE_PUB_KEY": Meteor.settings.public.uploadcare.publickey,
"UPLOADCARE_STORE": 1
}
};
var ft = new FileTransfer();
ft.onprogress = function (progressEvent) {
if (progressEvent.lengthComputable && _.isFunction(progressCallback)) {
//console.log(progressEvent.loaded / progressEvent.total);
progressCallback(progressEvent.loaded / progressEvent.total);
}
};
ft.upload(imageURI, "https://upload.uploadcare.com/base/", function (result) {
var ucFile = uploadcare.fileFrom('uploaded', JSON.parse(result.response).file);
ucFile.done(function (fileInfo) {
//do something with fileinfo
callback(null, { imageId: imageId, imageUuid: fileInfo.uuid });
});
}, cameraFail, options);
};
var cameraFail = function (message) {
Session.set('uploadProgress', undefined);
callback(message);
};
navigator.camera.getPicture(cameraSuccess, cameraFail, {
quality: 90,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: false,
encodingType: Camera.EncodingType.JPEG,
saveToPhotoAlbum: false
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment