Skip to content

Commit 5eb9345

Browse files
committed
move most of stored file logic to core. support stored file in url and url and image tags. add a readme to the sample project
1 parent d480fce commit 5eb9345

File tree

14 files changed

+326
-137
lines changed

14 files changed

+326
-137
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.cloudinary;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
public class StoredFile {
9+
protected Long version;
10+
11+
protected String publicId;
12+
13+
protected String format;
14+
15+
protected String signature;
16+
17+
protected String type = "upload";
18+
19+
protected String resourceType = "image";
20+
21+
private static final String IMAGE_RESOURCE_TYPE = "image";
22+
23+
private static final String AUTO_RESOURCE_TYPE = "auto";
24+
25+
private static final Pattern PRELOADED_PATTERN = Pattern.compile("^([^\\/]+)\\/([^\\/]+)\\/v(\\d+)\\/([^#]+)#?([^\\/]+)?$");
26+
27+
public Long getVersion() {
28+
return version;
29+
}
30+
31+
public void setVersion(Long version) {
32+
this.version = version;
33+
}
34+
35+
public String getPublicId() {
36+
return publicId;
37+
}
38+
39+
public void setPublicId(String publicId) {
40+
this.publicId = publicId;
41+
}
42+
43+
protected String getPublicIdForSigning() {
44+
return publicId + ((format != null && !format.isEmpty() && resourceType.equals("raw")) ? "." + format : "");
45+
}
46+
47+
public String getFormat() {
48+
return format;
49+
}
50+
51+
public void setFormat(String format) {
52+
this.format = format;
53+
}
54+
55+
public String getSignature() {
56+
return signature;
57+
}
58+
59+
public void setSignature(String signature) {
60+
this.signature = signature;
61+
}
62+
63+
public String getResourceType() {
64+
return resourceType;
65+
}
66+
67+
public void setResourceType(String resourceType) {
68+
this.resourceType = resourceType;
69+
}
70+
71+
public String getType() {
72+
return type;
73+
}
74+
75+
public void setType(String type) {
76+
this.type = type;
77+
}
78+
79+
public String getPreloadedFile() {
80+
StringBuilder sb = new StringBuilder();
81+
sb.append(resourceType).append("/")
82+
.append(type)
83+
.append("/v").append(version).append("/")
84+
.append(publicId);
85+
if (format != null && !format.isEmpty()) {
86+
sb.append(".").append(format);
87+
}
88+
if (signature != null && !signature.isEmpty()) {
89+
sb.append("#").append(signature);
90+
}
91+
return sb.toString();
92+
}
93+
94+
public void setPreloadedFile(String uri) {
95+
if (uri.matches(PRELOADED_PATTERN.pattern())) {
96+
Matcher match = PRELOADED_PATTERN.matcher(uri);
97+
match.find();
98+
resourceType = match.group(1);
99+
type = match.group(2);
100+
version = Long.parseLong(match.group(3));
101+
String filename = match.group(4);
102+
if (match.groupCount() == 5) signature = match.group(5);
103+
int lastDotIndex = filename.lastIndexOf('.');
104+
if (lastDotIndex == -1) {
105+
publicId = filename;
106+
} else {
107+
publicId = filename.substring(0, lastDotIndex);
108+
format = filename.substring(lastDotIndex + 1);
109+
}
110+
}
111+
}
112+
113+
public String getComputedSignature(Cloudinary cloudinary) {
114+
Map<String, Object> params = new HashMap<String, Object>();
115+
params.put("version", getVersion().toString());
116+
params.put("public_id", getPublicIdForSigning());
117+
cloudinary.signRequest(params, new HashMap<String, Object>());
118+
return params.get("signature").toString();
119+
}
120+
121+
public boolean getIsImage() {
122+
return IMAGE_RESOURCE_TYPE.equals(resourceType) || AUTO_RESOURCE_TYPE.equals(resourceType);
123+
}
124+
}

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class Url {
2020
String resourceType = "image";
2121
String format = null;
2222
String version = null;
23+
String source = null;
2324
Transformation transformation = null;
2425

2526
public Url(Cloudinary cloudinary) {
@@ -37,6 +38,7 @@ public Url type(String type) {
3738
return this;
3839
}
3940

41+
@Deprecated
4042
public Url resourcType(String resourceType) {
4143
return resourceType(resourceType);
4244
}
@@ -96,13 +98,32 @@ public Url shorten(boolean shorten) {
9698
return this;
9799
}
98100

101+
public Url source(String source) {
102+
this.source = source;
103+
return this;
104+
}
105+
106+
public Url source(StoredFile source) {
107+
if (source.getResourceType() != null) resourceType = source.getResourceType();
108+
if (source.getType() != null) type = source.getType();
109+
if (source.getVersion() != null) version = source.getVersion().toString();
110+
format = source.getFormat();
111+
this.source = source.getPublicId();
112+
return this;
113+
}
114+
99115
public Transformation transformation() {
100116
if (this.transformation == null)
101117
this.transformation = new Transformation();
102118
return this.transformation;
103119
}
104120

105-
public String generate(String source) {
121+
public String generate(String source) {
122+
this.source = source;
123+
return this.generate();
124+
}
125+
126+
public String generate() {
106127
if (type.equals("fetch") && StringUtils.isNotBlank(format)) {
107128
transformation().fetchFormat(format);
108129
this.format = null;
@@ -172,8 +193,26 @@ public String imageTag(String source) {
172193
return imageTag(source, Cloudinary.emptyMap());
173194
}
174195

175-
public String imageTag(String source, Map<String, String> attributes) {
176-
String url = generate(source);
196+
public String imageTag(String source, Map<String, String> attributes) {
197+
this.source = source;
198+
return imageTag(attributes);
199+
}
200+
201+
public String imageTag() {
202+
return imageTag(Cloudinary.emptyMap());
203+
}
204+
205+
public String imageTag(StoredFile source) {
206+
return imageTag(source, Cloudinary.emptyMap());
207+
}
208+
209+
public String imageTag(StoredFile source, Map<String, String> attributes) {
210+
source(source);
211+
return imageTag(attributes);
212+
}
213+
214+
public String imageTag(Map<String, String> attributes) {
215+
String url = generate();
177216
attributes = new TreeMap<String, String>(attributes); // Make sure they are ordered.
178217
if (transformation().getHtmlHeight() != null)
179218
attributes.put("height", transformation().getHtmlHeight());

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class CloudinaryImageTag extends SimpleTagSupport implements DynamicAttri
3232
private String extraClasses = null;
3333

3434
private String src = null;
35+
private StoredFile storedSrc = null;
3536

3637
private String type = null;
3738
private String resourceType = null;
@@ -64,21 +65,26 @@ public void doTag() throws JspException, IOException {
6465
}
6566

6667
Url url = cloudinary.url();
67-
68+
if (storedSrc != null) {
69+
url.source(storedSrc);
70+
} else {
71+
url.source(src);
72+
}
6873
Transformation baseTransformation = new Transformation().params(tagAttrs);
6974
if (namedTransformation != null) baseTransformation.named(namedTransformation);
7075
url.transformation(baseTransformation.chain().rawTransformation(transformation));
7176
if (format != null) url.format(format);
7277
if (type != null) url.type(type);
7378
if (resourceType != null) url.resourceType(resourceType);
79+
7480
if (secure != null) {
7581
url.secure(secure.booleanValue());
7682
} else if(Boolean.TRUE.equals(isSecureRequest())) {
7783
url.secure(true);
7884
}
7985
if (cdnSubdomain != null) url.cdnSubdomain(cdnSubdomain.booleanValue());
8086

81-
out.println(url.imageTag(src, attributes));
87+
out.println(url.imageTag(attributes));
8288
}
8389

8490
public void setId(String id) {
@@ -101,6 +107,14 @@ public void setSrc(String src) {
101107
this.src = src;
102108
}
103109

110+
public StoredFile getStoredSrc() {
111+
return storedSrc;
112+
}
113+
114+
public void setStoredSrc(StoredFile storedSrc) {
115+
this.storedSrc = storedSrc;
116+
}
117+
104118
public String getSrc() {
105119
return src;
106120
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
public class CloudinaryUrl extends SimpleTagSupport implements DynamicAttributes {
2222

2323
private String src = null;
24+
private StoredFile storedSrc = null;
2425

2526
private String type = null;
2627
private String resourceType = null;
@@ -45,6 +46,12 @@ public void doTag() throws JspException, IOException {
4546
JspWriter out = getJspContext().getOut();
4647

4748
Url url = cloudinary.url();
49+
if (storedSrc != null) {
50+
url.source(storedSrc);
51+
} else {
52+
url.source(src);
53+
}
54+
4855
Transformation baseTransformation = new Transformation().params(tagAttrs);
4956
if (namedTransformation != null) baseTransformation.named(namedTransformation);
5057
url.transformation(baseTransformation.chain().rawTransformation(transformation));
@@ -58,13 +65,21 @@ public void doTag() throws JspException, IOException {
5865
}
5966
if (cdnSubdomain != null) url.cdnSubdomain(cdnSubdomain.booleanValue());
6067

61-
out.println(url.generate(src));
68+
out.println(url.generate());
6269
}
6370

6471
public void setSrc(String src) {
6572
this.src = src;
6673
}
6774

75+
public StoredFile getStoredSrc() {
76+
return storedSrc;
77+
}
78+
79+
public void setStoredSrc(StoredFile storedSrc) {
80+
this.storedSrc = storedSrc;
81+
}
82+
6883
public String getSrc() {
6984
return src;
7085
}

cloudinary-taglib/src/main/resources/META-INF/cloudinary.tld

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@
161161
<required>false</required> <!-- should be true but replaces publicId so can't break -->
162162
<rtexprvalue>true</rtexprvalue>
163163
</attribute>
164+
<attribute>
165+
<name>storedSrc</name>
166+
<required>false</required> <!-- should be true but replaces publicId so can't break -->
167+
<rtexprvalue>true</rtexprvalue>
168+
</attribute>
164169
<attribute>
165170
<name>publicId</name><!-- Deprecated -->
166171
<required>false</required>
@@ -209,7 +214,12 @@
209214
<body-content>scriptless</body-content>
210215
<attribute>
211216
<name>src</name>
212-
<required>true</required>
217+
<required>false</required>
218+
<rtexprvalue>true</rtexprvalue>
219+
</attribute>
220+
<attribute>
221+
<name>storedSrc</name>
222+
<required>false</required>
213223
<rtexprvalue>true</rtexprvalue>
214224
</attribute>
215225
<attribute>

samples/photo_album/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Cloudinary Java/Spring MVC Sample Project
2+
=========================================
3+
4+
A simple web application that allows you to uploads photos, maintain a database with references to them, list them with their metadata, and display them using various cloud-based transformations.
5+
6+
## Installation
7+
8+
Run the following commands from your shell.
9+
10+
Clone the Cloudinary Java project:
11+
12+
git clone git://github.com/cloudinary/cloudinary_java.git
13+
14+
Compile the parent project and install it locally, Note: This sample depends on a snapshot build of the Java client which isn't available on an external maven repository:
15+
16+
cd cloudinary_java
17+
mvn compile && mvn install
18+
19+
Compile the sample project and create a WAR file (package):
20+
21+
cd samples/photo_album
22+
mvn compile && mvn package
23+
24+
A WAR file should have been created in:
25+
26+
target/photo_album.war
27+
28+
You need to deploy this war file into your J2EE container of choice. The following instructions assume a *nix with [Tomcat](http://tomcat.apache.org/) 7 on `$CATALINA_HOME`.
29+
30+
mv target/photo_album.war $CATALINA_HOME/webapps/
31+
32+
If you would like to deploy the sample application in the root server path do this instead:
33+
34+
mv target/photo_album.war $CATALINA_HOME/webapps/ROOT.war
35+
36+
Next you need to pass your Cloudinary account's Cloud Name, API Key, and API Secret. This sample application assumes these values exists in the form
37+
of a `CLOUDINARY_URL` settings either as an environment variable or as system property. The `CLOUDINARY_URL` value is available in the [dashboard of your Cloudinary account](https://cloudinary.com/console).
38+
If you don't have a Cloudinary account yet, [click here](https://cloudinary.com/users/register/free) to create one for free.
39+
40+
The specific method with which you pass that information to Tomcat (or any other Servlet container) is not important but we present 2 alternatives here.
41+
42+
* Start Tomcat with `CLOUDINARY_URL` as a process bound environment variable
43+
44+
CLOUDINARY_URL=cloudinary://<API-KEY>:<API-SECRET>@<CLOUD-NAME> $CATALINA_HOME/bin/startup.sh
45+
46+
* Set `TOMCAT_OPTS` environment variable either globally (`/etc/profile`) or for the user running Tomcat (`~/.profile`)
47+
48+
TOMCAT_OPTS=-DCLOUDINARY_URL=cloudinary://<API-KEY>:<API-SECRET>@<CLOUD-NAME>
49+
50+
If you chose the second option you will need to now start Tomcat:
51+
52+
$CATALINA_HOME/bin/startup.sh
53+
54+
and point your browser to [http://localhost:8080/photo_album/](http://localhost:8080/photo_album/) or [http://localhost:8080/](http://localhost:8080/) if you opted to put the WAR file in root. *Note:* you might need to give Tomcat a while to pick the deployed WAR file explode it, and deploy it.
55+
56+
This sample uses an in memory [HSQLDB](http://hsqldb.org/) database so you should expect all data to be lost when the servlet process ends.

0 commit comments

Comments
 (0)