Skip to content

Commit b67eaf4

Browse files
committed
refactor Map encoding for upload
1 parent 5b25a3d commit b67eaf4

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Collection;
1313
import java.util.Collections;
1414
import java.util.HashMap;
15+
import java.util.HashSet;
1516
import java.util.Iterator;
1617
import java.util.List;
1718
import java.util.Map;
@@ -249,4 +250,19 @@ public static Map asMap(Object...values) {
249250
public static Map emptyMap() {
250251
return Collections.EMPTY_MAP;
251252
}
253+
254+
public static String encodeMap(Object arg) {
255+
if (arg != null && arg instanceof Map) {
256+
Map<String,String> mapArg = (Map<String,String>) arg;
257+
HashSet out = new HashSet();
258+
for (Map.Entry<String, String> entry : mapArg.entrySet()) {
259+
out.add(entry.getKey() + "=" + entry.getValue());
260+
}
261+
return StringUtils.join(out.toArray(), "|");
262+
} else if (arg == null) {
263+
return null;
264+
} else {
265+
return arg.toString();
266+
}
267+
}
252268
}

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,7 @@ public Map<String, Object> buildUploadParams(Map options) {
6767
params.put("face_coordinates", options.get("face_coordinates").toString());
6868
}
6969
params.put("allowed_formats", StringUtils.join(Cloudinary.asArray(options.get("allowed_formats")), ","));
70-
Map<String,String> inputContext = null;
71-
if (options.get("context") != null && options.get("context") instanceof Map) {
72-
inputContext = ((Map<String,String>) options.get("context"));
73-
}
74-
if (inputContext != null) {
75-
HashSet context = new HashSet();
76-
for (Map.Entry<String, String> entry : inputContext.entrySet()) {
77-
context.add(entry.getKey() + "=" + entry.getValue());
78-
}
79-
params.put("context", StringUtils.join(context.toArray(), "|"));
80-
} else {
81-
params.put("context", options.get("context"));
82-
}
70+
params.put("context", Cloudinary.encodeMap(options.get("context")));
8371
return params;
8472
}
8573

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@ public void testAllowedFormats() throws Exception {
198198
@Test
199199
public void testAllowedFormatsWithIllegalFormat() throws Exception {
200200
//should prevent non whitelisted formats from being uploaded if allowed_formats is specified
201-
boolean error_found = false;
201+
boolean errorFound = false;
202202
String[] formats = {"jpg"};
203203
try{
204204
cloudinary.uploader().upload("src/test/resources/logo.png", Cloudinary.asMap("allowed_formats", formats));
205205
} catch(Exception e) {
206-
error_found=true;
206+
errorFound=true;
207207
}
208-
assertTrue(error_found);
208+
assertTrue(errorFound);
209209
}
210210

211211
@Test

0 commit comments

Comments
 (0)