-
Notifications
You must be signed in to change notification settings - Fork 9
/
cloudinaryURL.ts
100 lines (84 loc) · 2.37 KB
/
cloudinaryURL.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import IURLConfig from "../../config/interfaces/Config/IURLConfig.js";
import {isUrl} from "./urlUtils/isUrl.js";
import {isFileName} from "./urlUtils/isFileName.js";
import {publicIDContainsVersion} from "./urlUtils/publicIDContainsVersion.js";
/**
* Create the URL prefix for Cloudinary resources.
* Available use cases
* http://res.cloudinary.com/{cloudName}
* https://res.cloudinary.com/{cloudName}
* https://{cloudName}-res.cloudinary.com/
* http://{domain}/${cloudName}
* https://{domain}/${cloudName}
* https://{domain}
* @private
*
* @param {string} cloudName
* @param {IURLConfig} urlConfig
*/
function getUrlPrefix(cloudName: string, urlConfig: IURLConfig): string {
const secure = urlConfig.secure;
const privateCDN = urlConfig.privateCdn;
const cname = urlConfig.cname;
const secureDistribution = urlConfig.secureDistribution;
if (!secure && !cname) {
return `http://res.cloudinary.com/${cloudName}`;
}
if (secure && !secureDistribution && privateCDN) {
return `https://${cloudName}-res.cloudinary.com`;
}
if (secure && !secureDistribution) {
return `https://res.cloudinary.com/${cloudName}`;
}
if (secure && secureDistribution && privateCDN) {
return `https://${secureDistribution}`;
}
if (secure && secureDistribution) {
return `https://${secureDistribution}/${cloudName}`;
}
if (!secure && cname) {
return `http://${cname}/${cloudName}`;
} else {
return 'ERROR';
}
}
/**
* @private
* @param assetType
*/
function handleAssetType(assetType: string): string {
//default to image
if (!assetType) {
return 'image';
}
return assetType;
}
/**
* @private
* @param deliveryType
*/
function handleDeliveryType(deliveryType: string): string {
//default to upload
if (!deliveryType) {
return 'upload';
}
return deliveryType;
}
/**
*
* @param {string} publicID
* @param {number} version
* @param {boolean} forceVersion
*/
function getUrlVersion(publicID: string, version: number | string, forceVersion:boolean): string {
const shouldForceVersion = forceVersion !== false;
if (version) {
return `v${version}`;
}
// In all these conditions we never force a version
if (publicIDContainsVersion(publicID) || isUrl(publicID) || isFileName(publicID)) {
return '';
}
return shouldForceVersion ? 'v1' : '';
}
export {handleAssetType, getUrlVersion, handleDeliveryType, getUrlPrefix};