import { appConfig } from '../../core/config/config.js';
/**
* CDN URL Builder
* Centralized class for building CDN URLs to avoid code duplication
*/
export class CDNUrlBuilder {
/**
* Check if CDN is configured
*/
isConfigured(): boolean {
return appConfig.isCDNConfigured();
}
/**
* Get CDN base URL
*/
getBaseURL(): string {
return appConfig.getCDNBaseURL();
}
/**
* Build full CDN URL from filename
* @param filename - Image filename (e.g., IMG_1460.JPG)
* @returns Full CDN URL
* @throws Error if CDN is not configured
*/
buildURL(filename: string): string {
const baseURL = this.getBaseURL();
if (!baseURL) {
throw new Error('CDN_BASE_URL is not configured');
}
// Ensure base URL ends with / and filename doesn't start with /
const cleanBase = baseURL.endsWith('/') ? baseURL : `${baseURL}/`;
const cleanFilename = filename.startsWith('/') ? filename.slice(1) : filename;
return `${cleanBase}${cleanFilename}`;
}
/**
* Build CDN URL with directory (post, story, pin)
* @param filename - Image filename (e.g., IMG_1460.JPG)
* @param directory - Directory name: 'post', 'story', or 'pin'
* @returns Full CDN URL with directory
* @throws Error if CDN is not configured
*/
buildURLWithDirectory(
filename: string,
directory: 'post' | 'story' | 'pin'
): string {
const baseURL = this.getBaseURL();
if (!baseURL) {
throw new Error('CDN_BASE_URL is not configured');
}
// Ensure base URL ends with / and filename doesn't start with /
const cleanBase = baseURL.endsWith('/') ? baseURL : `${baseURL}/`;
const cleanFilename = filename.startsWith('/') ? filename.slice(1) : filename;
// Build URL: baseURL/directory/filename
return `${cleanBase}${directory}/${cleanFilename}`;
}
/**
* Build CDN URL for content generation (always uses 'story' directory)
* @param filename - Image filename (e.g., IMG_1460.JPG)
* @returns Full CDN URL for content generation
* @throws Error if CDN is not configured
*/
buildURLForContentGeneration(filename: string): string {
return this.buildURLWithDirectory(filename, 'story');
}
/**
* Build CDN URL for publishing based on content type
* @param filename - Image filename (e.g., IMG_1460.JPG)
* @param contentType - Content type: 'post', 'story', or 'pin'
* @returns Full CDN URL for publishing
* @throws Error if CDN is not configured
*/
buildURLForPublishing(
filename: string,
contentType: 'post' | 'story' | 'pin'
): string {
return this.buildURLWithDirectory(filename, contentType);
}
}
// Export singleton instance
export const cdnUrlBuilder = new CDNUrlBuilder();