Refactor: Update CDN image delivery URLs

This commit is contained in:
gpt-engineer-app[bot]
2025-10-30 00:58:22 +00:00
parent 4b08836d6d
commit 9f52d423f0
7 changed files with 27 additions and 18 deletions

View File

@@ -144,8 +144,7 @@ export function UppyPhotoUpload({
if (statusResponse.ok) {
const status: UploadSuccessResponse = await statusResponse.json();
if (status.uploaded && status.urls) {
const CLOUDFLARE_ACCOUNT_HASH = import.meta.env.VITE_CLOUDFLARE_ACCOUNT_HASH;
return `https://imagedelivery.net/${CLOUDFLARE_ACCOUNT_HASH}/${cloudflareId}/public`;
return `https://cdn.thrillwiki.com/images/${cloudflareId}/public`;
}
}

View File

@@ -12,17 +12,16 @@ export type CloudflareVariant =
| 'logo'
| 'public';
const CLOUDFLARE_ACCOUNT_HASH = import.meta.env.VITE_CLOUDFLARE_ACCOUNT_HASH;
/**
* Build a Cloudflare image URL with specified variant
* Uses CDN proxy for branded URLs
*/
export function getCloudflareImageUrl(
imageId: string | undefined,
variant: CloudflareVariant = 'public'
): string | undefined {
if (!imageId) return undefined;
return `https://imagedelivery.net/${CLOUDFLARE_ACCOUNT_HASH}/${imageId}/${variant}`;
return `https://cdn.thrillwiki.com/images/${imageId}/${variant}`;
}
/**
@@ -53,9 +52,14 @@ export function getBannerUrls(imageId: string | undefined) {
/**
* Extract Cloudflare image ID from various URL formats
* Supports both old imagedelivery.net and new CDN URLs
*/
export function extractCloudflareImageId(url: string): string | null {
// Match imagedelivery.net URLs
const match = url.match(/imagedelivery\.net\/[^\/]+\/([a-f0-9-]+)\//i);
return match ? match[1] : null;
// Match old imagedelivery.net URLs
const deliveryMatch = url.match(/imagedelivery\.net\/[^\/]+\/([a-f0-9-]+)\//i);
if (deliveryMatch) return deliveryMatch[1];
// Match new cdn.thrillwiki.com URLs
const cdnMatch = url.match(/cdn\.thrillwiki\.com\/images\/([a-f0-9-]+)\//i);
return cdnMatch ? cdnMatch[1] : null;
}

View File

@@ -88,11 +88,9 @@ export async function uploadPendingImages(images: UploadedImage[]): Promise<Uplo
// Clean up object URL
URL.revokeObjectURL(image.url);
const CLOUDFLARE_ACCOUNT_HASH = import.meta.env.VITE_CLOUDFLARE_ACCOUNT_HASH;
// Step 3: Return uploaded image metadata with wasNewlyUploaded flag
return {
url: `https://imagedelivery.net/${CLOUDFLARE_ACCOUNT_HASH}/${result.result.id}/public`,
url: `https://cdn.thrillwiki.com/images/${result.result.id}/public`,
cloudflare_id: result.result.id,
caption: image.caption,
isLocal: false,

View File

@@ -105,11 +105,13 @@ export function normalizePhotoSubmissionItems(
/**
* Validate photo URL is from Cloudflare Images
* Supports both old imagedelivery.net and new CDN URLs
*/
export function isValidCloudflareUrl(url: string): boolean {
try {
const urlObj = new URL(url);
return urlObj.hostname.includes('imagedelivery.net');
return urlObj.hostname.includes('imagedelivery.net') ||
urlObj.hostname === 'cdn.thrillwiki.com';
} catch {
return false;
}

View File

@@ -1,6 +1,7 @@
import { supabase } from '@/integrations/supabase/client';
import { getErrorMessage } from './errorHandler';
import { logger } from './logger';
import { extractCloudflareImageId } from './cloudflareImageUtils';
// Core submission item interface with dependencies
// Type safety for item_data will be added in Phase 5 after fixing components
@@ -735,12 +736,17 @@ async function approvePhotos(data: any, dependencyMap: Map<string, string>, user
// Insert photos into the photos table
const photosToInsert = resolvedData.photos.map((photo: any, index: number) => {
// Extract CloudFlare image ID from URL if not provided
// Supports both old imagedelivery.net and new cdn.thrillwiki.com URLs
let cloudflareImageId = photo.cloudflare_image_id;
if (!cloudflareImageId && photo.url) {
// URL format: https://imagedelivery.net/{account_hash}/{image_id}/{variant}
cloudflareImageId = extractCloudflareImageId(photo.url);
// Fallback: parse from URL structure
if (!cloudflareImageId) {
const urlParts = photo.url.split('/');
cloudflareImageId = urlParts[urlParts.length - 2];
}
}
return {
cloudflare_image_id: cloudflareImageId,

View File

@@ -277,7 +277,7 @@ Deno.serve(async (req) => {
if (result.success) {
cloudflareImageId = result.result.id;
cloudflareImageUrl = `https://imagedelivery.net/${CLOUDFLARE_ACCOUNT_HASH}/${cloudflareImageId}/avatar`;
cloudflareImageUrl = `https://cdn.thrillwiki.com/images/${cloudflareImageId}/avatar`;
console.log('[OAuth Profile] Uploaded to Cloudflare:', { cloudflareImageId, cloudflareImageUrl });
} else {
throw new Error('Cloudflare upload failed');

View File

@@ -587,8 +587,8 @@ serve(async (req) => {
const result = imageResult.result
const duration = endRequest(tracking);
// Construct proper imagedelivery.net URLs using account hash and image ID
const baseUrl = `https://imagedelivery.net/${CLOUDFLARE_ACCOUNT_HASH}/${result.id}`
// Construct CDN URLs for display
const baseUrl = `https://cdn.thrillwiki.com/images/${result.id}`
edgeLogger.info('Image status retrieved', { action: 'get_image_status', requestId: tracking.requestId, duration });