fix:图片访问

fix:权限修改
This commit is contained in:
dzc committed 2026-10-29 14:25:46 +08:00
1 parent 6d267bcfc5
commit 653b4688cd
80 files changed
+232 -265

No files matched your search

+10 -49
View File
@@ -1,50 +1,11 @@
import { isDevEnv } from "./app"
export { fileUrl as filePath, originalFileUrl, stripFileHtml, downloadFile, previewFiles } from './file-access'
import { fileHtml } from './file-access'
const normalizeBase = (base: string | undefined) => {
if (!base) return ""
return base.endsWith("/") ? base.slice(0, -1) : base
}
const ensureLeadingSlash = (url: string) => {
if (!url) return ""
return url.startsWith("/") ? url : `/${url}`
}
export const filePath = (path?: string | null) => {
const rawPath = (path || "").trim()
if (!rawPath) return ""
if (/^https?:\/\//i.test(rawPath)) return rawPath
const devBase = normalizeBase(import.meta.env.VITE_IMAGE_DEV_PATH || import.meta.env.VITE_APP_DEV_SERVICE)
const prodBase = normalizeBase(import.meta.env.VITE_APP_PROD_IMAGE)
const base = isDevEnv() ? devBase : prodBase
if (!base) return rawPath
return `${base}${ensureLeadingSlash(rawPath)}`
}
export const parseHtml = (html: string) => {
const origin = "https://stgl.pj.gov.cn"
return html.replace(/<img\b[^>]*>/gi, (imgTag) => {
let next = imgTag
next = next.replace(/\bsrc\s*=\s*(['"])(.*?)\1/i, (_m, quote: string, src: string) => {
let resolved = src
if (resolved?.startsWith("/api")) resolved = `${origin}${resolved}`
if (resolved?.startsWith("//")) resolved = `https:${resolved}`
return `src=${quote}${resolved}${quote}`
})
const styleMatch = next.match(/\bstyle\s*=\s*(['"])([\s\S]*?)\1/i)
const required = "max-width: 100%; height: auto; display: block;"
if (styleMatch) {
const quote = styleMatch[1]
const styleValue = styleMatch[2] ?? ""
if (styleValue.includes("max-width") || styleValue.includes("width")) return next
const merged = styleValue.trim().length ? `${styleValue.replace(/;?\s*$/, "; ")}${required}` : required
return next.replace(/\bstyle\s*=\s*(['"])([\s\S]*?)\1/i, `style=${quote}${merged}${quote}`)
}
return next.replace(/<img\b/i, `<img style="${required}"`)
})
}
/** Preserve existing responsive article images while signing resource attributes. */
export const parseHtml = (html: string) => fileHtml(html || '').replace(/<img\b[^>]*>/gi, img => {
const required = 'max-width: 100%; height: auto; display: block;'
const style = img.match(/\bstyle\s*=\s*(['"])([\s\S]*?)\1/i)
if (!style) return img.replace(/<img\b/i, `<img style="${required}"`)
if (/(?:max-)?width\s*:/i.test(style[2])) return img
return img.replace(style[0], `style=${style[1]}${style[2].replace(/;?\s*$/, '; ')}${required}${style[1]}`)
})