|
2 | 2 |
|
3 | 3 | import java.io.UnsupportedEncodingException; |
4 | 4 | import java.net.URI; |
| 5 | +import java.net.URISyntaxException; |
5 | 6 | import java.net.URLDecoder; |
6 | 7 | import java.security.MessageDigest; |
7 | 8 | import java.security.NoSuchAlgorithmException; |
|
11 | 12 | import java.util.Collection; |
12 | 13 | import java.util.Collections; |
13 | 14 | import java.util.HashMap; |
| 15 | +import java.util.Iterator; |
14 | 16 | import java.util.List; |
15 | 17 | import java.util.Map; |
16 | 18 | import java.util.TreeMap; |
17 | 19 |
|
18 | 20 | import org.apache.commons.codec.binary.Hex; |
19 | 21 | import org.apache.commons.lang.StringUtils; |
| 22 | +import org.apache.http.client.utils.URIBuilder; |
| 23 | +import org.apache.http.client.utils.URLEncodedUtils; |
20 | 24 |
|
21 | 25 | @SuppressWarnings({"rawtypes", "unchecked"}) |
22 | 26 | public class Cloudinary { |
@@ -98,7 +102,35 @@ public String apiSignRequest(Map<String, Object> paramsToSign, String apiSecret) |
98 | 102 | byte[] digest = md.digest((to_sign + apiSecret).getBytes()); |
99 | 103 | return Hex.encodeHexString(digest); |
100 | 104 | } |
101 | | - |
| 105 | + |
| 106 | + public String privateDownload(String publicId, String format, Map<String, Object> options) throws URISyntaxException { |
| 107 | + String apiKey = Cloudinary.asString(options.get("api_key"), this.getStringConfig("api_key")); |
| 108 | + if (apiKey == null) |
| 109 | + throw new IllegalArgumentException("Must supply api_key"); |
| 110 | + String apiSecret = Cloudinary.asString(options.get("api_secret"), this.getStringConfig("api_secret")); |
| 111 | + if (apiSecret == null) |
| 112 | + throw new IllegalArgumentException("Must supply api_secret"); |
| 113 | + Map<String, Object> params = new HashMap<String, Object>(); |
| 114 | + params.put("public_id", publicId); |
| 115 | + params.put("format", format); |
| 116 | + params.put("attachment", options.get("attachment")); |
| 117 | + params.put("type", options.get("type")); |
| 118 | + for (Iterator iterator = params.values().iterator(); iterator.hasNext();) { |
| 119 | + Object value = iterator.next(); |
| 120 | + if (value == null || "".equals(value)) { |
| 121 | + iterator.remove(); |
| 122 | + } |
| 123 | + } |
| 124 | + params.put("timestamp", new Long(System.currentTimeMillis() / 1000L).toString()); |
| 125 | + params.put("signature", this.apiSignRequest(params, apiSecret)); |
| 126 | + params.put("api_key", apiKey); |
| 127 | + URIBuilder builder = new URIBuilder(cloudinaryApiUrl("download", options)); |
| 128 | + for (Map.Entry<String, Object> param : params.entrySet()) { |
| 129 | + builder.addParameter(param.getKey(), param.getValue().toString()); |
| 130 | + } |
| 131 | + return builder.toString(); |
| 132 | + } |
| 133 | + |
102 | 134 | protected void initFromUrl(String cloudinaryUrl) { |
103 | 135 | URI cloudinaryUri = URI.create(cloudinaryUrl); |
104 | 136 | setConfig("cloud_name", cloudinaryUri.getHost()); |
|
0 commit comments