提交开发分支未提交代码并修正配置
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { fileApi } from '@/api/file'
|
||||
import { adminFileApi } from '@/api/admin'
|
||||
import type { IdType } from '@/utils/id'
|
||||
|
||||
const SIGNED_IMAGE_CACHE_TTL_MS = 10 * 60 * 1000
|
||||
|
||||
export type SignedImageSource = 'public' | 'admin'
|
||||
|
||||
type SignedImageCacheEntry = {
|
||||
url: string
|
||||
expiresAt: number
|
||||
@@ -12,6 +15,22 @@ const signedImageUrlCache = new Map<string, SignedImageCacheEntry>()
|
||||
const pendingSignedImageUrlRequests = new Map<string, Promise<string>>()
|
||||
|
||||
const normalizeFileId = (fileId: IdType | null | undefined) => String(fileId ?? '').trim()
|
||||
const buildCacheKey = (source: SignedImageSource, fileId: string) => `${source}:${fileId}`
|
||||
const normalizeImageUrl = (url: string): string => {
|
||||
const trimmedUrl = url.trim()
|
||||
if (!trimmedUrl) return ''
|
||||
if (/^(https?:)?\/\//i.test(trimmedUrl) || /^(data|blob):/i.test(trimmedUrl) || trimmedUrl.startsWith('/')) {
|
||||
return trimmedUrl
|
||||
}
|
||||
return `http://${trimmedUrl}`
|
||||
}
|
||||
|
||||
const requestSignedImageUrl = (source: SignedImageSource, fileId: string): Promise<string> => {
|
||||
if (source === 'admin') {
|
||||
return adminFileApi.getSignedUrl(fileId)
|
||||
}
|
||||
return fileApi.getSignedUrl(fileId).then(response => String(response.data || ''))
|
||||
}
|
||||
|
||||
const getUsableCacheEntry = (key: string) => {
|
||||
const cached = signedImageUrlCache.get(key)
|
||||
@@ -29,17 +48,23 @@ export const parseImageFileIds = (value?: string | null): string[] =>
|
||||
.map(id => id.trim())
|
||||
.filter(Boolean)
|
||||
|
||||
export const getCachedSignedImageUrlSync = (fileId: IdType | null | undefined): string => {
|
||||
const key = normalizeFileId(fileId)
|
||||
return key ? getUsableCacheEntry(key) || '' : ''
|
||||
export const getCachedSignedImageUrlSync = (
|
||||
fileId: IdType | null | undefined,
|
||||
source: SignedImageSource = 'public'
|
||||
): string => {
|
||||
const fileIdKey = normalizeFileId(fileId)
|
||||
if (!fileIdKey) return ''
|
||||
return getUsableCacheEntry(buildCacheKey(source, fileIdKey)) || ''
|
||||
}
|
||||
|
||||
export const getCachedSignedImageUrl = async (
|
||||
fileId: IdType | null | undefined,
|
||||
fallback = ''
|
||||
fallback = '',
|
||||
source: SignedImageSource = 'public'
|
||||
): Promise<string> => {
|
||||
const key = normalizeFileId(fileId)
|
||||
if (!key) return fallback
|
||||
const fileIdKey = normalizeFileId(fileId)
|
||||
if (!fileIdKey) return fallback
|
||||
const key = buildCacheKey(source, fileIdKey)
|
||||
|
||||
const cachedUrl = getUsableCacheEntry(key)
|
||||
if (cachedUrl) return cachedUrl
|
||||
@@ -49,9 +74,9 @@ export const getCachedSignedImageUrl = async (
|
||||
return pendingRequest.then(url => url || fallback).catch(() => fallback)
|
||||
}
|
||||
|
||||
const request = fileApi.getSignedUrl(key)
|
||||
const request = requestSignedImageUrl(source, fileIdKey)
|
||||
.then(response => {
|
||||
const url = String(response || '').trim()
|
||||
const url = normalizeImageUrl(String(response || ''))
|
||||
if (url) {
|
||||
signedImageUrlCache.set(key, {
|
||||
url,
|
||||
@@ -68,17 +93,41 @@ export const getCachedSignedImageUrl = async (
|
||||
return request.then(url => url || fallback).catch(() => fallback)
|
||||
}
|
||||
|
||||
export const clearCachedSignedImageUrl = (
|
||||
fileId: IdType | null | undefined,
|
||||
source: SignedImageSource = 'public'
|
||||
): void => {
|
||||
const fileIdKey = normalizeFileId(fileId)
|
||||
if (!fileIdKey) return
|
||||
const cacheKey = buildCacheKey(source, fileIdKey)
|
||||
signedImageUrlCache.delete(cacheKey)
|
||||
pendingSignedImageUrlRequests.delete(cacheKey)
|
||||
}
|
||||
|
||||
export const refreshCachedSignedImageUrl = async (
|
||||
fileId: IdType | null | undefined,
|
||||
fallback = '',
|
||||
source: SignedImageSource = 'public'
|
||||
): Promise<string> => {
|
||||
clearCachedSignedImageUrl(fileId, source)
|
||||
return getCachedSignedImageUrl(fileId, fallback, source)
|
||||
}
|
||||
|
||||
export const getCachedSignedImageUrls = async (
|
||||
fileIds: Array<IdType | null | undefined>,
|
||||
fallback = ''
|
||||
fallback = '',
|
||||
source: SignedImageSource = 'public'
|
||||
): Promise<string[]> => {
|
||||
const urls = await Promise.all(fileIds.map(fileId => getCachedSignedImageUrl(fileId)))
|
||||
const urls = await Promise.all(fileIds.map(fileId => getCachedSignedImageUrl(fileId, '', source)))
|
||||
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 getCachedSignedImageUrlsSync = (
|
||||
fileIds: Array<IdType | null | undefined>,
|
||||
source: SignedImageSource = 'public'
|
||||
): string[] =>
|
||||
fileIds.map(fileId => getCachedSignedImageUrlSync(fileId, source)).filter(Boolean)
|
||||
|
||||
export const extractLegacyFileId = (value?: string | null): string => {
|
||||
const source = String(value || '').replace(/<[^>]*>/g, '')
|
||||
|
||||
Reference in New Issue
Block a user