useImage.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * 이미지 URL 헬퍼 composable
  3. */
  4. export const useImage = () => {
  5. const config = useRuntimeConfig()
  6. /**
  7. * 이미지 상대 경로를 절대 URL로 변환
  8. * @param {string} path - 이미지 상대 경로 (예: "/uploads/images/abc.jpg")
  9. * @returns {string} - 전체 URL (예: "http://gojinFORDKOREA.mycafe24.com/uploads/images/abc.jpg")
  10. */
  11. const getImageUrl = (path) => {
  12. if (!path) return ''
  13. // 이미 전체 URL인 경우 그대로 반환
  14. if (path.startsWith('http://') || path.startsWith('https://')) {
  15. return path
  16. }
  17. // 1순위: imageBase 설정값
  18. const imageBase = config.public.imageBase
  19. if (imageBase) return `${imageBase}${path}`
  20. // 2순위(fallback): apiBase에서 origin 추출 (로컬 개발 환경 자동 대응)
  21. const apiBase = config.public.apiBase
  22. if (apiBase) {
  23. try {
  24. return `${new URL(apiBase).origin}${path}`
  25. } catch (_) { /* noop */ }
  26. }
  27. return path
  28. }
  29. /**
  30. * 미디어 파일(이미지, 비디오) 상대 경로를 절대 URL로 변환
  31. * @param {string} path - 미디어 파일 상대 경로 (예: "/images/abc.jpg")
  32. * @returns {string} - 전체 URL (예: "http://FORDKOREA.interscope.co.kr/images/abc.jpg")
  33. */
  34. const getMediaUrl = (path) => {
  35. if (!path) return ''
  36. // 이미 전체 URL인 경우 그대로 반환
  37. if (path.startsWith('http://') || path.startsWith('https://')) {
  38. return path
  39. }
  40. // 상대 경로인 경우 mediaBase 붙이기
  41. const mediaBase = config.public.mediaBase
  42. return `${mediaBase}${path}`
  43. }
  44. return {
  45. getImageUrl,
  46. getMediaUrl
  47. }
  48. }