mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 08:51:13 -05:00
Refactor code structure and remove redundant changes
This commit is contained in:
102
lib/cloudflare.ts
Normal file
102
lib/cloudflare.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* CloudFlare Images utility
|
||||
*
|
||||
* Provides helper functions to construct CloudFlare Images URLs
|
||||
* using the configured CDN domain (cdn.thrillwiki.com)
|
||||
*/
|
||||
|
||||
import { env } from './env';
|
||||
|
||||
/**
|
||||
* Available image variants for CloudFlare Images
|
||||
*/
|
||||
export type ImageVariant = 'public' | 'thumbnail' | 'banner' | 'avatar' | 'og';
|
||||
|
||||
/**
|
||||
* Get CloudFlare Images URL for a given image ID and variant
|
||||
*
|
||||
* @param imageId - CloudFlare image ID
|
||||
* @param variant - Image variant (default: 'public')
|
||||
* @returns Full CloudFlare CDN URL
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const url = getCloudFlareImageUrl('abc123', 'thumbnail');
|
||||
* // Returns: https://cdn.thrillwiki.com/images/abc123/thumbnail
|
||||
* ```
|
||||
*/
|
||||
export function getCloudFlareImageUrl(
|
||||
imageId: string,
|
||||
variant: ImageVariant = 'public'
|
||||
): string {
|
||||
const baseUrl = env.NEXT_PUBLIC_CLOUDFLARE_IMAGE_URL;
|
||||
|
||||
// cdn.thrillwiki.com format: {base_url}/images/{image-id}/{variant-id}
|
||||
return `${baseUrl}/images/${imageId}/${variant}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract image ID from a CloudFlare Images URL
|
||||
*
|
||||
* @param url - CloudFlare Images URL
|
||||
* @returns Image ID or null if not a valid CloudFlare URL
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const imageId = extractImageId('https://cdn.thrillwiki.com/images/abc123/public');
|
||||
* // Returns: 'abc123'
|
||||
* ```
|
||||
*/
|
||||
export function extractImageId(url: string): string | null {
|
||||
try {
|
||||
// Pattern for cdn.thrillwiki.com: /images/{id}/{variant}
|
||||
const cdnMatch = url.match(/\/images\/([^\/]+)\/[^\/]+$/);
|
||||
if (cdnMatch) {
|
||||
return cdnMatch[1];
|
||||
}
|
||||
|
||||
// Pattern for imagedelivery.net: /{hash}/{id}/{variant}
|
||||
const deliveryMatch = url.match(/imagedelivery\.net\/[^\/]+\/([^\/]+)\/[^\/]+$/);
|
||||
if (deliveryMatch) {
|
||||
return deliveryMatch[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a URL is a CloudFlare Images URL
|
||||
*
|
||||
* @param url - URL to check
|
||||
* @returns true if URL is a CloudFlare Images URL
|
||||
*/
|
||||
export function isCloudFlareImageUrl(url: string): boolean {
|
||||
return url.includes('cdn.thrillwiki.com/images') ||
|
||||
url.includes('imagedelivery.net');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get multiple variant URLs for an image
|
||||
*
|
||||
* @param imageId - CloudFlare image ID
|
||||
* @param variants - Array of variants to generate URLs for
|
||||
* @returns Object mapping variant names to URLs
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const urls = getImageVariants('abc123', ['public', 'thumbnail']);
|
||||
* // Returns: { public: '...', thumbnail: '...' }
|
||||
* ```
|
||||
*/
|
||||
export function getImageVariants(
|
||||
imageId: string,
|
||||
variants: ImageVariant[]
|
||||
): Record<ImageVariant, string> {
|
||||
return variants.reduce((acc, variant) => {
|
||||
acc[variant] = getCloudFlareImageUrl(imageId, variant);
|
||||
return acc;
|
||||
}, {} as Record<ImageVariant, string>);
|
||||
}
|
||||
Reference in New Issue
Block a user