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,17 +128,11 @@ 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) {
try { return checkFileExtension(url, [
const mediaExtensions = [
'.mp4', '.mp4',
'.webm', '.webm',
'.ogg', '.ogg',
@ -151,8 +145,29 @@ export function isMedia(url: string) {
'.opus', '.opus',
'.wma', '.wma',
'.3gp' '.3gp'
] ])
return mediaExtensions.some((ext) => new URL(url).pathname.toLowerCase().endsWith(ext)) }
function checkFileExtension(url: string, extensions: string[]): boolean {
try {
const lowerCaseUrl = url.toLowerCase()
const endsWithImageExtion = extensions.some((ext) => lowerCaseUrl.endsWith(ext))
if (endsWithImageExtion) {
return true
}
const u = new URL(lowerCaseUrl)
const hasImageExtension = extensions.some((ext) => u.pathname.endsWith(ext))
if (hasImageExtension) {
return true
}
const fileNameParam = u.searchParams.get('filename')
if (fileNameParam) {
return extensions.some((ext) => fileNameParam.endsWith(ext))
}
return false
} catch { } catch {
return false return false
} }