fix: 修复本地上传资源路径解析
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import PhotoSwipe from 'photoswipe';
|
||||
import PhotoSwipeLightbox from 'photoswipe/lightbox';
|
||||
import 'photoswipe/style.css';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
export interface MediaItem {
|
||||
url?: string;
|
||||
@@ -67,7 +68,9 @@ function bindWheelZoom(lightbox: PhotoSwipeLightbox): void {
|
||||
}
|
||||
|
||||
export async function openMediaViewer(items: MediaItem[], index = 0): Promise<void> {
|
||||
const list = (items || []).filter((item) => item.url);
|
||||
const list = (items || [])
|
||||
.map((item) => ({ ...item, url: resolveMediaUrl(item.url) }))
|
||||
.filter((item) => item.url);
|
||||
if (!list.length) return;
|
||||
const start = Math.min(Math.max(index, 0), list.length - 1);
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
export function resolveMediaUrl(url?: string | null): string {
|
||||
const value = String(url || '').trim();
|
||||
if (!value) return '';
|
||||
if (/^\d{4}\/\d{2}\/\d{2}\/[^?#]+$/i.test(value)) {
|
||||
return `/uploads/${value}`;
|
||||
}
|
||||
if (/^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::\d+)?\/uploads\//i.test(value)) {
|
||||
return value.replace(/^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::\d+)?/i, '');
|
||||
}
|
||||
if (typeof window !== 'undefined' && /^https?:\/\//i.test(value)) {
|
||||
try {
|
||||
const parsed = new URL(value);
|
||||
if (parsed.origin === window.location.origin && parsed.pathname.startsWith('/uploads/')) {
|
||||
return `${parsed.pathname}${parsed.search}${parsed.hash}`;
|
||||
}
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -81,13 +81,13 @@
|
||||
>
|
||||
<video
|
||||
v-if="isVideo(s.media)"
|
||||
:src="s.media.url"
|
||||
:src="mediaUrl(s.media)"
|
||||
class="carousel-media"
|
||||
muted
|
||||
playsinline
|
||||
preload="metadata"
|
||||
/>
|
||||
<img v-else :src="s.media.url" :alt="s.media.name" class="carousel-media" loading="lazy" />
|
||||
<img v-else :src="mediaUrl(s.media)" :alt="s.media.name" class="carousel-media" loading="lazy" />
|
||||
<div v-if="isVideo(s.media)" class="play-overlay">
|
||||
<svg viewBox="0 0 64 64" width="56" height="56" aria-hidden="true">
|
||||
<circle cx="32" cy="32" r="30" fill="rgba(0,0,0,0.42)" />
|
||||
@@ -176,10 +176,10 @@
|
||||
<span class="field-value">
|
||||
<img
|
||||
v-if="f.type === 'IMAGE' && f.value"
|
||||
:src="f.value"
|
||||
:src="mediaUrl(f.value)"
|
||||
class="field-img"
|
||||
loading="lazy"
|
||||
@click="openViewer([{ url: f.value, type: 'image', name: f.label }], 0)"
|
||||
@click="openViewer([{ url: mediaUrl(f.value), type: 'image', name: f.label }], 0)"
|
||||
/>
|
||||
<a v-else-if="(f.type === 'LINK' || f.type === 'FILE') && safeUrl(f.value)" :href="safeUrl(f.value)" target="_blank" rel="noopener" class="field-link">
|
||||
{{ f.type === 'FILE' ? t('field.viewFile') : f.value }}
|
||||
@@ -208,8 +208,8 @@
|
||||
<div v-if="expanded.has(i)" class="tl-media">
|
||||
<div v-for="(m, j) in n.media" :key="j" class="tl-media-item" @click="openViewer(n.media, j)">
|
||||
<div class="tl-media-thumb">
|
||||
<video v-if="isVideo(m)" :src="m.url" muted playsinline preload="metadata" />
|
||||
<img v-else :src="m.url" loading="lazy" />
|
||||
<video v-if="isVideo(m)" :src="mediaUrl(m)" muted playsinline preload="metadata" />
|
||||
<img v-else :src="mediaUrl(m)" loading="lazy" />
|
||||
<div v-if="isVideo(m)" class="tl-play">▶</div>
|
||||
</div>
|
||||
<div v-if="m.name" class="tl-cap text-ellipsis">{{ m.name }}</div>
|
||||
@@ -237,9 +237,9 @@
|
||||
<div v-if="currentCert" class="cert">
|
||||
<img
|
||||
v-if="currentCert.url"
|
||||
:src="currentCert.url"
|
||||
:src="mediaUrl(currentCert)"
|
||||
loading="lazy"
|
||||
@click="openViewer(data.certificates.map((c) => ({ url: c.url, name: c.name, type: 'image' })), certIndex)"
|
||||
@click="openViewer(data.certificates.map((c) => ({ url: mediaUrl(c), name: c.name, type: 'image' })), certIndex)"
|
||||
/>
|
||||
<div v-else class="cert-placeholder">{{ currentCert.name }}</div>
|
||||
<div class="cert-head">
|
||||
@@ -380,6 +380,7 @@ import { fetchTrace, fetchAppInfo } from '@/api';
|
||||
import { setLocale, SUPPORTED_LOCALES } from '@/locales';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { resolveAppIcon, setFavicon } from '@/utils/appIcon';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
import ThemeSwitch from '@/components/ThemeSwitch.vue';
|
||||
|
||||
const route = useRoute();
|
||||
@@ -428,16 +429,20 @@ function ellipsis(s?: string) {
|
||||
if (!s) return '-';
|
||||
return s.length > 16 ? `${s.slice(0, 8)}…${s.slice(-6)}` : s;
|
||||
}
|
||||
function isVideo(m: { type?: string; url?: string }) {
|
||||
return m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(m.url || '');
|
||||
function mediaUrl(value?: string | { url?: string; key?: string } | null): string {
|
||||
if (typeof value === 'string') return resolveMediaUrl(value);
|
||||
return resolveMediaUrl(value?.url || value?.key);
|
||||
}
|
||||
function isVideo(m: { type?: string; url?: string; key?: string }) {
|
||||
return m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(mediaUrl(m));
|
||||
}
|
||||
// 仅允许 http(s) 或站内相对路径作为外链,过滤 javascript:/data: 等危险协议
|
||||
function safeUrl(v?: string): string {
|
||||
if (!v) return '';
|
||||
return /^(https?:\/\/|\/)/i.test(v) ? v : '';
|
||||
const url = mediaUrl(v);
|
||||
return /^(https?:\/\/|\/)/i.test(url) ? url : '';
|
||||
}
|
||||
function openViewer(items: any[], index: number) {
|
||||
openMediaViewer(items.filter((item) => item?.url), index);
|
||||
openMediaViewer(items.map((item) => ({ ...item, url: mediaUrl(item) })).filter((item) => item?.url), index);
|
||||
}
|
||||
function switchCert(step: number) {
|
||||
const total = data.value?.certificates?.length || 0;
|
||||
|
||||
@@ -15,6 +15,7 @@ export default defineConfig({
|
||||
host: true,
|
||||
proxy: {
|
||||
'/api': { target: 'http://localhost:3000', changeOrigin: true },
|
||||
'/uploads': { target: 'http://localhost:3000', changeOrigin: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user