Skip to content

Commit 2b27181

Browse files
committed
Merge branch 'master' of github.com:cloudinary/cloudinary_java
2 parents 78c5a92 + d887dea commit 2b27181

File tree

8 files changed

+576
-20
lines changed

8 files changed

+576
-20
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();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.cloudinary;
2+
3+
import org.cloudinary.json.JSONArray;
4+
import org.cloudinary.json.JSONObject;
5+
6+
public class ResponsiveBreakpoints {
7+
private boolean createDerived = true;
8+
private Transformation transformation = null;
9+
private Integer maxWidth = null; //default 1000
10+
private Integer minWidth = null; //default 50
11+
private Integer bytesStep = null; //default 20kb
12+
private Integer maxImages = null; //default 20
13+
14+
public boolean isCreateDerived() {
15+
return createDerived;
16+
}
17+
public ResponsiveBreakpoints createDerived(boolean createDerived) {
18+
this.createDerived = createDerived;
19+
return this;
20+
}
21+
22+
public Transformation transformation() {
23+
return transformation;
24+
}
25+
public ResponsiveBreakpoints transformation(Transformation transformation) {
26+
this.transformation = transformation;
27+
return this;
28+
}
29+
30+
public Integer maxWidth() {
31+
return maxWidth;
32+
}
33+
public ResponsiveBreakpoints maxWidth(Integer maxWidth) {
34+
this.maxWidth = maxWidth;
35+
return this;
36+
}
37+
38+
public Integer minWidth() {
39+
return minWidth;
40+
}
41+
public ResponsiveBreakpoints minWidth(Integer minWidth) {
42+
this.minWidth = minWidth;
43+
return this;
44+
}
45+
46+
public Integer bytesStep() {
47+
return bytesStep;
48+
}
49+
public ResponsiveBreakpoints bytesStep(Integer bytesStep) {
50+
this.bytesStep = bytesStep;
51+
return this;
52+
}
53+
54+
public Integer maxImages() {
55+
return maxImages;
56+
}
57+
public ResponsiveBreakpoints maxImages(Integer maxImages) {
58+
this.maxImages = maxImages;
59+
return this;
60+
}
61+
62+
public JSONObject toJson() {
63+
JSONObject json = new JSONObject();
64+
json.put("create_derived", createDerived);
65+
if (transformation != null)
66+
json.put("transformation", transformation.generate());
67+
if (maxWidth != null)
68+
json.put("max_width", maxWidth);
69+
if (minWidth != null)
70+
json.put("min_width", minWidth);
71+
if (bytesStep != null)
72+
json.put("bytes_step", bytesStep);
73+
if (maxImages != null)
74+
json.put("max_images", maxImages);
75+
return json;
76+
}
77+
78+
public static String toJsonString(Object breakpoints) {
79+
if (breakpoints == null)
80+
return null;
81+
82+
JSONArray arr = new JSONArray();
83+
if (breakpoints instanceof ResponsiveBreakpoints) {
84+
arr.put(((ResponsiveBreakpoints) breakpoints).toJson());
85+
} else if (breakpoints instanceof ResponsiveBreakpoints[]) {
86+
for (ResponsiveBreakpoints i : (ResponsiveBreakpoints[]) breakpoints) {
87+
arr.put(i.toJson());
88+
}
89+
} else {
90+
throw new IllegalArgumentException("breakpoints must be either of type ResponsiveBreakpoints or ResponsiveBreakpoints[]");
91+
}
92+
return arr.toString();
93+
}
94+
95+
}

0 commit comments

Comments
 (0)