You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

import { baseUrl } from "@/config/env"
const ABSOLUTE_URL_RE = /^https?:\/\//i
const isAbsoluteUrl = (path) =>
ABSOLUTE_URL_RE.test(path) || path.startsWith("blob:") || path.startsWith("data:")
const getApiBase = () => (baseUrl || "").replace(/\/+$/, "")
/**
* ȥ�� VITE_BASE_URL ����ǰ׺��֧���ظ�ǰ׺�����õ�������·������ /profile/2026/...
*/
export const toRelativeFilePath = (path) => {
if (!path) return ""
if (isAbsoluteUrl(path)) return path
const prefix = getApiBase()
let p = path.startsWith("/") ? path : `/${path}`
if (prefix) {
const normalizedPrefix = prefix.startsWith("/") ? prefix : `/${prefix}`
while (p === normalizedPrefix || p.startsWith(`${normalizedPrefix}/`)) {
p = p.slice(normalizedPrefix.length) || "/"
if (!p.startsWith("/")) p = `/${p}`
}
}
return p
}
/**
* ƴ��Ϊǰ�˿ɷ���·�����ݵȣ����ظ����ã����� uploader ���ԣ�
*/
export const toFullFileUrl = (path) => {
if (!path) return ""
if (isAbsoluteUrl(path)) return path
const prefix = getApiBase()
const relative = toRelativeFilePath(path)
if (!prefix) return relative
return prefix + relative
}