fix:"统一媒体结果校验并补齐交付状态"
This commit is contained in:
1 parent
bb3baed095
commit
5d6253cbf1
14 files changed
+510
-106
No files matched your search
@@ -9,17 +9,44 @@ export interface MediaResult {
|
||||
}
|
||||
|
||||
const DATA_MEDIA_RE = /^data:(image|video|audio|model)\/[a-z0-9.+-]+;base64,[a-z0-9+/\s]+=*$/i
|
||||
const FORMAT_MIMES: Record<string, string> = { png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', webp: 'image/webp', gif: 'image/gif', avif: 'image/avif', mp4: 'video/mp4', webm: 'video/webm', mov: 'video/quicktime', mp3: 'audio/mpeg', wav: 'audio/wav', ogg: 'audio/ogg', opus: 'audio/ogg', aac: 'audio/aac', flac: 'audio/flac', m4a: 'audio/mp4', glb: 'model/gltf-binary', gltf: 'model/gltf+json' }
|
||||
|
||||
function mediaMime(value: unknown): string | undefined {
|
||||
if (typeof value !== 'string') return undefined
|
||||
const mime = value.trim().toLowerCase()
|
||||
return /^(image\/(png|jpeg|webp|gif|avif)|model\/gltf-binary|model\/gltf\+json|(audio|video)\/[a-z0-9.+-]+)$/.test(mime) && !mime.endsWith('xml') ? mime : undefined
|
||||
}
|
||||
|
||||
function imageSignatureMime(payload: string): string | undefined {
|
||||
if (payload.startsWith('iVBORw0KGgo')) return 'image/png'
|
||||
if (payload.startsWith('/9j/')) return 'image/jpeg'
|
||||
if (payload.startsWith('R0lGODdh') || payload.startsWith('R0lGODlh')) return 'image/gif'
|
||||
return undefined
|
||||
}
|
||||
|
||||
function dataMime(value?: string): string | undefined {
|
||||
return value?.startsWith('data:') ? value.slice(5, value.indexOf(';')) : undefined
|
||||
}
|
||||
|
||||
function referenceMime(value?: string): string | undefined {
|
||||
if (!value) return undefined
|
||||
return dataMime(value) || FORMAT_MIMES[new URL(value).pathname.toLowerCase().match(/\.([a-z0-9]+)$/)?.[1] || '']
|
||||
}
|
||||
|
||||
export function safeMediaUrl(value: unknown): string | undefined {
|
||||
if (typeof value !== 'string' || !value.trim()) return undefined
|
||||
const candidate = value.trim()
|
||||
if (/[\u0000-\u001f\u007f\\]/.test(candidate)) return undefined
|
||||
// Providers sometimes return base64 media instead of a hosted URL. Only
|
||||
// accept data URLs for media and require a base64 payload; never render
|
||||
// arbitrary data/javascript URLs from an upstream response.
|
||||
if (DATA_MEDIA_RE.test(candidate)) return candidate.replace(/\s+/g, '')
|
||||
if (DATA_MEDIA_RE.test(candidate) && mediaMime(candidate.slice(5, candidate.indexOf(';')))) {
|
||||
const marker = candidate.indexOf(';')
|
||||
return base64DataUrl(candidate.slice(marker + 8), candidate.slice(5, marker), '')
|
||||
}
|
||||
try {
|
||||
const url = new URL(candidate)
|
||||
return url.protocol === 'https:' || url.protocol === 'http:' ? url.href : undefined
|
||||
return (url.protocol === 'https:' || url.protocol === 'http:') && !url.username && !url.password ? url.href : undefined
|
||||
} catch { return undefined }
|
||||
}
|
||||
|
||||
@@ -27,39 +54,66 @@ function base64DataUrl(value: unknown, mimeType: unknown, format: string): strin
|
||||
if (typeof value !== 'string' || !value.trim()) return undefined
|
||||
const payload = value.trim().replace(/\s+/g, '')
|
||||
if (!/^[a-z0-9+/]+={0,2}$/i.test(payload) || payload.length % 4 === 1) return undefined
|
||||
const mime = typeof mimeType === 'string' && /^(image|video|audio|model)\/[a-z0-9.+-]+$/i.test(mimeType)
|
||||
? mimeType.toLowerCase()
|
||||
: ({ png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', webp: 'image/webp', gif: 'image/gif', mp4: 'video/mp4', webm: 'video/webm', mp3: 'audio/mpeg', wav: 'audio/wav', ogg: 'audio/ogg', opus: 'audio/ogg', aac: 'audio/aac', flac: 'audio/flac', m4a: 'audio/mp4', glb: 'model/gltf-binary', gltf: 'model/gltf+json' } as Record<string, string>)[format]
|
||||
const mime = mediaMime(mimeType) || FORMAT_MIMES[format] || imageSignatureMime(payload)
|
||||
return mime ? `data:${mime};base64,${payload}` : undefined
|
||||
}
|
||||
|
||||
function mediaValue(value: unknown, mimeType: unknown, format: string): string | undefined {
|
||||
function mediaValue(value: unknown, mimeType: unknown, format: string, depth = 0): string | undefined {
|
||||
if (depth > 4) return undefined
|
||||
if (typeof value === 'string') return safeMediaUrl(value) || base64DataUrl(value, mimeType, format)
|
||||
if (!value || typeof value !== 'object') return undefined
|
||||
const object = value as { url?: unknown; b64_json?: unknown; base64?: unknown; mime_type?: unknown; mimeType?: unknown; format?: unknown }
|
||||
const objectMime = object.mimeType || object.mime_type || mimeType
|
||||
const objectFormat = typeof object.format === 'string' ? object.format.toLowerCase() : format
|
||||
return mediaValue(object.url, objectMime, objectFormat) || base64DataUrl(object.b64_json || object.base64, objectMime, objectFormat)
|
||||
return mediaValue(object.url, objectMime, objectFormat, depth + 1) || base64DataUrl(object.b64_json || object.base64, objectMime, objectFormat)
|
||||
}
|
||||
|
||||
function mediaKind(type: string): string {
|
||||
return ({ 图像: 'image', 视频: 'video', 音频: 'audio', '3D': 'model', '3d': 'model' } as Record<string, string>)[type] || (['image', 'video', 'audio', 'model'].includes(type) ? type : '')
|
||||
}
|
||||
|
||||
function typedMediaValue(value: unknown, kind: string, mime: string, format: string): string | undefined {
|
||||
const url = mediaValue(value, mime, format)
|
||||
const actualMime = referenceMime(url)
|
||||
return actualMime && !actualMime.startsWith(`${kind}/`) ? undefined : url
|
||||
}
|
||||
|
||||
function firstTypedMedia(kind: string, mime: string, format: string, ...values: unknown[]): string | undefined {
|
||||
for (const value of values) {
|
||||
const url = typedMediaValue(value, kind, mime, format)
|
||||
if (url) return url
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** Normalize generation assets without treating every provider's generic URL as a 3D model. */
|
||||
export function mediaResult(result: Record<string, unknown> = {}, type = ''): MediaResult {
|
||||
const format = typeof result.format === 'string' ? result.format.toLowerCase() : ''
|
||||
const mime = typeof result.mimeType === 'string' ? result.mimeType.toLowerCase() : typeof result.mime_type === 'string' ? result.mime_type.toLowerCase() : ''
|
||||
const url = mediaValue(result.url, mime, format) || base64DataUrl(result.b64_json || result.base64 || result.data, mime, format)
|
||||
const suffix = url && !url.startsWith('data:') ? new URL(url).pathname.toLowerCase().match(/\.([a-z0-9]+)$/)?.[1] || '' : ''
|
||||
const isImage = type === '图像' || type === 'image' || mime.startsWith('image/') || /^(png|jpe?g|webp|gif|avif)$/.test(format || suffix || '')
|
||||
const isVideo = type === '视频' || type === 'video' || mime.startsWith('video/') || /^(mp4|webm|mov)$/.test(format || suffix || '')
|
||||
const isAudio = type === '音频' || type === 'audio' || mime.startsWith('audio/') || /^(mp3|wav|ogg|opus|m4a|flac|aac)$/.test(format || suffix || '')
|
||||
const isModel = type === '3D' || type === '3d' || /^(glb|gltf|obj|fbx|stl)$/.test(format || suffix || '')
|
||||
const mime = mediaMime(result.mimeType) || mediaMime(result.mime_type) || ''
|
||||
const rawUrl = mediaValue(result.url, mime, format) || base64DataUrl(result.b64_json || result.base64 || result.data, mime, format)
|
||||
const suffix = rawUrl && !rawUrl.startsWith('data:') ? new URL(rawUrl).pathname.toLowerCase().match(/\.([a-z0-9]+)$/)?.[1] || '' : ''
|
||||
const actualMime = dataMime(rawUrl) || mime || FORMAT_MIMES[format || suffix] || ''
|
||||
const inferredKind = actualMime.split('/')[0] || (/^(obj|fbx|stl)$/.test(format || suffix) ? 'model' : '')
|
||||
const kind = mediaKind(type) || inferredKind
|
||||
const url = mediaKind(type) && inferredKind && mediaKind(type) !== inferredKind ? undefined : rawUrl
|
||||
return {
|
||||
imageUrl: mediaValue(result.imageUrl, mime, format) || mediaValue(result.image_url, mime, format) || (isImage ? url : undefined),
|
||||
videoUrl: mediaValue(result.videoUrl, mime, format) || mediaValue(result.video_url, mime, format) || (isVideo ? url : undefined),
|
||||
audioUrl: mediaValue(result.audioUrl, mime, format) || mediaValue(result.audio_url, mime, format) || (isAudio ? url : undefined),
|
||||
modelUrl: mediaValue(result.modelUrl, mime, format) || (isModel ? url : undefined),
|
||||
previewUrl: mediaValue(result.previewUrl, mime, format) || mediaValue(result.preview_url, mime, format) || mediaValue(result.last_frame_url, 'image/png', ''),
|
||||
imageUrl: firstTypedMedia('image', mime, format, result.imageUrl, result.image_url) || (kind === 'image' ? url : undefined),
|
||||
videoUrl: firstTypedMedia('video', mime, format, result.videoUrl, result.video_url) || (kind === 'video' ? url : undefined),
|
||||
audioUrl: firstTypedMedia('audio', mime, format, result.audioUrl, result.audio_url) || (kind === 'audio' ? url : undefined),
|
||||
modelUrl: firstTypedMedia('model', mime, format, result.modelUrl, result.model_url) || (kind === 'model' ? url : undefined),
|
||||
previewUrl: firstTypedMedia('image', '', '', result.previewUrl, result.preview_url, result.last_frame_url),
|
||||
resultUrl: url,
|
||||
resultFormat: format || suffix,
|
||||
resultFormat: format || suffix || ({ 'image/png': 'png', 'image/jpeg': 'jpeg', 'audio/mpeg': 'mp3', 'audio/ogg': 'ogg' } as Record<string, string>)[actualMime],
|
||||
}
|
||||
}
|
||||
|
||||
export function hasPrimaryMedia(result: MediaResult, type: string): boolean {
|
||||
switch (mediaKind(type)) {
|
||||
case 'video': return !!result.videoUrl
|
||||
case 'audio': return !!result.audioUrl
|
||||
case 'image': return !!result.imageUrl
|
||||
case 'model': return !!result.modelUrl
|
||||
default: return !!result.resultUrl
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ import UiModal from '../components/UiModal.vue'
|
||||
import ModelGroupPermissions from '../components/ModelGroupPermissions.vue'
|
||||
import UsageHistory from '../components/UsageHistory.vue'
|
||||
import MediaResult from '../components/MediaResult.vue'
|
||||
import { generationLabel, mediaResult, mediaTypeLabel, safeMediaUrl, taskMediaType } from '../services/media'
|
||||
import { generationLabel, hasPrimaryMedia, mediaResult, mediaTypeLabel, safeMediaUrl, taskMediaType } from '../services/media'
|
||||
|
||||
defineProps<{ view: string; displayName?: string }>()
|
||||
const emit = defineEmits<{ navigate: [view: string]; notify: [message: string]; balance: [value: number | undefined] }>()
|
||||
@@ -499,7 +499,7 @@ function mapAsyncTask(task: AsyncTaskResult, model: ModelItem, taskPrompt?: stri
|
||||
const modelUrl = assets.modelUrl
|
||||
const videoUrl = assets.videoUrl
|
||||
const previewUrl = assets.previewUrl
|
||||
const deliveryPending = task.result?.deliveryStatus === 'pending'
|
||||
const deliveryPending = task.result?.deliveryStatus === 'pending' || task.status === 'succeeded' && !hasPrimaryMedia(assets, type)
|
||||
const imageUrl = assets.imageUrl
|
||||
const audioUrl = assets.audioUrl
|
||||
const resultUrl = assets.resultUrl
|
||||
@@ -565,7 +565,8 @@ async function pollTask(taskId: string, apiKey: string) {
|
||||
// Refresh financial state without encouraging another generation request.
|
||||
await refreshOverview()
|
||||
historyRevision.value++
|
||||
emit('notify', latest.status === 'reconciling' ? `${previous.name}结果尚未确认,额度仍冻结,请联系管理员核账,勿重复提交` : latest.result?.deliveryStatus === 'pending' ? '上游已完成,结果文件尚未接入,请联系管理员,勿重复生成' : latest.status === 'succeeded' ? `${previous.name}完成,可以查看和保存结果` : `${previous.name}未能完成,请查看错误说明`)
|
||||
const deliveryPending = latest.status === 'succeeded' && (latest.result?.deliveryStatus === 'pending' || !hasPrimaryMedia(mapped, taskMediaType(latest.taskType, model.type)))
|
||||
emit('notify', latest.status === 'reconciling' ? `${previous.name}结果尚未确认,额度仍冻结,请联系管理员核账,勿重复提交` : deliveryPending ? '上游已完成,结果文件尚未接入,请联系管理员,勿重复生成' : latest.status === 'succeeded' ? `${previous.name}完成,可以查看和保存结果` : `${previous.name}未能完成,请查看错误说明`)
|
||||
}
|
||||
} catch (error) {
|
||||
taskPollers.delete(taskId)
|
||||
|
||||
Reference in new issue
Block a user