重构项目文件夹目录
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import { fileApi } from '@/api/file'
|
||||
import type { IdType } from '@/utils/id'
|
||||
|
||||
const SIGNED_IMAGE_CACHE_TTL_MS = 10 * 60 * 1000
|
||||
|
||||
type SignedImageCacheEntry = {
|
||||
url: string
|
||||
expiresAt: number
|
||||
}
|
||||
|
||||
const signedImageUrlCache = new Map<string, SignedImageCacheEntry>()
|
||||
const pendingSignedImageUrlRequests = new Map<string, Promise<string>>()
|
||||
|
||||
const normalizeFileId = (fileId: IdType | null | undefined) => String(fileId ?? '').trim()
|
||||
|
||||
const getUsableCacheEntry = (key: string) => {
|
||||
const cached = signedImageUrlCache.get(key)
|
||||
if (!cached) return undefined
|
||||
if (cached.expiresAt <= Date.now()) {
|
||||
signedImageUrlCache.delete(key)
|
||||
return undefined
|
||||
}
|
||||
return cached.url
|
||||
}
|
||||
|
||||
export const parseImageFileIds = (value?: string | null): string[] =>
|
||||
String(value || '')
|
||||
.split(',')
|
||||
.map(id => id.trim())
|
||||
.filter(Boolean)
|
||||
|
||||
export const getCachedSignedImageUrlSync = (fileId: IdType | null | undefined): string => {
|
||||
const key = normalizeFileId(fileId)
|
||||
return key ? getUsableCacheEntry(key) || '' : ''
|
||||
}
|
||||
|
||||
export const getCachedSignedImageUrl = async (
|
||||
fileId: IdType | null | undefined,
|
||||
fallback = ''
|
||||
): Promise<string> => {
|
||||
const key = normalizeFileId(fileId)
|
||||
if (!key) return fallback
|
||||
|
||||
const cachedUrl = getUsableCacheEntry(key)
|
||||
if (cachedUrl) return cachedUrl
|
||||
|
||||
const pendingRequest = pendingSignedImageUrlRequests.get(key)
|
||||
if (pendingRequest) {
|
||||
return pendingRequest.then(url => url || fallback).catch(() => fallback)
|
||||
}
|
||||
|
||||
const request = fileApi.getSignedUrl(key)
|
||||
.then(response => {
|
||||
const url = String(response || '').trim()
|
||||
if (url) {
|
||||
signedImageUrlCache.set(key, {
|
||||
url,
|
||||
expiresAt: Date.now() + SIGNED_IMAGE_CACHE_TTL_MS
|
||||
})
|
||||
}
|
||||
return url
|
||||
})
|
||||
.finally(() => {
|
||||
pendingSignedImageUrlRequests.delete(key)
|
||||
})
|
||||
|
||||
pendingSignedImageUrlRequests.set(key, request)
|
||||
return request.then(url => url || fallback).catch(() => fallback)
|
||||
}
|
||||
|
||||
export const getCachedSignedImageUrls = async (
|
||||
fileIds: Array<IdType | null | undefined>,
|
||||
fallback = ''
|
||||
): Promise<string[]> => {
|
||||
const urls = await Promise.all(fileIds.map(fileId => getCachedSignedImageUrl(fileId)))
|
||||
const validUrls = urls.filter(Boolean)
|
||||
return validUrls.length ? validUrls : (fallback ? [fallback] : [])
|
||||
}
|
||||
|
||||
export const getCachedSignedImageUrlsSync = (fileIds: Array<IdType | null | undefined>): string[] =>
|
||||
fileIds.map(fileId => getCachedSignedImageUrlSync(fileId)).filter(Boolean)
|
||||
|
||||
export const extractLegacyFileId = (value?: string | null): string => {
|
||||
const source = String(value || '').replace(/<[^>]*>/g, '')
|
||||
return source.match(/\/api\/file\/get\/([^/?#"'<>]+)/)?.[1] || ''
|
||||
}
|
||||
Reference in New Issue
Block a user