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.
39 lines
1.1 KiB
39 lines
1.1 KiB
import { baseUrl } from "@/config/env"
|
|
|
|
const isAbsoluteUrl = (path) => typeof path === "string" && /^https?:\/\//i.test(path)
|
|
|
|
const getApiBase = () => (baseUrl || "").replace(/\/+$/, "")
|
|
|
|
/**
|
|
* 去掉 VITE_BASE_URL 多余前缀,支持重复前缀,得到相对路径,如 /profile/2026/...
|
|
*/
|
|
export const toRelativeFilePath = (path) => {
|
|
if (typeof path !== "string" || !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
|
|
}
|
|
|
|
/**
|
|
* 拼接为前端可访问路径,如 /profile/2026/06/12/xxx.pdf
|
|
*/
|
|
export const toFullFileUrl = (path) => {
|
|
if (typeof path !== "string" || !path) return ""
|
|
if (isAbsoluteUrl(path)) return path
|
|
|
|
const prefix = getApiBase()
|
|
const relative = toRelativeFilePath(path)
|
|
if (!prefix) return relative
|
|
return prefix + relative
|
|
}
|
|
|