From 4c7052c17de8b9e477bcd7f91d4c9fcb4117d219 Mon Sep 17 00:00:00 2001 From: codytseng Date: Tue, 30 Dec 2025 12:30:10 +0800 Subject: [PATCH] feat: improve url resource type detection logic --- src/lib/url.ts | 57 +++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/src/lib/url.ts b/src/lib/url.ts index 5b767f9..d8ea4e8 100644 --- a/src/lib/url.ts +++ b/src/lib/url.ts @@ -128,31 +128,46 @@ export function isLocalNetworkUrl(urlString: string): boolean { } export function isImage(url: string) { - try { - const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.heic', '.svg'] - return imageExtensions.some((ext) => new URL(url).pathname.toLowerCase().endsWith(ext)) - } catch { - return false - } + return checkFileExtension(url, ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.heic', '.svg']) } 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 { - const mediaExtensions = [ - '.mp4', - '.webm', - '.ogg', - '.mov', - '.mp3', - '.wav', - '.flac', - '.aac', - '.m4a', - '.opus', - '.wma', - '.3gp' - ] - return mediaExtensions.some((ext) => new URL(url).pathname.toLowerCase().endsWith(ext)) + 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 { return false }