| 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 trimHost(url: string): string { |
| 29 | if (!url) |
| 30 | return url |
| 31 | |
| 32 | if (this.cdnEndpoint && url.startsWith(this.cdnEndpoint)) { |
| 33 | return url.replace(this.cdnEndpoint, '').replace(/^\/+/, '') |
| 34 | } |
| 35 | |
| 36 | if (url.startsWith(this.s3Endpoint)) { |
| 37 | return url.replace(this.s3Endpoint, '').replace(/^\/+/, '') |
| 38 | } |
| 39 | |
| 40 | return url |
| 41 | } |
| 42 | } |
| 43 |