|
| 1 | +const UPLOAD_CDN_ORIGIN = "https://cdn.upload.solid-connection.com"; |
| 2 | +const UPLOAD_CDN_HOSTNAME = "cdn.upload.solid-connection.com"; |
| 3 | +const DEFAULT_CDN_HOSTNAME = "cdn.default.solid-connection.com"; |
| 4 | +const LEGACY_BUCKET_NAME = "solid-connection"; |
| 5 | + |
| 6 | +const LOCAL_STATIC_PREFIXES = ["/images/", "/svgs/"]; |
| 7 | + |
| 8 | +const isHttpUrl = (value: string) => value.startsWith("http://") || value.startsWith("https://"); |
| 9 | + |
| 10 | +const normalizeOrigin = (value: string | undefined) => { |
| 11 | + if (!value) return undefined; |
| 12 | + const trimmed = value.trim(); |
| 13 | + if (!trimmed) return undefined; |
| 14 | + |
| 15 | + try { |
| 16 | + const parsed = new URL(trimmed); |
| 17 | + return `${parsed.protocol}//${parsed.host}`.replace(/\/+$/, ""); |
| 18 | + } catch { |
| 19 | + return undefined; |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +const getHostname = (value: string | undefined) => { |
| 24 | + if (!value) return undefined; |
| 25 | + |
| 26 | + try { |
| 27 | + return new URL(value).hostname.toLowerCase(); |
| 28 | + } catch { |
| 29 | + return undefined; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +const envUploadOrigin = normalizeOrigin(process.env.NEXT_PUBLIC_UPLOADED_IMAGE_URL); |
| 34 | +const envDefaultOrigin = normalizeOrigin(process.env.NEXT_PUBLIC_IMAGE_URL); |
| 35 | + |
| 36 | +const uploadOrigin = envUploadOrigin ?? UPLOAD_CDN_ORIGIN; |
| 37 | + |
| 38 | +const cdnHostnames = new Set( |
| 39 | + [UPLOAD_CDN_HOSTNAME, DEFAULT_CDN_HOSTNAME, getHostname(envUploadOrigin), getHostname(envDefaultOrigin)].filter( |
| 40 | + (hostname): hostname is string => Boolean(hostname), |
| 41 | + ), |
| 42 | +); |
| 43 | + |
| 44 | +const legacyS3VirtualHostRegex = new RegExp( |
| 45 | + `^${LEGACY_BUCKET_NAME.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&")}\\.s3([.-][a-z0-9-]+)?\\.amazonaws\\.com$`, |
| 46 | + "i", |
| 47 | +); |
| 48 | + |
| 49 | +const s3PathStyleHostRegex = /^s3([.-][a-z0-9-]+)?\.amazonaws\.com$/i; |
| 50 | + |
| 51 | +const joinUploadOrigin = (path: string, search = "", hash = "") => { |
| 52 | + const normalizedPath = path.startsWith("/") ? path : `/${path}`; |
| 53 | + return `${uploadOrigin}${normalizedPath}${search}${hash}`; |
| 54 | +}; |
| 55 | + |
| 56 | +const getLegacyS3ObjectPath = (pathname: string) => { |
| 57 | + const prefix = `/${LEGACY_BUCKET_NAME}/`; |
| 58 | + if (pathname.startsWith(prefix)) { |
| 59 | + return pathname.slice(prefix.length - 1); |
| 60 | + } |
| 61 | + |
| 62 | + if (pathname === `/${LEGACY_BUCKET_NAME}`) { |
| 63 | + return "/"; |
| 64 | + } |
| 65 | + |
| 66 | + return null; |
| 67 | +}; |
| 68 | + |
| 69 | +const shouldKeepAsLocalStaticPath = (value: string) => { |
| 70 | + return LOCAL_STATIC_PREFIXES.some((prefix) => value.startsWith(prefix)); |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * 이미지 URL을 upload CDN 기준으로 정규화한다. |
| 75 | + * - 상대 경로(key)는 upload CDN으로 변환 |
| 76 | + * - legacy default CDN/S3 URL은 upload CDN으로 변환 |
| 77 | + * - 로컬 정적 경로(/images, /svgs), blob/data, 외부 도메인은 유지 |
| 78 | + */ |
| 79 | +export const normalizeImageUrlToUploadCdn = (url: string | null | undefined): string => { |
| 80 | + if (!url) return ""; |
| 81 | + |
| 82 | + const trimmed = url.trim(); |
| 83 | + if (!trimmed) return ""; |
| 84 | + |
| 85 | + if (trimmed.startsWith("blob:") || trimmed.startsWith("data:")) { |
| 86 | + return trimmed; |
| 87 | + } |
| 88 | + |
| 89 | + if (trimmed.startsWith("//")) { |
| 90 | + return normalizeImageUrlToUploadCdn(`https:${trimmed}`); |
| 91 | + } |
| 92 | + |
| 93 | + if (trimmed.startsWith("/")) { |
| 94 | + if (shouldKeepAsLocalStaticPath(trimmed)) { |
| 95 | + return trimmed; |
| 96 | + } |
| 97 | + |
| 98 | + return trimmed; |
| 99 | + } |
| 100 | + |
| 101 | + if (!isHttpUrl(trimmed)) { |
| 102 | + return joinUploadOrigin(trimmed.replace(/^\/+/, "")); |
| 103 | + } |
| 104 | + |
| 105 | + let parsed: URL; |
| 106 | + try { |
| 107 | + parsed = new URL(trimmed); |
| 108 | + } catch { |
| 109 | + return trimmed; |
| 110 | + } |
| 111 | + |
| 112 | + const hostname = parsed.hostname.toLowerCase(); |
| 113 | + |
| 114 | + if (cdnHostnames.has(hostname)) { |
| 115 | + return joinUploadOrigin(parsed.pathname, parsed.search, parsed.hash); |
| 116 | + } |
| 117 | + |
| 118 | + if (legacyS3VirtualHostRegex.test(hostname)) { |
| 119 | + return joinUploadOrigin(parsed.pathname, parsed.search, parsed.hash); |
| 120 | + } |
| 121 | + |
| 122 | + if (s3PathStyleHostRegex.test(hostname)) { |
| 123 | + const objectPath = getLegacyS3ObjectPath(parsed.pathname); |
| 124 | + if (objectPath) { |
| 125 | + return joinUploadOrigin(objectPath, parsed.search, parsed.hash); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return trimmed; |
| 130 | +}; |
| 131 | + |
| 132 | +export const getUploadCdnOrigin = () => uploadOrigin; |
0 commit comments