| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- /**
- * 이미지 URL 헬퍼 composable
- */
- export const useImage = () => {
- const config = useRuntimeConfig()
- /**
- * 이미지 상대 경로를 절대 URL로 변환
- * @param {string} path - 이미지 상대 경로 (예: "/uploads/images/abc.jpg")
- * @returns {string} - 전체 URL (예: "http://gojinFORDKOREA.mycafe24.com/uploads/images/abc.jpg")
- */
- const getImageUrl = (path) => {
- if (!path) return ''
- // 이미 전체 URL인 경우 그대로 반환
- if (path.startsWith('http://') || path.startsWith('https://')) {
- return path
- }
- // 1순위: imageBase 설정값
- const imageBase = config.public.imageBase
- if (imageBase) return `${imageBase}${path}`
- // 2순위(fallback): apiBase에서 origin 추출 (로컬 개발 환경 자동 대응)
- const apiBase = config.public.apiBase
- if (apiBase) {
- try {
- return `${new URL(apiBase).origin}${path}`
- } catch (_) { /* noop */ }
- }
- return path
- }
- /**
- * 미디어 파일(이미지, 비디오) 상대 경로를 절대 URL로 변환
- * @param {string} path - 미디어 파일 상대 경로 (예: "/images/abc.jpg")
- * @returns {string} - 전체 URL (예: "http://FORDKOREA.interscope.co.kr/images/abc.jpg")
- */
- const getMediaUrl = (path) => {
- if (!path) return ''
- // 이미 전체 URL인 경우 그대로 반환
- if (path.startsWith('http://') || path.startsWith('https://')) {
- return path
- }
- // 상대 경로인 경우 mediaBase 붙이기
- const mediaBase = config.public.mediaBase
- return `${mediaBase}${path}`
- }
- return {
- getImageUrl,
- getMediaUrl
- }
- }
|