22
33import java .io .UnsupportedEncodingException ;
44import java .net .URLDecoder ;
5+ import java .security .MessageDigest ;
6+ import java .security .NoSuchAlgorithmException ;
57import java .util .Map ;
68import java .util .TreeMap ;
79import java .util .zip .CRC32 ;
810
11+ import org .apache .commons .codec .binary .Base64 ;
12+ import org .apache .commons .codec .binary .Hex ;
913import org .apache .commons .lang .StringUtils ;
1014
1115public class Url {
@@ -15,12 +19,14 @@ public class Url {
1519 String secureDistribution ;
1620 boolean cdnSubdomain ;
1721 boolean shorten ;
22+ boolean signUrl ;
1823 String cname ;
1924 String type = "upload" ;
2025 String resourceType = "image" ;
2126 String format = null ;
2227 String version = null ;
2328 String source = null ;
29+ String apiSecret = null ;
2430 Transformation transformation = null ;
2531
2632 public Url (Cloudinary cloudinary ) {
@@ -31,6 +37,8 @@ public Url(Cloudinary cloudinary) {
3137 this .privateCdn = cloudinary .getBooleanConfig ("private_cdn" , false );
3238 this .cdnSubdomain = cloudinary .getBooleanConfig ("cdn_subdomain" , false );
3339 this .shorten = cloudinary .getBooleanConfig ("shorten" , false );
40+ this .signUrl = cloudinary .getBooleanConfig ("sign_url" , false );
41+ this .apiSecret = cloudinary .getStringConfig ("api_secret" );
3442 }
3543
3644 public Url type (String type ) {
@@ -117,6 +125,11 @@ public Transformation transformation() {
117125 this .transformation = new Transformation ();
118126 return this .transformation ;
119127 }
128+
129+ public Url signed (boolean signUrl ) {
130+ this .signUrl = signUrl ;
131+ return this ;
132+ }
120133
121134 public String generate (String source ) {
122135 this .source = source ;
@@ -178,9 +191,24 @@ public String generate() {
178191
179192 if (version != null )
180193 version = "v" + version ;
181-
182- return StringUtils .join (new String [] { prefix , resourceType , type , transformationStr , version , source }, "/" ).replaceAll (
183- "([^:])\\ /+" , "$1/" );
194+
195+ String rest = StringUtils .join (new String [] {transformationStr , version , source }, "/" );
196+ rest = rest .replaceAll ("^/+" , "" ).replaceAll ("([^:])\\ /+" , "$1/" );
197+
198+ if (signUrl ) {
199+ MessageDigest md = null ;
200+ try {
201+ md = MessageDigest .getInstance ("SHA-1" );
202+ }
203+ catch (NoSuchAlgorithmException e ) {
204+ throw new RuntimeException ("Unexpected exception" , e );
205+ }
206+ byte [] digest = md .digest ((rest + apiSecret ).getBytes ());
207+ String signature = Base64 .encodeBase64URLSafeString (digest );
208+ rest = "s--" + signature .substring (0 , 8 ) + "--/" + rest ;
209+ }
210+
211+ return StringUtils .join (new String [] { prefix , resourceType , type , rest }, "/" ).replaceAll ("([^:])\\ /+" , "$1/" );
184212 }
185213
186214 public String generateSpriteCss (String source ) {
0 commit comments