Skip to content

Commit c90de4c

Browse files
committed
* In the tag lib, use the Uploader's tag generator
* Allow null file parameters
1 parent f5ab0f3 commit c90de4c

File tree

2 files changed

+14
-38
lines changed

2 files changed

+14
-38
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
import org.apache.http.client.methods.HttpPost;
2020
import org.apache.http.entity.mime.HttpMultipartMode;
2121
import org.apache.http.entity.mime.MultipartEntity;
22-
import org.apache.http.entity.mime.content.FileBody;
23-
import org.apache.http.entity.mime.content.StringBody;
22+
import org.apache.http.entity.mime.content.*;
2423
import org.apache.http.impl.client.DefaultHttpClient;
2524
import org.json.simple.JSONObject;
2625
import org.json.simple.JSONValue;
@@ -242,6 +241,12 @@ public Map callApi(String action, Map<String, Object> params, Map options, Objec
242241
multipart.addPart("file", new FileBody((File) file));
243242
} else if (file instanceof String) {
244243
multipart.addPart("file", new StringBody((String) file));
244+
} else if (file instanceof byte[]) {
245+
multipart.addPart("file", new ByteArrayBody((byte[]) file, "file"));
246+
} else if (file == null) {
247+
// no-problem
248+
} else {
249+
throw new IOException("Uprecognized file parameter " + file);
245250
}
246251
postMethod.setEntity(multipart);
247252

cloudinary-taglib/src/main/java/com/cloudinary/taglib/CloudinaryUploadTag.java

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,53 +28,24 @@ public void doTag() throws JspException, IOException {
2828
if (cloudinary == null) {
2929
throw new JspException("Cloudinary config could not be located");
3030
}
31+
Uploader uploader = cloudinary.uploader();
3132

32-
StringBuilder renderedHtml = new StringBuilder();
33-
34-
renderedHtml.append("<input ");
35-
36-
Map<String, String> attributes = new HashMap<String, String>();
37-
attributes.put("type", "file");
38-
attributes.put("name", name);
33+
Map<String, Object> htmlOptions = new HashMap<String, Object>();
34+
htmlOptions.put("type", "file");
35+
htmlOptions.put("name", name);
3936
if (id != null) {
40-
attributes.put("id", id);
37+
htmlOptions.put("id", id);
4138
}
4239

4340
Map<String, String> options = new HashMap<String, String>();
4441
options.put("resource_type", resourceType);
4542
if (tags != null) {
4643
options.put("tags", tags);
4744
}
48-
49-
String cloudinaryUrl = cloudinary.cloudinaryApiUrl("upload", options);
50-
51-
Uploader uploader = new Uploader(cloudinary);
52-
Map<String, Object> params = uploader.buildUploadParams(options);
53-
uploader.signRequestParams(params, options);
54-
55-
attributes.put("data-url", cloudinaryUrl);
56-
57-
StringBuilder jsonParams = new StringBuilder();
58-
jsonParams.append("{");
59-
for (Map.Entry<String, Object> entry : params.entrySet()) {
60-
if (entry.getValue() != null) {
61-
jsonParams.append("'" + entry.getKey() + "':'" + entry.getValue() + "',");
62-
}
63-
}
64-
jsonParams.append("}");
65-
String escapedJsonParams = StringEscapeUtils.escapeHtml4(jsonParams.toString());
66-
67-
attributes.put("data-form-data", escapedJsonParams);
68-
attributes.put("data-cloudinary-field", fieldName);
69-
attributes.put("class", "cloudinary-fileupload" + ((extraClasses != null) ? " " + extraClasses : ""));
70-
71-
for (Map.Entry<String, String> entry : attributes.entrySet()) {
72-
renderedHtml.append(" " + entry.getKey() + "=\"" + entry.getValue() + "\"");
73-
}
7445

75-
renderedHtml.append("/>");
46+
String renderedHtml = uploader.imageUploadTag(fieldName, options, htmlOptions);
7647

77-
getJspContext().getOut().println(renderedHtml.toString());
48+
getJspContext().getOut().println(renderedHtml);
7849
}
7950

8051
public void setId(String id) {

0 commit comments

Comments
 (0)