58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { APP_INITIALIZER, InjectionToken } from '@angular/core';
|
|
import { lastValueFrom } from 'rxjs';
|
|
|
|
import { environment } from '../../environments/environment';
|
|
|
|
export interface AppConfig {
|
|
apiBaseUrl: string;
|
|
}
|
|
|
|
export const APP_CONFIG = new InjectionToken<AppConfig>('appConfig');
|
|
|
|
// Default to the Angular environment value (http://localhost:8000 in dev, '' in prod).
|
|
// A runtime config.json can override this in production.
|
|
const _appConfig: AppConfig = {
|
|
apiBaseUrl: environment.apiBaseUrl,
|
|
};
|
|
|
|
export function appConfigFactory(http: HttpClient) {
|
|
return (): Promise<void> => {
|
|
return lastValueFrom(http.get<AppConfig>('config.json')).then(
|
|
(config) => { Object.assign(_appConfig, config); },
|
|
() => {
|
|
// Keep defaults if config.json is not available (dev mode).
|
|
},
|
|
);
|
|
};
|
|
}
|
|
|
|
export const appConfigProvider = {
|
|
provide: APP_CONFIG,
|
|
useValue: _appConfig,
|
|
};
|
|
|
|
export const appConfigInitProvider = {
|
|
provide: APP_INITIALIZER,
|
|
useFactory: appConfigFactory,
|
|
deps: [HttpClient],
|
|
multi: true,
|
|
};
|
|
|
|
export function getAppConfig(): AppConfig {
|
|
return _appConfig;
|
|
}
|
|
|
|
/** Resolve an image URL for rendering.
|
|
* - Absolute HTTP(S) URLs are returned as-is.
|
|
* - Static SVG assets (svg/…) are served by the Angular build — returned as-is.
|
|
* - API paths (/api/storage/…) and legacy paths (uploads/…) are prefixed with apiBaseUrl.
|
|
*/
|
|
export function resolveImageUrl(url: string): string {
|
|
if (!url) return '';
|
|
if (url.startsWith('http://') || url.startsWith('https://')) return url;
|
|
if (url.startsWith('svg/')) return url;
|
|
if (url.startsWith('/')) return `${getAppConfig().apiBaseUrl}${url}`;
|
|
return `${getAppConfig().apiBaseUrl}/${url}`;
|
|
}
|