Skip to content

Commit d887dea

Browse files
committed
support createArchive
1 parent 1597c3a commit d887dea

File tree

6 files changed

+352
-21
lines changed

6 files changed

+352
-21
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package com.cloudinary;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class ArchiveParams {
8+
public static final String FORMAT_ZIP = "zip";
9+
10+
public static final String MODE_DOWNLOAD = "download";
11+
public static final String MODE_CREATE = "create";
12+
13+
private String resourceType = "image";
14+
private String type = null;
15+
private String mode = MODE_CREATE;
16+
private String targetFormat = null;
17+
private String targetPublicId = null;
18+
private boolean flattenFolders = false;
19+
private boolean flattenTransformations = false;
20+
private boolean useOriginalFilename = false;
21+
private boolean async = false;
22+
private boolean keepDerived = false;
23+
private String notificationUrl = null;
24+
private String[] targetTags = null;
25+
private String[] tags = null;
26+
private String[] publicIds = null;
27+
private String[] prefixes = null;
28+
private Transformation[] transformations = null;
29+
30+
public String resourceType() {
31+
return resourceType;
32+
}
33+
34+
public ArchiveParams resourceType(String resourceType) {
35+
if (resourceType == null)
36+
throw new IllegalArgumentException("resource type must be non-null");
37+
this.resourceType = resourceType;
38+
return this;
39+
}
40+
41+
public String type() {
42+
return type;
43+
}
44+
45+
public ArchiveParams type(String type) {
46+
this.type = type;
47+
return this;
48+
}
49+
50+
public String mode() {
51+
return mode;
52+
}
53+
54+
public ArchiveParams mode(String mode) {
55+
this.mode = mode;
56+
return this;
57+
}
58+
59+
public String targetFormat() {
60+
return targetFormat;
61+
}
62+
63+
public ArchiveParams targetFormat(String targetFormat) {
64+
this.targetFormat = targetFormat;
65+
return this;
66+
}
67+
68+
public String targetPublicId() {
69+
return targetPublicId;
70+
}
71+
72+
public ArchiveParams targetPublicId(String targetPublicId) {
73+
this.targetPublicId = targetPublicId;
74+
return this;
75+
}
76+
77+
public boolean isFlattenFolders() {
78+
return flattenFolders;
79+
}
80+
81+
public ArchiveParams flattenFolders(boolean flattenFolders) {
82+
this.flattenFolders = flattenFolders;
83+
return this;
84+
}
85+
86+
public boolean isFlattenTransformations() {
87+
return flattenTransformations;
88+
}
89+
90+
public ArchiveParams flattenTransformations(boolean flattenTransformations) {
91+
this.flattenTransformations = flattenTransformations;
92+
return this;
93+
}
94+
95+
public boolean isUseOriginalFilename() {
96+
return useOriginalFilename;
97+
}
98+
99+
public ArchiveParams useOriginalFilename(boolean useOriginalFilename) {
100+
this.useOriginalFilename = useOriginalFilename;
101+
return this;
102+
}
103+
104+
public boolean isAsync() {
105+
return async;
106+
}
107+
108+
public ArchiveParams async(boolean async) {
109+
this.async = async;
110+
return this;
111+
}
112+
113+
public boolean isKeepDerived() {
114+
return keepDerived;
115+
}
116+
117+
public ArchiveParams keepDerived(boolean keepDerived) {
118+
this.keepDerived = keepDerived;
119+
return this;
120+
}
121+
122+
public String notificationUrl() {
123+
return notificationUrl;
124+
}
125+
126+
public ArchiveParams notificationUrl(String notificationUrl) {
127+
this.notificationUrl = notificationUrl;
128+
return this;
129+
}
130+
131+
public String[] targetTags() {
132+
return targetTags;
133+
}
134+
135+
public ArchiveParams targetTags(String[] targetTags) {
136+
this.targetTags = targetTags;
137+
return this;
138+
}
139+
140+
public String[] tags() {
141+
return tags;
142+
}
143+
144+
public ArchiveParams tags(String[] tags) {
145+
this.tags = tags;
146+
return this;
147+
}
148+
149+
public String[] publicIds() {
150+
return publicIds;
151+
}
152+
153+
public ArchiveParams publicIds(String[] publicIds) {
154+
this.publicIds = publicIds;
155+
return this;
156+
}
157+
158+
public String[] prefixes() {
159+
return prefixes;
160+
}
161+
162+
public ArchiveParams prefixes(String[] prefixes) {
163+
this.prefixes = prefixes;
164+
return this;
165+
}
166+
167+
public Transformation[] transformations() {
168+
return transformations;
169+
}
170+
171+
public ArchiveParams transformations(Transformation[] transformations) {
172+
this.transformations = transformations;
173+
return this;
174+
}
175+
176+
public Map<String, Object> toMap() {
177+
Map<String, Object> params = new HashMap<String, Object>();
178+
params.put("resource_type", resourceType);
179+
params.put("type", type);
180+
params.put("mode", mode);
181+
if (targetPublicId != null)
182+
params.put("target_public_id", targetPublicId);
183+
params.put("flatten_folders", flattenFolders);
184+
params.put("flatten_transformations", flattenTransformations);
185+
params.put("use_original_filename", useOriginalFilename);
186+
params.put("async", async);
187+
params.put("keep_derived", keepDerived);
188+
if (notificationUrl != null)
189+
params.put("notification_url", notificationUrl);
190+
if (targetTags != null)
191+
params.put("target_tags", targetTags);
192+
if (tags != null)
193+
params.put("tags", tags);
194+
if (publicIds != null)
195+
params.put("public_ids", publicIds);
196+
if (prefixes != null)
197+
params.put("prefixes", prefixes);
198+
if (transformations != null) {
199+
params.put("transformations", Arrays.asList(transformations));
200+
}
201+
return params;
202+
}
203+
}

cloudinary-core/src/main/java/com/cloudinary/Cloudinary.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public String apiSignRequest(Map<String, Object> paramsToSign, String apiSecret)
135135
for (Map.Entry<String, Object> param : new TreeMap<String, Object>(paramsToSign).entrySet()) {
136136
if (param.getValue() instanceof Collection) {
137137
params.add(param.getKey() + "=" + StringUtils.join((Collection) param.getValue(), ","));
138+
} else if (param.getValue() instanceof Object[]) {
139+
params.add(param.getKey() + "=" + StringUtils.join((Object[]) param.getValue(), ","));
138140
} else {
139141
if (StringUtils.isNotBlank(param.getValue())) {
140142
params.add(param.getKey() + "=" + param.getValue().toString());
@@ -170,14 +172,14 @@ public String privateDownload(String publicId, String format, Map<String, Object
170172
params.put("format", format);
171173
params.put("attachment", options.get("attachment"));
172174
params.put("type", options.get("type"));
173-
params.put("timestamp", new Long(System.currentTimeMillis() / 1000L).toString());
175+
params.put("timestamp", Util.timestamp());
174176
signRequest(params, options);
175177
return buildUrl(cloudinaryApiUrl("download", options), params);
176178
}
177179

178180
public String zipDownload(String tag, Map<String, Object> options) throws Exception {
179181
Map<String, Object> params = new HashMap<String, Object>();
180-
params.put("timestamp", new Long(System.currentTimeMillis() / 1000L).toString());
182+
params.put("timestamp", Util.timestamp());
181183
params.put("tag", tag);
182184
Object transformation = options.get("transformation");
183185
if (transformation != null) {
@@ -191,6 +193,22 @@ public String zipDownload(String tag, Map<String, Object> options) throws Except
191193
return buildUrl(cloudinaryApiUrl("download_tag.zip", options), params);
192194
}
193195

196+
public String downloadArchive(Map<String, Object> options, String targetFormat) throws UnsupportedEncodingException {
197+
Map params = Util.buildArchiveParams(options, targetFormat);
198+
params.put("mode", ArchiveParams.MODE_DOWNLOAD);
199+
signRequest(params, options);
200+
return buildUrl(cloudinaryApiUrl("generate_archive", options), params);
201+
}
202+
203+
public String downloadArchive(ArchiveParams params) throws UnsupportedEncodingException {
204+
return downloadArchive(params.toMap(), params.targetFormat());
205+
}
206+
207+
public String downloadZip(Map<String, Object> options) throws UnsupportedEncodingException {
208+
return downloadArchive(options, "zip");
209+
}
210+
211+
194212
private String buildUrl(String base, Map<String, Object> params) throws UnsupportedEncodingException {
195213
StringBuilder urlBuilder = new StringBuilder();
196214
urlBuilder.append(base);
@@ -199,9 +217,23 @@ private String buildUrl(String base, Map<String, Object> params) throws Unsuppor
199217
}
200218
boolean first = true;
201219
for (Map.Entry<String, Object> param : params.entrySet()) {
220+
String keyValue = null;
221+
Object value = param.getValue();
202222
if (!first) urlBuilder.append("&");
203-
urlBuilder.append(param.getKey()).append("=").append(
204-
URLEncoder.encode(param.getValue().toString(), "UTF-8"));
223+
if (value instanceof Object[])
224+
value = Arrays.asList(value);
225+
if (value instanceof Collection) {
226+
String key = param.getKey() + "[]=";
227+
Collection<Object> items = (Collection) value;
228+
List<String> encodedItems = new ArrayList<String>();
229+
for (Object item : items)
230+
encodedItems.add(URLEncoder.encode(item.toString(), "UTF-8"));
231+
keyValue = key + StringUtils.join(encodedItems, "&" + key);
232+
} else {
233+
keyValue = param.getKey() + "=" +
234+
URLEncoder.encode(value.toString(), "UTF-8");
235+
}
236+
urlBuilder.append(keyValue);
205237
first = false;
206238
}
207239
return urlBuilder.toString();

cloudinary-core/src/main/java/com/cloudinary/ResponsiveBreakpoints.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static String toJsonString(Object breakpoints) {
8181

8282
JSONArray arr = new JSONArray();
8383
if (breakpoints instanceof ResponsiveBreakpoints) {
84-
arr.put(0, ((ResponsiveBreakpoints) breakpoints).toJson());
84+
arr.put(((ResponsiveBreakpoints) breakpoints).toJson());
8585
} else if (breakpoints instanceof ResponsiveBreakpoints[]) {
8686
for (ResponsiveBreakpoints i : (ResponsiveBreakpoints[]) breakpoints) {
8787
arr.put(i.toJson());

cloudinary-core/src/main/java/com/cloudinary/Uploader.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,13 @@ public Map explicit(String publicId, Map options) throws IOException {
197197
params.put("invalidate", ObjectUtils.asBoolean(options.get("invalidate"), false).toString());
198198
return callApi("explicit", params, options, null);
199199
}
200-
200+
201+
@Deprecated
201202
public Map generate_sprite(String tag, Map options) throws IOException {
203+
return generateSprite(tag, options);
204+
}
205+
206+
public Map generateSprite(String tag, Map options) throws IOException {
202207
if (options == null)
203208
options = ObjectUtils.emptyMap();
204209
Map<String, Object> params = new HashMap<String, Object>();
@@ -303,9 +308,23 @@ public Map text(String text, Map options) throws IOException {
303308
}
304309
return callApi("text", params, options, null);
305310
}
311+
312+
public Map createArchive(Map options, String targetFormat) throws IOException {
313+
Map params = Util.buildArchiveParams(options, targetFormat);
314+
return callApi("generate_archive", params, options, null);
315+
}
316+
317+
public Map createZip(Map options) throws IOException {
318+
return createArchive(options, "zip");
319+
}
320+
321+
public Map createArchive(ArchiveParams params) throws IOException {
322+
return createArchive(params.toMap(), params.targetFormat());
323+
}
306324

307325
public void signRequestParams(Map<String, Object> params, Map options) {
308-
params.put("timestamp", new Long(System.currentTimeMillis() / 1000L).toString());
326+
if (!params.containsKey("timestamp"))
327+
params.put("timestamp", Util.timestamp());
309328
cloudinary.signRequest(params, options);
310329
}
311330

cloudinary-core/src/main/java/com/cloudinary/Util.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,39 @@ public static void clearEmpty(Map params) {
145145
}
146146
}
147147

148+
@SuppressWarnings({ "rawtypes", "unchecked" })
149+
public static final Map<String, Object> buildArchiveParams(Map options, String targetFormat) {
150+
if (options == null)
151+
options = ObjectUtils.emptyMap();
152+
Map<String, Object> params = new HashMap<String, Object>();
153+
params.put("type", options.get("type"));
154+
params.put("mode", options.get("mode"));
155+
params.put("target_format", targetFormat);
156+
params.put("target_public_id", options.get("target_public_id"));
157+
params.put("flatten_folders", ObjectUtils.asBoolean(options.get("flatten_folders"), false));
158+
params.put("flatten_transformations", ObjectUtils.asBoolean(options.get("flatten_transformations"), false));
159+
params.put("use_original_filename", ObjectUtils.asBoolean(options.get("use_original_filename"), false));
160+
params.put("async", ObjectUtils.asBoolean(options.get("async"), false));
161+
params.put("keep_derived", ObjectUtils.asBoolean(options.get("keep_derived"), false));
162+
params.put("notification_url", options.get("notification_url"));
163+
if (options.get("target_tags") != null)
164+
params.put("target_tags", ObjectUtils.asArray(options.get("target_tags")));
165+
if (options.get("tags") != null)
166+
params.put("tags", ObjectUtils.asArray(options.get("tags")));
167+
if (options.get("public_ids") != null)
168+
params.put("public_ids", ObjectUtils.asArray(options.get("public_ids")));
169+
if (options.get("prefixes") != null)
170+
params.put("prefixes", ObjectUtils.asArray(options.get("prefixes")));
171+
if (options.get("transformations") != null)
172+
params.put("transformations", buildEager((List<Transformation>) options.get("transformations")));
173+
if (options.get("timestamp") != null)
174+
params.put("timestamp", options.get("timestamp"));
175+
else
176+
params.put("timestamp", Util.timestamp());
177+
return params;
178+
}
179+
180+
protected static String timestamp() {
181+
return new Long(System.currentTimeMillis() / 1000L).toString();
182+
}
148183
}

0 commit comments

Comments
 (0)