feat: improve url resource type detection logic

This commit is contained in:
codytseng 2025-12-30 12:30:10 +08:00
parent eb6c017319
commit 4c7052c17d

View file

@ -128,31 +128,46 @@ export function isLocalNetworkUrl(urlString: string): boolean {
} }
export function isImage(url: string) { export function isImage(url: string) {
try { return checkFileExtension(url, ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.heic', '.svg'])
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.heic', '.svg']
return imageExtensions.some((ext) => new URL(url).pathname.toLowerCase().endsWith(ext))
} catch {
return false
}
} }
export function isMedia(url: string) { export function isMedia(url: string) {
return checkFileExtension(url, [
'.mp4',
'.webm',
'.ogg',
'.mov',
'.mp3',
'.wav',
'.flac',
'.aac',
'.m4a',
'.opus',
'.wma',
'.3gp'
])
}
function checkFileExtension(url: string, extensions: string[]): boolean {
try { try {
const mediaExtensions = [ const lowerCaseUrl = url.toLowerCase()
'.mp4', const endsWithImageExtion = extensions.some((ext) => lowerCaseUrl.endsWith(ext))
'.webm', if (endsWithImageExtion) {
'.ogg', return true
'.mov', }
'.mp3',
'.wav', const u = new URL(lowerCaseUrl)
'.flac', const hasImageExtension = extensions.some((ext) => u.pathname.endsWith(ext))
'.aac', if (hasImageExtension) {
'.m4a', return true
'.opus', }
'.wma',
'.3gp' const fileNameParam = u.searchParams.get('filename')
] if (fileNameParam) {
return mediaExtensions.some((ext) => new URL(url).pathname.toLowerCase().endsWith(ext)) return extensions.some((ext) => fileNameParam.endsWith(ext))
}
return false
} catch { } catch {
return false return false
} }