fix: 修复本地上传资源路径解析
This commit is contained in:
@@ -12,8 +12,8 @@
|
||||
@drop="onDrop(i)"
|
||||
@dragend="onDragEnd"
|
||||
>
|
||||
<video v-if="isVideo(m)" class="thumb" :src="m.url" muted preload="metadata" />
|
||||
<img v-else class="thumb" :src="m.url" :alt="m.name" loading="lazy" />
|
||||
<video v-if="isVideo(m)" class="thumb" :src="mediaSrc(m)" muted preload="metadata" />
|
||||
<img v-else class="thumb" :src="mediaSrc(m)" :alt="m.name" loading="lazy" />
|
||||
<div v-if="isVideo(m)" class="play"><n-icon :component="PlayCircleOutline" :size="26" /></div>
|
||||
<div class="order">{{ i + 1 }}</div>
|
||||
<button class="del" type="button" :title="t('common.action.delete')" @click.stop="remove(i)">×</button>
|
||||
@@ -52,6 +52,7 @@ import { AddOutline, PlayCircleOutline } from '@vicons/ionicons5';
|
||||
import { post } from '@/api/request';
|
||||
import { message } from '@/utils/feedback';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
interface MediaItem {
|
||||
key: string;
|
||||
@@ -77,8 +78,12 @@ function updateCaption(i: number, v: string) {
|
||||
emit('update:modelValue', arr);
|
||||
}
|
||||
|
||||
function mediaSrc(m: MediaItem) {
|
||||
return resolveMediaUrl(m.url || m.key);
|
||||
}
|
||||
|
||||
function isVideo(m: MediaItem) {
|
||||
return m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(m.url || m.key || '');
|
||||
return m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(mediaSrc(m));
|
||||
}
|
||||
|
||||
async function onPick(e: Event) {
|
||||
@@ -104,7 +109,7 @@ async function uploadOne(file: File) {
|
||||
const res: any = await post(`/upload?type=${type}`, fd);
|
||||
emit('update:modelValue', [
|
||||
...list.value,
|
||||
{ key: res.key, url: res.url, name: props.captionable ? '' : res.name, type },
|
||||
{ key: res.key, url: resolveMediaUrl(res.url), name: props.captionable ? '' : res.name, type },
|
||||
]);
|
||||
} catch {
|
||||
message.error(t('common.msg.uploadFailed'));
|
||||
@@ -118,7 +123,7 @@ function remove(i: number) {
|
||||
}
|
||||
|
||||
function toViewerItem(m: MediaItem) {
|
||||
return { url: m.url || m.key, name: m.name || m.key, type: m.type };
|
||||
return { url: mediaSrc(m), name: m.name || m.key, type: m.type };
|
||||
}
|
||||
|
||||
function preview(index: number) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<div class="media-upload" :class="{ text: listType === 'text' }">
|
||||
<div v-if="listType === 'image-card'" class="image-grid">
|
||||
<div v-for="(m, i) in items" :key="m.key" class="image-item previewable" @click="preview(i)">
|
||||
<img v-if="isImage(m)" class="image-thumb" :src="m.url || m.key" :alt="m.name || m.key" loading="lazy" />
|
||||
<img v-if="isImage(m)" class="image-thumb" :src="mediaSrc(m)" :alt="m.name || m.key" loading="lazy" />
|
||||
<div v-else class="file-thumb">{{ fileExt(m) }}</div>
|
||||
<button class="remove" type="button" :title="t('common.action.delete')" @click.stop="remove(m.key)">×</button>
|
||||
</div>
|
||||
@@ -46,6 +46,7 @@ import { AddOutline, CloudUploadOutline } from '@vicons/ionicons5';
|
||||
import { post } from '@/api/request';
|
||||
import { message } from '@/utils/feedback';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
interface MediaItem {
|
||||
key: string;
|
||||
@@ -72,8 +73,12 @@ const accept = computed(() =>
|
||||
const allowMultiple = computed(() => props.max > 1);
|
||||
const canUpload = computed(() => items.value.length + uploading.value.length < props.max);
|
||||
|
||||
function mediaSrc(m: MediaItem) {
|
||||
return resolveMediaUrl(m.url || m.key);
|
||||
}
|
||||
|
||||
function isImage(m: MediaItem) {
|
||||
return props.type === 'image' || /^image\//.test(m.type || '') || /\.(png|jpe?g|gif|webp|bmp|svg)$/i.test(m.url || m.key);
|
||||
return props.type === 'image' || /^image\//.test(m.type || '') || /\.(png|jpe?g|gif|webp|bmp|svg)$/i.test(mediaSrc(m));
|
||||
}
|
||||
|
||||
function fileExt(m: MediaItem) {
|
||||
@@ -104,7 +109,7 @@ async function uploadOne(file: File) {
|
||||
const res: any = await post(`/upload?type=${props.type}`, fd);
|
||||
emit('update:modelValue', [
|
||||
...items.value,
|
||||
{ key: res.key, url: res.url, name: res.name || file.name, type: props.type },
|
||||
{ key: res.key, url: resolveMediaUrl(res.url), name: res.name || file.name, type: props.type },
|
||||
]);
|
||||
} catch {
|
||||
message.error(t('common.msg.uploadFailed'));
|
||||
@@ -119,7 +124,7 @@ function remove(key: string) {
|
||||
|
||||
function preview(index: number) {
|
||||
void openMediaViewer(
|
||||
items.value.map((m) => ({ url: m.url || m.key, name: m.name || m.key, type: m.type })),
|
||||
items.value.map((m) => ({ url: mediaSrc(m), name: m.name || m.key, type: m.type })),
|
||||
index,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import PhotoSwipe from 'photoswipe';
|
||||
import PhotoSwipeLightbox from 'photoswipe/lightbox';
|
||||
import 'photoswipe/style.css';
|
||||
import { i18n } from '@/locales';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
export interface MediaViewerItem {
|
||||
url?: string;
|
||||
@@ -308,7 +309,9 @@ function registerToolbar(lightbox: PhotoSwipeLightbox): void {
|
||||
}
|
||||
|
||||
export async function openMediaViewer(items: MediaViewerItem[], 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);
|
||||
const dataSource = await Promise.all(
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -130,14 +130,14 @@
|
||||
:model-value="toMedia(form.dynamicFields[f.fieldKey])"
|
||||
type="image"
|
||||
:max="1"
|
||||
@update:model-value="(v) => (form.dynamicFields[f.fieldKey] = v[0]?.url || '')"
|
||||
@update:model-value="(v) => (form.dynamicFields[f.fieldKey] = mediaKey(v[0]) || '')"
|
||||
/>
|
||||
<media-upload
|
||||
v-else-if="f.type === 'FILE'"
|
||||
:model-value="toMedia(form.dynamicFields[f.fieldKey])"
|
||||
type="file"
|
||||
:max="1"
|
||||
@update:model-value="(v) => (form.dynamicFields[f.fieldKey] = v[0]?.url || '')"
|
||||
@update:model-value="(v) => (form.dynamicFields[f.fieldKey] = mediaKey(v[0]) || '')"
|
||||
/>
|
||||
<n-input v-else v-model:value="form.dynamicFields[f.fieldKey]" />
|
||||
</n-form-item-gi>
|
||||
@@ -176,7 +176,7 @@
|
||||
<template v-for="m in n.media" :key="m.url || m.key">
|
||||
<video
|
||||
v-if="isVideoMedia(m)"
|
||||
:src="m.url"
|
||||
:src="mediaUrl(m)"
|
||||
controls
|
||||
preload="metadata"
|
||||
style="max-height: 90px; border-radius: 6px; background: #000"
|
||||
@@ -184,7 +184,7 @@
|
||||
<img
|
||||
v-else
|
||||
class="node-media-image"
|
||||
:src="m.url"
|
||||
:src="mediaUrl(m)"
|
||||
:alt="m.name || n.title"
|
||||
loading="lazy"
|
||||
@click="previewNodeImage(n.media, m)"
|
||||
@@ -213,7 +213,7 @@
|
||||
<img
|
||||
v-if="c.watermarkUrl || c.originUrl"
|
||||
class="cert-preview-image"
|
||||
:src="c.watermarkUrl || c.originUrl"
|
||||
:src="certUrl(c)"
|
||||
:alt="c.name"
|
||||
loading="lazy"
|
||||
@click="previewCert(c)"
|
||||
@@ -341,6 +341,7 @@ import MediaSorter from '@/components/MediaSorter.vue';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { message, dialog } from '@/utils/feedback';
|
||||
import { fmtDate, fmtSecond } from '@/utils/format';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
const { t } = useI18n();
|
||||
const route = useRoute();
|
||||
@@ -415,7 +416,7 @@ function onTemplateChange() {
|
||||
|
||||
// 动态字段 IMAGE/FILE 适配:dynamicFields 存 url 字符串,MediaUpload 需要 MediaItem[]
|
||||
function toMedia(url: unknown) {
|
||||
return url ? [{ key: String(url), url: String(url), name: String(url) }] : [];
|
||||
return url ? [{ key: String(url), url: resolveMediaUrl(String(url)), name: String(url) }] : [];
|
||||
}
|
||||
function auditTagType(s?: string) {
|
||||
return s === 'APPROVED' ? 'success' : s === 'REJECTED' ? 'error' : 'warning';
|
||||
@@ -514,7 +515,7 @@ function normalizeNode(n: any) {
|
||||
...n,
|
||||
media: media.map((m: any) => ({
|
||||
key: m.key || m.url,
|
||||
url: m.url,
|
||||
url: resolveMediaUrl(m.url || m.key),
|
||||
name: m.name,
|
||||
type: m.type || (isVideoMedia(m) ? 'video' : 'image'),
|
||||
})),
|
||||
@@ -527,8 +528,8 @@ function normalizeCert(c: any) {
|
||||
originKey: c.originKey ?? c.originFileUrl,
|
||||
watermarkKey: c.watermarkKey ?? c.watermarkFileUrl,
|
||||
issuedAt: c.issuedAt,
|
||||
originUrl: c.originUrl,
|
||||
watermarkUrl: c.watermarkUrl,
|
||||
originUrl: resolveMediaUrl(c.originUrl || c.originKey || c.originFileUrl),
|
||||
watermarkUrl: resolveMediaUrl(c.watermarkUrl || c.watermarkKey || c.watermarkFileUrl),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -672,10 +673,10 @@ const editingNode = ref<number | null>(null);
|
||||
const editingNodeLocalId = ref<string | null>(null);
|
||||
const nodeForm = reactive<any>({ nodeType: NodeType.CREATE, title: '', occurredAt: null, location: '', description: '', media: [] });
|
||||
function isVideoMedia(m: any) {
|
||||
return m.type === 'video' || /\.(mp4|webm|mov|m4v)$/i.test(m.url || m.key || '');
|
||||
return m.type === 'video' || /\.(mp4|webm|mov|m4v)$/i.test(mediaUrl(m));
|
||||
}
|
||||
function toViewerItem(m: any) {
|
||||
return { url: m.url || m.key, name: m.name || m.key, type: m.type };
|
||||
return { url: mediaUrl(m), name: m.name || m.key, type: m.type };
|
||||
}
|
||||
function previewNodeImage(media: any[] = [], current: any) {
|
||||
const images = (media || []).filter((item) => !isVideoMedia(item) && (item.url || item.key));
|
||||
@@ -683,10 +684,19 @@ function previewNodeImage(media: any[] = [], current: any) {
|
||||
void openMediaViewer(images.map(toViewerItem), Math.max(index, 0));
|
||||
}
|
||||
function previewCert(cert: any) {
|
||||
const url = cert.watermarkUrl || cert.originUrl;
|
||||
const url = certUrl(cert);
|
||||
if (!url) return;
|
||||
void openMediaViewer([{ url, name: cert.name || cert.originKey || cert.watermarkKey }]);
|
||||
}
|
||||
function mediaUrl(item?: any) {
|
||||
return resolveMediaUrl(item?.url || item?.key);
|
||||
}
|
||||
function mediaKey(item?: any) {
|
||||
return item?.key || item?.url || '';
|
||||
}
|
||||
function certUrl(cert?: any) {
|
||||
return resolveMediaUrl(cert?.watermarkUrl || cert?.originUrl || cert?.watermarkKey || cert?.originKey);
|
||||
}
|
||||
function openNode(n?: any) {
|
||||
editingNode.value = n?.id ?? null;
|
||||
editingNodeLocalId.value = n?.localId ?? null;
|
||||
@@ -698,7 +708,7 @@ function openNode(n?: any) {
|
||||
description: n?.description ?? '',
|
||||
media: (n?.media ?? []).map((m: any) => ({
|
||||
key: m.key || m.url,
|
||||
url: m.url,
|
||||
url: resolveMediaUrl(m.url || m.key),
|
||||
name: m.name,
|
||||
type: m.type || (isVideoMedia(m) ? 'video' : 'image'),
|
||||
})),
|
||||
@@ -780,7 +790,7 @@ async function saveCert() {
|
||||
normalizeCert({
|
||||
...payload,
|
||||
localId: nextLocalId('cert'),
|
||||
originUrl: certForm.files[0].url,
|
||||
originUrl: resolveMediaUrl(certForm.files[0].url || certForm.files[0].key),
|
||||
}),
|
||||
);
|
||||
message.success(t('common.msg.created'));
|
||||
|
||||
@@ -52,6 +52,7 @@ import { dialog, message } from '@/utils/feedback';
|
||||
import { renderTableFilterTitle } from '@/utils/tableFilter';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { useRemoteSort } from '@/utils/tableSort';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
const { t } = useI18n();
|
||||
const router = useRouter();
|
||||
@@ -78,7 +79,7 @@ const columns: DataTableColumns<any> = [
|
||||
render: (r) =>
|
||||
r.images?.[0]?.url
|
||||
? h(NAvatar, {
|
||||
src: r.images[0].url,
|
||||
src: mediaUrl(r.images[0]),
|
||||
size: 44,
|
||||
style: 'border-radius:8px;cursor:zoom-in',
|
||||
onClick: () => previewArtworkImages(r.images),
|
||||
@@ -228,9 +229,12 @@ function auditDisplay(r: any) {
|
||||
function goCreate() {
|
||||
router.push('/artworks/new');
|
||||
}
|
||||
function mediaUrl(item: any) {
|
||||
return resolveMediaUrl(item?.url || item?.key);
|
||||
}
|
||||
function previewArtworkImages(images: any[]) {
|
||||
void openMediaViewer(
|
||||
(images || []).map((item) => ({ url: item.url || item.key, name: item.name || item.key, type: item.type || 'image' })),
|
||||
(images || []).map((item) => ({ url: mediaUrl(item), name: item.name || item.key, type: item.type || 'image' })),
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ import { useTablePage } from '@/composables/useTablePage';
|
||||
import { renderTableFilterTitle } from '@/utils/tableFilter';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { useRemoteSort } from '@/utils/tableSort';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
const { t } = useI18n();
|
||||
const router = useRouter();
|
||||
@@ -92,7 +93,7 @@ const columns: DataTableColumns<any> = [
|
||||
render: (r) =>
|
||||
r.images?.[0]?.url
|
||||
? h(NAvatar, {
|
||||
src: r.images[0].url,
|
||||
src: mediaUrl(r.images[0]),
|
||||
size: 40,
|
||||
style: 'border-radius:8px;cursor:zoom-in',
|
||||
onClick: () => previewArtworkImages(r.images),
|
||||
@@ -173,9 +174,12 @@ function onSearch() {
|
||||
resetPage();
|
||||
load();
|
||||
}
|
||||
function mediaUrl(item: any) {
|
||||
return resolveMediaUrl(item?.url || item?.key);
|
||||
}
|
||||
function previewArtworkImages(images: any[]) {
|
||||
void openMediaViewer(
|
||||
(images || []).map((item) => ({ url: item.url || item.key, name: item.name || item.key, type: item.type || 'image' })),
|
||||
(images || []).map((item) => ({ url: mediaUrl(item), name: item.name || item.key, type: item.type || 'image' })),
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ import { fmt, statusType } from '@/utils/format';
|
||||
import { useTablePage } from '@/composables/useTablePage';
|
||||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||||
import { useRemoteSort } from '@/utils/tableSort';
|
||||
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
||||
|
||||
const { t } = useI18n();
|
||||
const auth = useAuthStore();
|
||||
@@ -150,7 +151,7 @@ function renderImages(row: any) {
|
||||
width: 38,
|
||||
height: 38,
|
||||
objectFit: 'cover',
|
||||
src: item.url || item.key,
|
||||
src: mediaUrl(item),
|
||||
previewDisabled: true,
|
||||
onClick: () => previewImages(images, index),
|
||||
}),
|
||||
@@ -167,11 +168,15 @@ function renderImages(row: any) {
|
||||
|
||||
function previewImages(images: any[], index: number) {
|
||||
void openMediaViewer(
|
||||
images.map((item) => ({ url: item.url || item.key, name: item.name || item.key, type: item.type || 'image' })),
|
||||
images.map((item) => ({ url: mediaUrl(item), name: item.name || item.key, type: item.type || 'image' })),
|
||||
index,
|
||||
);
|
||||
}
|
||||
|
||||
function mediaUrl(item: any) {
|
||||
return resolveMediaUrl(item?.url || item?.key);
|
||||
}
|
||||
|
||||
async function downloadTemplate() {
|
||||
await downloadFile('/outbound/import/template', 'outbound-import-template.zip');
|
||||
}
|
||||
|
||||
@@ -19,6 +19,10 @@ export default defineConfig({
|
||||
target: 'http://localhost:3000',
|
||||
changeOrigin: true,
|
||||
},
|
||||
'/uploads': {
|
||||
target: 'http://localhost:3000',
|
||||
changeOrigin: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { mkdtempSync, rmSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
import { LocalDriver } from './local.driver';
|
||||
|
||||
describe('LocalDriver', () => {
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
function createDriver(publicPrefix = '/uploads', cdnBaseUrl = '') {
|
||||
const localDir = mkdtempSync(join(tmpdir(), 'artveris-storage-'));
|
||||
tempDirs.push(localDir);
|
||||
return new LocalDriver({
|
||||
localDir,
|
||||
publicPrefix,
|
||||
cdnBaseUrl,
|
||||
});
|
||||
}
|
||||
|
||||
it('returns a relative public URL for local uploads without CDN', () => {
|
||||
expect(createDriver().getPublicUrl('2026/07/file.jpg')).toBe('/uploads/2026/07/file.jpg');
|
||||
});
|
||||
|
||||
it('normalizes trailing slash in public prefix', () => {
|
||||
expect(createDriver('/uploads/').getPublicUrl('2026/07/file.jpg')).toBe('/uploads/2026/07/file.jpg');
|
||||
});
|
||||
|
||||
it('normalizes missing leading slash in public prefix', () => {
|
||||
expect(createDriver('uploads').getPublicUrl('2026/07/file.jpg')).toBe('/uploads/2026/07/file.jpg');
|
||||
});
|
||||
|
||||
it('uses CDN base URL when configured', () => {
|
||||
expect(createDriver('/uploads', 'https://cdn.example.com/assets/').getPublicUrl('2026/07/file.jpg')).toBe(
|
||||
'https://cdn.example.com/assets/2026/07/file.jpg',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -6,7 +6,6 @@ export interface LocalDriverOptions {
|
||||
localDir: string;
|
||||
publicPrefix: string;
|
||||
cdnBaseUrl: string;
|
||||
baseUrl: string;
|
||||
}
|
||||
|
||||
// 本地磁盘存储驱动(默认)。文件经 Nest 静态目录对外访问。
|
||||
@@ -61,10 +60,17 @@ export class LocalDriver implements StorageDriver {
|
||||
return keys;
|
||||
}
|
||||
|
||||
private getPublicPrefix(): string {
|
||||
const raw = (this.opts.publicPrefix || '/uploads').trim();
|
||||
if (raw === '/') return '';
|
||||
return `/${raw.replace(/^\/+|\/+$/g, '')}`;
|
||||
}
|
||||
|
||||
getPublicUrl(key: string): string {
|
||||
const publicPath = `${this.getPublicPrefix()}/${key}`;
|
||||
if (this.opts.cdnBaseUrl) {
|
||||
return `${this.opts.cdnBaseUrl.replace(/\/$/, '')}/${key}`;
|
||||
}
|
||||
return `${this.opts.baseUrl.replace(/\/$/, '')}${this.opts.publicPrefix}/${key}`;
|
||||
return publicPath;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +89,6 @@ export class StorageAdminService {
|
||||
localDir: dto.localDir || this.config.get<string>('storage.localDir') || './storage',
|
||||
publicPrefix: '/uploads',
|
||||
cdnBaseUrl: dto.cdnBaseUrl || '',
|
||||
baseUrl: this.config.get<string>('baseUrl') || 'http://localhost:3000',
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,6 @@ export class StorageService implements OnModuleInit {
|
||||
localDir: s.localDir,
|
||||
publicPrefix: s.publicPrefix,
|
||||
cdnBaseUrl: s.cdnBaseUrl,
|
||||
baseUrl: this.config.get<string>('baseUrl') || 'http://localhost:3000',
|
||||
});
|
||||
this.logger.log('存储驱动:本地磁盘');
|
||||
return driver;
|
||||
|
||||
@@ -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