Skip to content
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
13 changes: 13 additions & 0 deletions __TESTS__/unit/asset/CloudinaryFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {CloudinaryFile} from "../../../src/assets/CloudinaryFile";
import {Underlay} from "../../../src/actions/underlay";
import {Source} from "../../../src/qualifiers/source";

describe('Tests for CloudinaryFile', () => {
let cloudinaryFile: CloudinaryFile = null;
Expand All @@ -21,5 +23,16 @@ describe('Tests for CloudinaryFile', () => {
it('Can be turned to a URL', () => {
expect(cloudinaryFile.toURL()).toBe('https://res.cloudinary.com/demo/image/upload/sample');
});

it('Can set private fields', () => {
cloudinaryFile
.setPublicID('sample')
.setSuffix('foo')
.setAssetType('video')
.setStorageType('fetch')
.setVersion('12345');

expect(cloudinaryFile.toURL()).toContain('video/fetch/v12345/sample');
});
});

38 changes: 33 additions & 5 deletions src/assets/CloudinaryFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CloudinaryFile {
protected authToken: IAuthTokenConfig; // populated from the cloud config
protected urlConfig: IURLConfig;

public version: number;
public version: number | string;
public publicID: string;
public extension: string;
public suffix: string;
Expand All @@ -43,6 +43,30 @@ class CloudinaryFile {
return this;
}

setStorageType(newType: string): this {
this.storageType = newType;
return this;
}

setSuffix(newSuffix: string): this {
this.suffix = newSuffix;
return this;
}

setVersion(newVersion: number | string): this {
if (newVersion) {
this.version = newVersion;
}
return this;
}

setAssetType(newType: string): this {
if (newType) {
this.assetType = newType;
}
return this;
}

sign(): this {
return this;
}
Expand All @@ -57,7 +81,7 @@ class CloudinaryFile {
* @description Creates a fully qualified CloudinaryURL
* @return {string} CloudinaryURL
*/
createCloudinaryURL(transformation?: Transformation): string {
createCloudinaryURL(transformation?: Transformation | string): string {
if (typeof this.cloudName === 'undefined') {
throw 'You must supply a cloudName in either toURL() or when initializing the asset';
}
Expand All @@ -78,9 +102,13 @@ class CloudinaryFile {
.filter((a) => a)
.join('/');

return encodeURI(url)
.replace(/\?/g, '%3F')
.replace(/=/g, '%3D');
if (typeof transformation === 'string') {
return url;
} else {
return encodeURI(url)
.replace(/\?/g, '%3F')
.replace(/=/g, '%3D');
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal/url/cloudinaryURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function handleStorageType(storageType: string): string {
* @param {number} version
* @param {boolean} forceVersion
*/
function getUrlVersion(publicID: string, version: number, forceVersion:boolean): string {
function getUrlVersion(publicID: string, version: number | string, forceVersion:boolean): string {
const shouldForceVersion = forceVersion !== false;

if (version) {
Expand Down