Files
cz-app/src/utils/resolve-oss-media.ts
2026-09-21 15:41:52 +08:00

37 lines
817 B
TypeScript

import { getOssListByIds } from '@/api/oss'
export function isHttpUrl(value?: string) {
return Boolean(value && /^https?:\/\//.test(value))
}
export async function resolveOssUrlMap(rawIds: Array<string | undefined>) {
const map = new Map<string, string>()
const ossIds = rawIds
.filter((id): id is string => Boolean(id && !isHttpUrl(id)))
.map(String)
if (!ossIds.length)
return map
try {
const files = await getOssListByIds(ossIds)
for (const file of files ?? []) {
if (file.ossId && file.url)
map.set(String(file.ossId), file.url)
}
}
catch {
// ignore
}
return map
}
export function pickMediaUrl(raw?: string, urlMap?: Map<string, string>) {
if (!raw)
return undefined
if (isHttpUrl(raw))
return raw
return urlMap?.get(String(raw))
}