Files
GNX-WEB/gnx-react/lib/imageUtils.ts
Iliyan Angelov 18ae8b9f88 update
2025-10-08 13:46:46 +03:00

56 lines
1.6 KiB
TypeScript

// Image utility functions
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
export const FALLBACK_IMAGES = {
BLOG: '/images/blog/blog-poster.png',
CASE_STUDY: '/images/case/poster.png',
SERVICE: '/images/service/thumb-one.png',
GALLERY: '/images/masonry/one.png',
BANNER: '/images/banner/banner-bg.png',
DEFAULT: '/images/logo.png'
};
export function getValidImageUrl(imageUrl?: string, fallback?: string): string {
if (!imageUrl || imageUrl.trim() === '') {
return fallback || FALLBACK_IMAGES.DEFAULT;
}
// If it's already a full URL, return as is
if (imageUrl.startsWith('http://') || imageUrl.startsWith('https://')) {
return imageUrl;
}
// If it starts with /media/, it's a Django media file - prepend API base URL
if (imageUrl.startsWith('/media/')) {
return `${API_BASE_URL}${imageUrl}`;
}
// If it starts with /images/, it's a local public file
if (imageUrl.startsWith('/images/')) {
return imageUrl;
}
// If it starts with /, check if it's a media file
if (imageUrl.startsWith('/')) {
// If it contains /media/, prepend API base URL
if (imageUrl.includes('/media/')) {
return `${API_BASE_URL}${imageUrl}`;
}
return imageUrl;
}
// Otherwise, assume it's a public path and add /
return `/${imageUrl}`;
}
// Alias for backward compatibility
export const getImageUrl = getValidImageUrl;
export function getImageAlt(title?: string, fallback: string = 'Image'): string {
return title ? `${title} - Image` : fallback;
}
// Export getValidImageAlt as an alias for getImageAlt for backward compatibility
export const getValidImageAlt = getImageAlt;