import type Photo from '../../data/photo'; import PowerduckState from '../../app/powerduck-state'; import { AppHttpProvider } from '../api-http'; import { DomainHelper } from './domain-helper'; const ABSOLUTE_URL_RE = /^https?:\/\//i; const LEADING_SLASH_ABSOLUTE_RE = /^\/https?:\/\//i; export default class FileHelper { static getFullPath(file: Photo) { if (file == null || file.path == null) { return null; } const rawPath = file.path.trim(); // 1. Already-absolute URL (Bunny CDN, S3, data:, protocol-relative) — return as-is. if (ABSOLUTE_URL_RE.test(rawPath) || rawPath.startsWith('//') || rawPath.startsWith('data:')) { return rawPath; } // 2. Stray leading-slash before scheme ("/https://..."): strip the slash. if (LEADING_SLASH_ABSOLUTE_RE.test(rawPath)) { return rawPath.slice(1); } // 3. Legacy relative path (e.g. "/photos/resorts/file.png") — resolve against backend domain. if (AppHttpProvider.enforceDomain == '/') { return PowerduckState.getCdnPath() + PowerduckState.getFilesPath() + rawPath; } return DomainHelper.getDomainFromUrl(AppHttpProvider.enforceDomain, true) + PowerduckState.getFilesPath() + rawPath; } }