Created
April 18, 2012 15:02
-
-
Save jafstar/2414161 to your computer and use it in GitHub Desktop.
Appcelerator + Cloudinary + Image Upload
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
//CREATE WINDOW | |
var win = Ti.UI.createWindow({ | |
title: 'Upload', | |
backgroundColor: '#FFF', | |
fullscreen: true, | |
navBarHidden: true | |
}); | |
//PHOTO VIEW CONTAINER | |
var photoView = Ti.UI.createImageView({ | |
top: 10, | |
width: 120, | |
height: 120, | |
backgroundColor: '#000' | |
}); | |
//CLICK PHOTO VIEW LISTENER | |
photoView.addEventListener('click', function(e) { | |
openGallery(); | |
}); | |
//OPEN GALLERY | |
function openGallery() { | |
Ti.Media.openPhotoGallery({ | |
success: function(event) { | |
var image = event.media; | |
photoView.setImage(image); | |
uploadPhoto(image); | |
}, | |
//IPHONE ONLY | |
allowEditing: true, | |
animate: true, | |
autoHide: true, | |
mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO] | |
}); | |
//END GALLERY | |
} | |
//UPLOAD PHOTO | |
function uploadPhoto(file) { | |
//KEYS | |
function keys(obj) { | |
var keys = []; | |
for (var key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
keys.push(key); | |
} | |
} | |
return keys; | |
} | |
//SIGNATURE | |
function sign_params(params, secret) { | |
var sorted_keys = keys(params).sort(); | |
var parts = []; | |
for (var i = 0; i < sorted_keys.length; i++) { | |
var key = sorted_keys[i]; | |
var value = params[key]; | |
if (value && value !== '') { | |
parts.push("" + key + "=" + value); | |
} | |
} | |
var str_to_sign = parts.join("&"); | |
return Ti.Utils.sha1(str_to_sign + secret); | |
} | |
//VARS | |
var photoID = "sample2"; | |
var key = "12345678910"; | |
var secret = "sOmeK1NdofS3CreTw0Rd"; | |
var timestamp = Math.round(new Date().getTime() / 1000); | |
var params = { | |
timestamp: timestamp, | |
public_id: photoID | |
}; | |
var signature = sign_params(params, secret); | |
var uploadURL = "http://api.cloudinary.com/v1_1/yourSite/image/upload?api_key=" + key + "&signature=" + signature; | |
//DATA | |
var data = { | |
"timestamp": timestamp, | |
"public_id": photoID, | |
"file": file, | |
"signature": signature | |
} | |
//UPLOAD IMAGE | |
var xhr = Ti.Network.createHTTPClient(); | |
xhr.onload = function() { | |
Ti.API.log(this.responseText); | |
}; | |
xhr.onerror = function(e) { | |
Ti.API.log('Upload Error => ' + e.error); | |
}; | |
xhr.open("POST", uploadURL); | |
xhr.send(data); | |
//END UPLOAD | |
} | |
//ADD PHOTOVIEW | |
win.add(photoView); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment