Skip to content

fix v1 url generation to correctly support border objects and strings #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions __TESTS__/backwardsComaptibility/createV1URL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,25 @@ describe('Create v1 urls', () => {
expect(createCloudinaryV1URL(source, {cloud_name: 'demo'})).toEqual(`https://res.cloudinary.com/demo/image/upload/${target}`);
});
});

it("Should generate transformation with border objects", function () {
const url = createCloudinaryV1URL("sample", {
cloud_name: 'demo',
border: {
width: 5,
color: '#ffaabbdd'
}
});

expect(url).toContain(`image/upload/bo_5px_solid_rgb:ffaabbdd/sample`);
});

it("Should generate transformation with border string", function () {
const url = createCloudinaryV1URL("sample", {
cloud_name: 'demo',
border: '4px_solid_white'
});

expect(url).toContain(`image/upload/bo_4px_solid_white/sample`);
});
});
11 changes: 8 additions & 3 deletions src/backwards/generateTransformationString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export function generateTransformationString(transformationOptions: V1ITransfora
let namedTransformations = [];
let childTransformations = toArray(transformationOptions.transformation || []);
let effect = transformationOptions.effect;
let border = transformationOptions.border;

// TODO, Do we need this?
const no_html_sizes = hasLayer || angle || crop === "fit" || crop === "limit";
Expand Down Expand Up @@ -118,9 +117,15 @@ export function generateTransformationString(transformationOptions: V1ITransfora
);
}

let borderParam;
let border = transformationOptions.border;
if (isObject(border)) {
borderParam = `${border.width != null ? border.width : 2}px_solid_${(border.color != null ? border.color : "black").replace(/^#/, 'rgb:')}`;
border = `${border.width != null ? border.width : 2}px_solid_${(border.color != null ? border.color : "black").replace(/^#/, 'rgb:')}`;
} else {
// @ts-ignore
if (/^\d+$/.exec(border)) { // fallback to html border attributes
transformationOptions.border = border;
border = void 0;
}
}

if (Array.isArray(fps)) {
Expand Down
2 changes: 1 addition & 1 deletion src/backwards/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export interface V1ITransforamtionOptions {
border?: {
width?: stringOrNumber;
color?: string;
};
} | string;
default_image?: string;
density?: stringOrNumber;
format?: ImageFormat;
Expand Down
2 changes: 1 addition & 1 deletion src/backwards/utils/isObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
*
* @param a
*/
export function isObject(a: any): boolean {
export function isObject(a: any): a is Record<string, any> {
return typeof a === 'object' && a !== null;
}