37 lines
817 B
TypeScript
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))
|
|
}
|