Skip to content

Commit 8d80263

Browse files
authored
Add force_version transformation parameter
1 parent 0aacbcc commit 8d80263

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
lib-es5/*
22
*.min.js
3-
node_modules
3+
node_modules
4+
test_cache/

lib-es5/utils/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,10 @@ function url(public_id) {
729729
var transformation = utils.generate_transformation_string(options);
730730
var resource_type = utils.option_consume(options, "resource_type", "image");
731731
var version = utils.option_consume(options, "version");
732+
var force_version = utils.option_consume(options, "force_version", config().force_version);
733+
if (force_version == null) {
734+
force_version = true;
735+
}
732736
var format = utils.option_consume(options, "format");
733737
var cloud_name = utils.option_consume(options, "cloud_name", config().cloud_name);
734738
if (!cloud_name) {
@@ -783,14 +787,16 @@ function url(public_id) {
783787
public_id = _finalize_source2[0];
784788
source_to_sign = _finalize_source2[1];
785789

786-
if (source_to_sign.indexOf("/") > 0 && !source_to_sign.match(/^v[0-9]+/) && !source_to_sign.match(/^https?:\//)) {
787-
if (version == null) {
788-
version = 1;
789-
}
790+
791+
if (version == null && force_version && source_to_sign.indexOf("/") >= 0 && !source_to_sign.match(/^v[0-9]+/) && !source_to_sign.match(/^https?:\//)) {
792+
version = 1;
790793
}
791794
if (version != null) {
792795
version = `v${version}`;
796+
} else {
797+
version = null;
793798
}
799+
794800
transformation = transformation.replace(/([^:])\/\//g, '$1/');
795801
if (sign_url && isEmpty(auth_token)) {
796802
var to_sign = [transformation, source_to_sign].filter(function (part) {

lib/utils/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,10 @@ function url(public_id, options = {}) {
732732
let transformation = utils.generate_transformation_string(options);
733733
let resource_type = utils.option_consume(options, "resource_type", "image");
734734
let version = utils.option_consume(options, "version");
735+
let force_version = utils.option_consume(options, "force_version", config().force_version);
736+
if (force_version == null) {
737+
force_version = true;
738+
}
735739
let format = utils.option_consume(options, "format");
736740
let cloud_name = utils.option_consume(options, "cloud_name", config().cloud_name);
737741
if (!cloud_name) {
@@ -773,14 +777,16 @@ function url(public_id, options = {}) {
773777
}
774778
[resource_type, type] = finalize_resource_type(resource_type, type, url_suffix, use_root_path, shorten);
775779
[public_id, source_to_sign] = finalize_source(public_id, format, url_suffix);
776-
if (source_to_sign.indexOf("/") > 0 && !source_to_sign.match(/^v[0-9]+/) && !source_to_sign.match(/^https?:\//)) {
777-
if (version == null) {
778-
version = 1;
779-
}
780+
781+
if (version == null && force_version && source_to_sign.indexOf("/") >= 0 && !source_to_sign.match(/^v[0-9]+/) && !source_to_sign.match(/^https?:\//)) {
782+
version = 1;
780783
}
781784
if (version != null) {
782785
version = `v${version}`;
786+
} else {
787+
version = null;
783788
}
789+
784790
transformation = transformation.replace(/([^:])\/\//g, '$1/');
785791
if (sign_url && isEmpty(auth_token)) {
786792
let to_sign = [transformation, source_to_sign].filter(function (part) {

test/utils_spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,28 @@ describe("utils", function () {
12571257
it("should not add version if public_id contains version already", function () {
12581258
test_cloudinary_url("v1234/test", {}, `http://res.cloudinary.com/${cloud_name}/image/upload/v1234/test`, {});
12591259
});
1260+
it("should not set default version v1 to resources stored in folders if force_version is set to false", function () {
1261+
test_cloudinary_url("folder/test", {},
1262+
`http://res.cloudinary.com/${cloud_name}/image/upload/v1/folder/test`, {});
1263+
test_cloudinary_url("folder/test",
1264+
{ force_version: false }, `http://res.cloudinary.com/${cloud_name}/image/upload/folder/test`, {});
1265+
});
1266+
it("explicitly set version is always passed", function () {
1267+
test_cloudinary_url("test",
1268+
{ force_version: false, version: '1234' }, `http://res.cloudinary.com/${cloud_name}/image/upload/v1234/test`, {});
1269+
test_cloudinary_url("folder/test",
1270+
{ force_version: false, version: '1234' }, `http://res.cloudinary.com/${cloud_name}/image/upload/v1234/folder/test`, {});
1271+
});
1272+
it("should use force_version from config", function () {
1273+
cloudinary.config({ force_version: false });
1274+
test_cloudinary_url("folder/test",
1275+
{}, `http://res.cloudinary.com/${cloud_name}/image/upload/folder/test`, {});
1276+
});
1277+
it("should override config with options", function () {
1278+
cloudinary.config({ force_version: false });
1279+
test_cloudinary_url("folder/test",
1280+
{ force_version: true }, `http://res.cloudinary.com/${cloud_name}/image/upload/v1/folder/test`, {});
1281+
});
12601282
it("should allow to shorted image/upload urls", function () {
12611283
test_cloudinary_url("test", {
12621284
shorten: true,

0 commit comments

Comments
 (0)