返回 AiToEarn
file.util.ts
根目录 / project / aitoearn-backend / libs / common / src / utils / file.util.ts
1 import { buildUrl, zodBuildUrl, zodTrimHost } from './url.util'
2
3 export class FileUtil {
4 private static hostUrl: string
5 private static cdnEndpoint: string
6 private static s3Endpoint: string
7
8 static init(config: { endpoint: string, cdnEndpoint?: string }) {
9 this.s3Endpoint = config.endpoint
10 this.cdnEndpoint = config.cdnEndpoint || ''
11 this.hostUrl = this.cdnEndpoint || this.s3Endpoint
12 }
13
14 static buildUrl(path: string): string {
15 if (!path)
16 return path
17 return buildUrl(this.hostUrl, path)
18 }
19
20 static zodBuildUrl() {
21 return zodBuildUrl(() => this.hostUrl)
22 }
23
24 static zodTrimHost() {
25 return zodTrimHost(() => this.hostUrl)
26 }
27
28 static isStorageUrl(url: string): boolean {
29 return [this.cdnEndpoint, this.s3Endpoint]
30 .filter(Boolean)
31 .map(endpoint => endpoint.replace(/\/+$/, ''))
32 .some(endpoint => url === endpoint || url.startsWith(`${endpoint}/`))
33 }
34
35 static trimHost(url: string): string {
36 if (!url)
37 return url
38
39 if (this.cdnEndpoint && url.startsWith(this.cdnEndpoint)) {
40 return url.replace(this.cdnEndpoint, '').replace(/^\/+/, '')
41 }
42
43 if (url.startsWith(this.s3Endpoint)) {
44 return url.replace(this.s3Endpoint, '').replace(/^\/+/, '')
45 }
46
47 return url
48 }
49 }
50
50 lines TYPESCRIPT