-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassetmgmt.ts
56 lines (49 loc) · 1.58 KB
/
assetmgmt.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
// From Infinity-Next, src/utils/funcs/assetmgmt.ts
import { cdnUrl } from './consts'
import { AssetMetadata } from './generated/popplio/types'
import logger from './logger'
interface RAMPOptions {
/**
* If set, will not prepend the CDN url to the path
*
* @default false
* */
noPrependCdn?: boolean
/**
* If set, will not prepend the cache-busting query string to the path
*
* @default false
* */
noCacheBusting?: boolean
}
/** Given asset metadata, return the path to use for display
*
* Sometimes, no path may be returned. This occurs if there is no default_path whatsoever
*
* @param {AssetMetadata} assetMetadata
**/
export const resolveAssetMetadataToPath = (assetMetadata: AssetMetadata, options?: RAMPOptions) => {
if (!assetMetadata) {
return ''
}
let path: string
if (assetMetadata?.exists) {
if (!assetMetadata?.path) {
logger.warn('ResolveAssetMetadataToPath', 'assetMetadata exists but has no path', assetMetadata)
assetMetadata.path = ''
}
path = assetMetadata.path
} else {
// Use default path in this case or path if its set
// If neither is set, this will thus return an empty string
path = assetMetadata?.default_path || assetMetadata?.path || ""
}
if (!options?.noPrependCdn) {
path = `${cdnUrl}/${path}`
}
if (!options?.noCacheBusting && assetMetadata.last_modified) {
let date = Math.floor(new Date(assetMetadata.last_modified).getTime())
path += `?ts=${date}`
}
return path
}