Skip to content

Commit 58ee182

Browse files
authored
Add option to exclude asset version when generating cloudinary URLs. (#166)
1 parent 1026835 commit 58ee182

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class Url {
3939
Transformation posterTransformation = null;
4040
String posterSource = null;
4141
Url posterUrl = null;
42+
boolean forceVersion = true;
4243

4344
private static final String CL_BLANK = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
4445
public static final String[] DEFAULT_VIDEO_SOURCE_TYPES = {"webm", "mp4", "ogv"};
@@ -312,6 +313,19 @@ public Url poster(Object poster) {
312313
}
313314
}
314315

316+
/**
317+
* Indicates whether to add '/v1/' to the URL when the public ID includes folders and a 'version' value was
318+
* not defined.
319+
* When no version is explicitly specified and the public id contains folders, a default v1 version
320+
* is added to the url. This boolean can disable that behaviour.
321+
* @param forceVersion Whether to add the version to the url.
322+
* @return This same Url instance for chaining.
323+
*/
324+
public Url forceVersion(boolean forceVersion){
325+
this.forceVersion = forceVersion;
326+
return this;
327+
}
328+
315329
public String generate() {
316330
return generate(null);
317331
}
@@ -357,7 +371,8 @@ public String generate(String source) {
357371
source = finalizedSource[0];
358372
String sourceToSign = finalizedSource[1];
359373

360-
if (sourceToSign.contains("/") && !StringUtils.hasVersionString(sourceToSign) && !httpSource && StringUtils.isEmpty(version)) {
374+
if (forceVersion && sourceToSign.contains("/") && !StringUtils.hasVersionString(sourceToSign) &&
375+
!httpSource && StringUtils.isEmpty(version)) {
361376
version = "1";
362377
}
363378

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,29 @@ public void testFolders() {
572572
assertEquals(DEFAULT_UPLOAD_PATH + "v123/folder/test", result);
573573
}
574574

575+
@Test
576+
public void testFoldersWithExcludeVersion(){
577+
// should not add version if the user turned off forceVersion
578+
String result = cloudinary.url().forceVersion(false).generate("folder/test");
579+
assertEquals(DEFAULT_UPLOAD_PATH + "folder/test", result);
580+
581+
// should still show explicit version if passed by the user
582+
result = cloudinary.url().forceVersion(false).version("1234").generate("folder/test");
583+
assertEquals(DEFAULT_UPLOAD_PATH + "v1234/folder/test", result);
584+
585+
// should add version no value specified for forceVersion:
586+
result = cloudinary.url().generate("folder/test");
587+
assertEquals(DEFAULT_UPLOAD_PATH + "v1/folder/test", result);
588+
589+
// should add version if forceVersion is true
590+
result = cloudinary.url().forceVersion(true).generate("folder/test");
591+
assertEquals(DEFAULT_UPLOAD_PATH + "v1/folder/test", result);
592+
593+
// should not use v1 if explicit version is passed
594+
result = cloudinary.url().forceVersion(true).version("1234").generate("folder/test");
595+
assertEquals(DEFAULT_UPLOAD_PATH + "v1234/folder/test", result);
596+
}
597+
575598
@Test
576599
public void testFoldersWithVersion() {
577600
// should not add version if public_id contains version already

0 commit comments

Comments
 (0)