Skip to content

Commit 9d301aa

Browse files
committed
support responsive breakpoints paramater
1 parent f41ea33 commit 9d301aa

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed
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(0, ((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+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ public Map explicit(String publicId, Map options) throws IOException {
191191
if (options.get("context") != null) {
192192
params.put("context", ObjectUtils.encodeMap(options.get("context")));
193193
}
194+
if (options.get("responsive_breakpoints") != null) {
195+
params.put("responsive_breakpoints", ResponsiveBreakpoints.toJsonString(options.get("responsive_breakpoints")));
196+
}
194197
params.put("invalidate", ObjectUtils.asBoolean(options.get("invalidate"), false).toString());
195198
return callApi("explicit", params, options, null);
196199
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.List;
77
import java.util.Map;
88

9+
import org.cloudinary.json.JSONArray;
10+
911
import com.cloudinary.utils.ObjectUtils;
1012
import com.cloudinary.utils.StringUtils;
1113

@@ -35,6 +37,7 @@ public static final Map<String, Object> buildUploadParams(Map options) {
3537
params.put("folder", (String) options.get("folder"));
3638
params.put("allowed_formats", StringUtils.join(ObjectUtils.asArray(options.get("allowed_formats")), ","));
3739
params.put("moderation", options.get("moderation"));
40+
params.put("responsive_breakpoints", ResponsiveBreakpoints.toJsonString(options.get("responsive_breakpoints")));
3841
params.put("upload_preset", options.get("upload_preset"));
3942

4043
if (options.get("signature") == null) {

cloudinary-core/src/test/java/com/cloudinary/test/CloudinaryTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.rules.TestName;
2020

2121
import com.cloudinary.Cloudinary;
22+
import com.cloudinary.ResponsiveBreakpoints;
2223
import com.cloudinary.Transformation;
2324
import com.cloudinary.transformation.*;
2425
import com.cloudinary.utils.ObjectUtils;
@@ -942,6 +943,37 @@ public void testOverlayError2() {
942943
// Must supply public_id for for non-text underlay
943944
cloudinary.url().transformation(new Transformation().underlay(new LayerBuilder().resourceType("video"))).generate("test");
944945
}
946+
947+
@Test
948+
public void testResponsiveBreakpointsToJson() {
949+
assertEquals(
950+
"[{\"create_derived\":true}]",
951+
ResponsiveBreakpoints.toJsonString(
952+
new ResponsiveBreakpoints()
953+
));
954+
955+
assertEquals(
956+
"[{\"create_derived\":false,\"max_width\":500,\"min_width\":100,\"max_images\":5,\"transformation\":\"a_45\"}]",
957+
ResponsiveBreakpoints.toJsonString(
958+
new ResponsiveBreakpoints().createDerived(false)
959+
.transformation(new Transformation().angle(45))
960+
.maxWidth(500)
961+
.minWidth(100)
962+
.maxImages(5)
963+
));
964+
assertEquals(
965+
"[{\"create_derived\":false,\"max_width\":500,\"min_width\":100,\"max_images\":5,\"transformation\":\"a_45\"},{\"create_derived\":true}]",
966+
ResponsiveBreakpoints.toJsonString(
967+
new ResponsiveBreakpoints[]{
968+
new ResponsiveBreakpoints().createDerived(false)
969+
.transformation(new Transformation().angle(45))
970+
.maxWidth(500)
971+
.minWidth(100)
972+
.maxImages(5),
973+
new ResponsiveBreakpoints()
974+
}
975+
));
976+
}
945977

946978
public static Map<String, String> getUrlParameters(URI uri) throws UnsupportedEncodingException {
947979
Map<String, String> params = new HashMap<String, String>();

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.cloudinary.Cloudinary;
2727
import com.cloudinary.Coordinates;
28+
import com.cloudinary.ResponsiveBreakpoints;
2829
import com.cloudinary.Transformation;
2930
import com.cloudinary.utils.ObjectUtils;
3031
import com.cloudinary.utils.Rectangle;
@@ -419,5 +420,15 @@ public void testFilenameOption() throws Exception {
419420
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("filename", "emanelif"));
420421
assertEquals("emanelif", result.get("original_filename"));
421422
}
423+
424+
@Test
425+
public void testResponsiveBreakpoints() throws Exception {
426+
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("responsive_breakpoints",
427+
new ResponsiveBreakpoints().maxImages(2).createDerived(false)
428+
));
429+
java.util.ArrayList breakpointsResponse = (java.util.ArrayList) result.get("responsive_breakpoints");
430+
java.util.ArrayList breakpoints = (java.util.ArrayList)((Map) breakpointsResponse.get(0)).get("breakpoints");
431+
assertEquals(2, breakpoints.size());
432+
}
422433

423434
}

0 commit comments

Comments
 (0)