-
Notifications
You must be signed in to change notification settings - Fork 108
/
Cloudinary.java
237 lines (202 loc) · 8.81 KB
/
Cloudinary.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.cloudinary;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.cloudinary.strategies.AbstractApiStrategy;
import com.cloudinary.strategies.AbstractUploaderStrategy;
import com.cloudinary.strategies.AbstractUrlBuilderStrategy;
import com.cloudinary.strategies.StrategyLoader;
import com.cloudinary.utils.ObjectUtils;
import com.cloudinary.utils.StringUtils;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Cloudinary {
private static List<String> UPLOAD_STRATEGIES = new ArrayList<String>(Arrays.asList("com.cloudinary.android.UploaderStrategy","com.cloudinary.http42.UploaderStrategy","com.cloudinary.http43.UploaderStrategy"));
private static List<String> API_STRATEGIES = new ArrayList<String>(Arrays.asList( "com.cloudinary.android.ApiStrategy", "com.cloudinary.http42.ApiStrategy", "com.cloudinary.http43.ApiStrategy" ));
private static List<String> URLBUILDER_STRATEGIES = new ArrayList<String>(Arrays.asList( "com.cloudinary.android.UrlBuilderStrategy", "com.cloudinary.http42.UrlBuilderStrategy", "com.cloudinary.http43.UrlBuilderStrategy" ));
public final static String CF_SHARED_CDN = "d3jpl91pxevbkh.cloudfront.net";
public final static String OLD_AKAMAI_SHARED_CDN = "cloudinary-a.akamaihd.net";
public final static String AKAMAI_SHARED_CDN = "res.cloudinary.com";
public final static String SHARED_CDN = AKAMAI_SHARED_CDN;
public final static String VERSION = "1.1.0";
public final static String USER_AGENT = "cld-java-" + VERSION;
public final Configuration config;
private AbstractUploaderStrategy uploaderStrategy;
private AbstractApiStrategy apiStrategy;
private AbstractUrlBuilderStrategy urlBuilderStrategy;
public Uploader uploader(){
return new Uploader(this,uploaderStrategy);
};
public Api api(){
return new Api(this,apiStrategy);
};
public static void registerUploaderStrategy(String className){
if (!UPLOAD_STRATEGIES.contains(className)){
UPLOAD_STRATEGIES.add(className);
}
}
public static void registerAPIStrategy(String className){
if (!API_STRATEGIES.contains(className)){
API_STRATEGIES.add(className);
}
}
public static void registerUrlBuilderStrategy(String className){
if (!URLBUILDER_STRATEGIES.contains(className)){
URLBUILDER_STRATEGIES.add(className);
}
}
private void loadStrategies() {
uploaderStrategy= StrategyLoader.find(UPLOAD_STRATEGIES);
if (uploaderStrategy==null){
throw new UnknownError("Can't find Cloudinary platform adapter [" + StringUtils.join(UPLOAD_STRATEGIES, ",") + "]");
}
apiStrategy= StrategyLoader.find(API_STRATEGIES);
if (apiStrategy==null){
throw new UnknownError("Can't find Cloudinary platform adapter [" + StringUtils.join(API_STRATEGIES, ",") + "]");
}
urlBuilderStrategy= StrategyLoader.find(URLBUILDER_STRATEGIES);
if (urlBuilderStrategy==null){
throw new UnknownError("Can't find Cloudinary platform adapter [" + StringUtils.join(URLBUILDER_STRATEGIES, ",") + "]");
}
}
public Cloudinary(Map config) {
loadStrategies();
this.config = new Configuration(config);
}
public Cloudinary(String cloudinaryUrl) {
loadStrategies();
this.config = new Configuration(parseConfigUrl(cloudinaryUrl));
}
public Cloudinary() {
loadStrategies();
String cloudinaryUrl = System.getProperty("CLOUDINARY_URL", System.getenv("CLOUDINARY_URL"));
if (cloudinaryUrl != null) {
this.config = new Configuration(parseConfigUrl(cloudinaryUrl));
}else {
this.config = new Configuration();
}
}
public Url url() {
return new Url(this);
}
public String cloudinaryApiUrl(String action, Map options) {
String cloudinary = ObjectUtils.asString(options.get("upload_prefix"),
ObjectUtils.asString(this.config.uploadPrefix, "https://api.cloudinary.com"));
String cloud_name = ObjectUtils.asString(options.get("cloud_name"), ObjectUtils.asString(this.config.cloudName));
if (cloud_name == null)
throw new IllegalArgumentException("Must supply cloud_name in tag or in configuration");
String resource_type = ObjectUtils.asString(options.get("resource_type"), "image");
return StringUtils.join(new String[] { cloudinary, "v1_1", cloud_name, resource_type, action }, "/");
}
private final static SecureRandom RND = new SecureRandom();
public String randomPublicId() {
byte[] bytes = new byte[8];
RND.nextBytes(bytes);
return StringUtils.encodeHexString(bytes);
}
public String signedPreloadedImage(Map result) {
return result.get("resource_type") + "/upload/v" + result.get("version") + "/" + result.get("public_id")
+ (result.containsKey("format") ? "." + result.get("format") : "") + "#" + result.get("signature");
}
public String apiSignRequest(Map<String, Object> paramsToSign, String apiSecret) {
Collection<String> params = new ArrayList<String>();
for (Map.Entry<String, Object> param : new TreeMap<String, Object>(paramsToSign).entrySet()) {
if (param.getValue() instanceof Collection) {
params.add(param.getKey() + "=" + StringUtils.join((Collection) param.getValue(), ","));
} else {
if (StringUtils.isNotBlank(param.getValue())) {
params.add(param.getKey() + "=" + param.getValue().toString());
}
}
}
String to_sign = StringUtils.join(params, "&");
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Unexpected exception", e);
}
byte[] digest = md.digest((to_sign + apiSecret).getBytes());
return StringUtils.encodeHexString(digest);
}
public void signRequest(Map<String, Object> params, Map<String, Object> options) {
String apiKey = ObjectUtils.asString(options.get("api_key"), this.config.apiKey);
if (apiKey == null)
throw new IllegalArgumentException("Must supply api_key");
String apiSecret = ObjectUtils.asString(options.get("api_secret"), this.config.apiSecret);
if (apiSecret == null)
throw new IllegalArgumentException("Must supply api_secret");
Util.clearEmpty(params);
params.put("signature", this.apiSignRequest(params, apiSecret));
params.put("api_key", apiKey);
}
public String privateDownload(String publicId, String format, Map<String, Object> options) throws Exception {
Map<String, Object> params = new HashMap<String, Object>();
params.put("public_id", publicId);
params.put("format", format);
params.put("attachment", options.get("attachment"));
params.put("type", options.get("type"));
params.put("timestamp", new Long(System.currentTimeMillis() / 1000L).toString());
signRequest(params, options);
AbstractUrlBuilderStrategy builder= urlBuilderStrategy.init(cloudinaryApiUrl("download", options));
for (Map.Entry<String, Object> param : params.entrySet()) {
builder.addParam(param.getKey(), param.getValue().toString());
}
return builder.url();
}
public String zipDownload(String tag, Map<String, Object> options) throws Exception {
Map<String, Object> params = new HashMap<String, Object>();
params.put("timestamp", new Long(System.currentTimeMillis() / 1000L).toString());
params.put("tag", tag);
Object transformation = options.get("transformation");
if (transformation != null) {
if (transformation instanceof Transformation) {
transformation = ((Transformation) transformation).generate();
}
params.put("transformation", transformation.toString());
}
params.put("transformation", transformation);
signRequest(params, options);
AbstractUrlBuilderStrategy builder= urlBuilderStrategy.init(cloudinaryApiUrl("download_tag.zip", options));
for (Map.Entry<String, Object> param : params.entrySet()) {
builder.addParam(param.getKey(), param.getValue().toString());
}
return builder.url();
}
protected Map parseConfigUrl(String cloudinaryUrl) {
Map params = new HashMap();
URI cloudinaryUri = URI.create(cloudinaryUrl);
params.put("cloud_name", cloudinaryUri.getHost());
if (cloudinaryUri.getUserInfo() != null) {
String[] creds = cloudinaryUri.getUserInfo().split(":");
params.put("api_key", creds[0]);
params.put("api_secret", creds[1]);
}
params.put("private_cdn", !StringUtils.isEmpty(cloudinaryUri.getPath()));
params.put("secure_distribution", cloudinaryUri.getPath());
if (cloudinaryUri.getQuery() != null) {
for (String param : cloudinaryUri.getQuery().split("&")) {
String[] keyValue = param.split("=");
try {
params.put(keyValue[0], URLDecoder.decode(keyValue[1], "ASCII"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unexpected exception", e);
}
}
}
return params;
}
@Deprecated
public static Map asMap(Object... values) {
return ObjectUtils.asMap(values);
}
}