|
1 | 1 | package com.cloudinary.taglib; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
4 | 6 |
|
5 | 7 | import javax.servlet.jsp.JspException; |
6 | 8 | import javax.servlet.jsp.tagext.SimpleTagSupport; |
7 | 9 |
|
| 10 | +import org.apache.commons.lang3.StringEscapeUtils; |
| 11 | + |
| 12 | +import com.cloudinary.*; |
| 13 | + |
8 | 14 | public class CloudinaryFileInputTag extends SimpleTagSupport { |
9 | 15 |
|
| 16 | + private String resourceType = "auto"; |
| 17 | + private String fieldName; |
| 18 | + private String extraClasses; |
| 19 | + |
10 | 20 | public void doTag() throws JspException, IOException { |
11 | | - String renderedHtml = ""; |
| 21 | + Cloudinary cloudinary = Singleton.getCloudinary(); |
| 22 | + if (cloudinary == null) { |
| 23 | + throw new JspException("Cloudinary config could not be located"); |
| 24 | + } |
| 25 | + |
| 26 | + StringBuilder renderedHtml = new StringBuilder(); |
| 27 | + |
| 28 | + renderedHtml.append("<input "); |
| 29 | + |
| 30 | + Map<String, String> attributes = new HashMap<String, String>(); |
| 31 | + attributes.put("type", "file"); |
| 32 | + attributes.put("name", "file"); |
| 33 | + |
| 34 | + Map<String, String> options = new HashMap<String, String>(); |
| 35 | + options.put("resource_type", resourceType); |
| 36 | + |
| 37 | + String cloudinaryUrl = cloudinary.cloudinaryApiUrl("upload", options); |
| 38 | + |
| 39 | + Uploader uploader = new Uploader(cloudinary); |
| 40 | + Map<String, Object> params = uploader.buildUploadParams(options); |
| 41 | + uploader.signRequestParams(params, options); |
| 42 | + |
| 43 | + attributes.put("data-url", cloudinaryUrl); |
| 44 | + |
| 45 | + StringBuilder jsonParams = new StringBuilder(); |
| 46 | + jsonParams.append("{"); |
| 47 | + for (Map.Entry<String, Object> entry : params.entrySet()) { |
| 48 | + renderedHtml.append("'" + entry.getKey() + "':'" + entry.getValue() + "',"); |
| 49 | + } |
| 50 | + jsonParams.append("}"); |
| 51 | + String escapedJsonParams = StringEscapeUtils.escapeHtml4(jsonParams.toString()); |
12 | 52 |
|
13 | | - getJspContext().getOut().println(renderedHtml); |
| 53 | + attributes.put("data-form-data", escapedJsonParams); |
| 54 | + attributes.put("data-cloudinary-field", fieldName); |
| 55 | + attributes.put("class", extraClasses + " cloudinary-fileupload"); |
| 56 | + |
| 57 | + for (Map.Entry<String, String> entry : attributes.entrySet()) { |
| 58 | + renderedHtml.append(" " + entry.getKey() + "='" + entry.getValue() + "'"); |
| 59 | + } |
| 60 | + |
| 61 | + renderedHtml.append("/>"); |
| 62 | + |
| 63 | + getJspContext().getOut().println(renderedHtml.toString()); |
| 64 | + } |
| 65 | + |
| 66 | + public void setResourceType(String resourceType) { |
| 67 | + this.resourceType = resourceType; |
| 68 | + } |
| 69 | + |
| 70 | + public String GetResourceType() { |
| 71 | + return resourceType; |
| 72 | + } |
| 73 | + |
| 74 | + public void setFieldName(String fieldName) { |
| 75 | + this.fieldName = fieldName; |
| 76 | + } |
| 77 | + |
| 78 | + public String getFieldName() { |
| 79 | + return fieldName; |
| 80 | + } |
| 81 | + |
| 82 | + public void setExtraClasses(String extraClasses) { |
| 83 | + this.extraClasses = extraClasses; |
| 84 | + } |
| 85 | + |
| 86 | + public String getExtraClasses() { |
| 87 | + return extraClasses; |
14 | 88 | } |
| 89 | + |
15 | 90 | } |
0 commit comments