-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTiCloudinary.js
94 lines (90 loc) · 3.36 KB
/
TiCloudinary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* This project is a commonJS module, offered for repeatable use in Appcelerator Titanium projects
* and was based on the following idea: https://gist.github.com/yozef/5841069. Many thanks Joseph!
*/
var moment = require('alloy/moment');
// Cloudinary credentials - **Replace these with your creds!!!
var cloudName = 'Your_Cloud_Name';
var apiKey = 999999999999;
var apiSecret = 'XXXXXXXXXX-XXXXX-XX';
// Image API URL base
var apiBaseUrl = 'http://api.cloudinary.com/v1_1/';
// Create and send alert on error
function alert(message, title, ok, callback) {
a = Ti.UI.createAlertDialog({
message: message,
title: title || L('alert_title', 'Alert'),
ok: ok || L('alert_ok', 'OK')
});
a.show();
if (callback) {
a.addEventListener('click', callback);
}
}
exports = {
/**
* Upload image from a TiBlob object and return the transformation url
*
* @param blob {Object} - TiBlob object of the image to be uploaded/transformed
* @param transformParams {String} - List of supported query string parameters for performing
* transforms on the image uploaded - read more on supported parameters at
* http://cloudinary.com/documentation/image_transformations
* @param callback {Function} - Optional callback function, called with an argument object containing the
* response `public_id` and transform url to call the Cloudinary API with to perform the transformation
*/
uploadAndTransform: function(blob, transformParams, callback) {
var nowUnix = moment().unix();
var signature = Ti.Utils.sha1('timestamp=' + nowUnix + apiSecret);
var url = apiBaseUrl + cloudName + '/image/upload';
var client = Ti.Network.createHTTPClient({
onload: function(e) {
Ti.API.info('Upload Response', this.responseText);
var response = JSON.parse(this.responseText);
var transformImgUrl = 'http://res.cloudinary.com/' + cloudName + '/image/upload/' + transformParams + '/' + response.public_id + '.png';
if (callback) {
callback({
public_id: response.public_id,
transformImgUrl: transformImgUrl
});
}
},
onerror: function(e) {
Ti.API.info('Cloudinary Upload Error', JSON.stringify(e));
alert('Please verify your data connection and try again.', 'Uh Oh');
},
timeout: 4000
});
client.setRequestHeader('enctype', 'multipart/form-data');
client.setRequestHeader('Content-Type', 'image/png');
client.open('POST', url);
var params = {
api_key: apiKey,
file: blob,
//public_id: someUniqueName, // if added, make sure the signature sha1 starts with:'public_id=' + someUniqueName + '×tamp=...'
signature: signature,
timestamp: nowUnix,
};
client.send(params);
},
/**
* Delete uploaded image
*
* @param public_id {String} - `public_id` of the image to be deleted
*/
deleteImage: function(public_id) {
var url = apiBaseUrl + cloudName + '/resources/image/upload?public_ids[]=' + public_id;
var client = Ti.Network.createHTTPClient({
onload: function(e) {
Ti.API.info('Delete Response', this.responseText);
},
onerror: function(e) {
Ti.API.info('Cloudinary Delete Error', JSON.stringify(e));
},
timeout: 4000
});
client.setUsername(apiKey);
client.setPassword(apiSecret);
client.open('DELETE', url);
client.send();
}
};