From 503707f0152bf60fe9ce839dfcbf5e4a5aa2fb09 Mon Sep 17 00:00:00 2001 From: guanxu <1510424541@qq.com> Date: Mon, 16 Mar 2026 20:53:15 +0800 Subject: [PATCH 1/9] fix(core): Fix MediaUtils parse path extension --- .../agentscope/core/formatter/MediaUtils.java | 15 ++++++++++ .../core/formatter/MediaUtilsTest.java | 29 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java index 62ed3530f..ef4b1f7c9 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java +++ b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java @@ -29,6 +29,7 @@ import java.nio.file.Path; import java.util.Base64; import java.util.List; +import java.util.Set; import javax.imageio.ImageIO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -354,8 +355,22 @@ public static String getExtension(String path) { } int dotIndex = path.lastIndexOf('.'); int slashIndex = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')); + + // Check for query string or fragment + Set masks = Set.of("?", "&", "#"); + int endIdx = path.length(); + for (String mask : masks) { + int idx = path.indexOf(mask); + if (idx != -1) { + endIdx = Math.min(endIdx, idx); + } + } + // Ensure the dot is after the last slash (not part of directory name) if (dotIndex > slashIndex && dotIndex < path.length() - 1) { + if (dotIndex < endIdx - 1) { + return path.substring(dotIndex + 1, endIdx); + } return path.substring(dotIndex + 1); } return ""; diff --git a/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java b/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java index 3f10397bb..c1d18fd96 100644 --- a/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java +++ b/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java @@ -329,4 +329,33 @@ void testUrlToRgbaImageInputStreamWithFile() throws IOException { assertNotNull(is); is.close(); } + + @Test + @DisplayName("Should get extension with query string") + void testGetExtensionWithQueryString() { + String extension = MediaUtils.getExtension("https://example.com/img.png?id=1&type=png"); + assertEquals("png", extension); + } + + @Test + @DisplayName("Should get extension with no mask query string") + void testGetExtensionWithNoMaskQueryString() { + String extension = MediaUtils.getExtension("https://example.com/img.png&id=1&type=png"); + assertEquals("png", extension); + } + + @Test + @DisplayName("Should get extension with fragment") + void testGetExtensionWithFragment() { + String extension = MediaUtils.getExtension("https://example.com/img.png#section1"); + assertEquals("png", extension); + } + + @Test + @DisplayName("Should get extension with query string and fragment") + void testGetExtensionWithQueryStringAndFragment() { + String extension = + MediaUtils.getExtension("https://example.com/img.png?id=1&type=png#section1"); + assertEquals("png", extension); + } } From 6e7c5b46864b77164df8fb2eba84334d39b10f80 Mon Sep 17 00:00:00 2001 From: guanxu <1510424541@qq.com> Date: Tue, 24 Mar 2026 21:50:37 +0800 Subject: [PATCH 2/9] fix: Strip path to resolve dot params --- .../java/io/agentscope/core/formatter/MediaUtils.java | 10 +++++----- .../io/agentscope/core/formatter/MediaUtilsTest.java | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java index ef4b1f7c9..0c981ea9b 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java +++ b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java @@ -353,8 +353,6 @@ public static String getExtension(String path) { if (path == null) { return ""; } - int dotIndex = path.lastIndexOf('.'); - int slashIndex = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')); // Check for query string or fragment Set masks = Set.of("?", "&", "#"); @@ -366,11 +364,13 @@ public static String getExtension(String path) { } } + // Strip query string or fragment + path = path.substring(0, endIdx); + int dotIndex = path.lastIndexOf('.'); + int slashIndex = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')); + // Ensure the dot is after the last slash (not part of directory name) if (dotIndex > slashIndex && dotIndex < path.length() - 1) { - if (dotIndex < endIdx - 1) { - return path.substring(dotIndex + 1, endIdx); - } return path.substring(dotIndex + 1); } return ""; diff --git a/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java b/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java index c1d18fd96..d3c4fe444 100644 --- a/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java +++ b/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java @@ -358,4 +358,11 @@ void testGetExtensionWithQueryStringAndFragment() { MediaUtils.getExtension("https://example.com/img.png?id=1&type=png#section1"); assertEquals("png", extension); } + + @Test + @DisplayName("Should get extension with query string and dot params") + void testGetExtensionWithQueryStringAndDotValues() { + String extension = MediaUtils.getExtension("https://example.com/img.png?v=2.0.0"); + assertEquals("png", extension); + } } From 9cf47b825b5f15fa26e5a6d4e9db22f8535667d6 Mon Sep 17 00:00:00 2001 From: guanxu <1510424541@qq.com> Date: Thu, 26 Mar 2026 11:23:36 +0800 Subject: [PATCH 3/9] Define url masks to static property --- .../main/java/io/agentscope/core/formatter/MediaUtils.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java index 0c981ea9b..58b70b067 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java +++ b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java @@ -53,6 +53,9 @@ public class MediaUtils { private static final List SUPPORTED_VIDEO_EXTENSIONS = List.of("mp4", "mpeg", "mpg", "mov", "avi", "webm", "wmv", "flv", "3gp", "3gpp"); + // URL masks + private static final Set URL_MASKS = Set.of("?", "&", "#"); + private MediaUtils() { // Utility class, prevent instantiation } @@ -355,9 +358,8 @@ public static String getExtension(String path) { } // Check for query string or fragment - Set masks = Set.of("?", "&", "#"); int endIdx = path.length(); - for (String mask : masks) { + for (String mask : URL_MASKS) { int idx = path.indexOf(mask); if (idx != -1) { endIdx = Math.min(endIdx, idx); From 7f0c7abbe92d7132bc26edf9c1d018eec704b818 Mon Sep 17 00:00:00 2001 From: guanxu <1510424541@qq.com> Date: Thu, 26 Mar 2026 11:28:53 +0800 Subject: [PATCH 4/9] Use List --- .../src/main/java/io/agentscope/core/formatter/MediaUtils.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java index 58b70b067..854411741 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java +++ b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java @@ -29,7 +29,6 @@ import java.nio.file.Path; import java.util.Base64; import java.util.List; -import java.util.Set; import javax.imageio.ImageIO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,7 +53,7 @@ public class MediaUtils { List.of("mp4", "mpeg", "mpg", "mov", "avi", "webm", "wmv", "flv", "3gp", "3gpp"); // URL masks - private static final Set URL_MASKS = Set.of("?", "&", "#"); + private static final List URL_MASKS = List.of("?", "&", "#"); private MediaUtils() { // Utility class, prevent instantiation From 70bb7c0fd90df6c62b33fce4ca9508d7cb3d0ca7 Mon Sep 17 00:00:00 2001 From: guanxu <1510424541@qq.com> Date: Thu, 26 Mar 2026 13:59:11 +0800 Subject: [PATCH 5/9] feat: Use URI to parse url path, use Path to parse file path --- .../agentscope/core/formatter/MediaUtils.java | 44 ++++---- .../core/formatter/MediaUtilsTest.java | 101 +++++++++++++++--- 2 files changed, 112 insertions(+), 33 deletions(-) diff --git a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java index 854411741..61b8b972c 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java +++ b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java @@ -27,6 +27,7 @@ import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Base64; import java.util.List; import javax.imageio.ImageIO; @@ -52,16 +53,13 @@ public class MediaUtils { private static final List SUPPORTED_VIDEO_EXTENSIONS = List.of("mp4", "mpeg", "mpg", "mov", "avi", "webm", "wmv", "flv", "3gp", "3gpp"); - // URL masks - private static final List URL_MASKS = List.of("?", "&", "#"); - private MediaUtils() { // Utility class, prevent instantiation } /** * Check if a URL is a local file path (not a URL with protocol scheme). - * Returns true for paths without http://, https://, ftp://, or file:// prefixes. + * Returns true for paths without http://, https://, ftp://, file:// or oss:// prefixes. * Used to distinguish local files from remote URLs for different processing paths. * * @param url The URL or file path to check @@ -352,27 +350,37 @@ public static String inferAudioFormatFromMediaType(String mediaType) { * Extract file extension from path or URL. */ public static String getExtension(String path) { - if (path == null) { + if (path == null || path.isBlank()) { return ""; } - // Check for query string or fragment - int endIdx = path.length(); - for (String mask : URL_MASKS) { - int idx = path.indexOf(mask); - if (idx != -1) { - endIdx = Math.min(endIdx, idx); + boolean localFile = isLocalFile(path); + + Path fileNamePath; + if (localFile) { + // treat as file + fileNamePath = Paths.get(path).normalize().getFileName(); + } else { + // treat as url + URI uri; + try { + uri = URI.create(path).normalize(); + } catch (IllegalArgumentException e) { + log.error("Invalid URL: {}", path); + return ""; } + fileNamePath = Paths.get(uri.getPath()).getFileName(); } - // Strip query string or fragment - path = path.substring(0, endIdx); - int dotIndex = path.lastIndexOf('.'); - int slashIndex = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')); + if (fileNamePath == null) { + return ""; + } - // Ensure the dot is after the last slash (not part of directory name) - if (dotIndex > slashIndex && dotIndex < path.length() - 1) { - return path.substring(dotIndex + 1); + String fileName = fileNamePath.toString(); + int dotIndex = fileName.lastIndexOf('.'); + // Ensure the dot exists and is not the last character + if (dotIndex > -1 && dotIndex < path.length() - 1) { + return fileName.substring(dotIndex + 1); } return ""; } diff --git a/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java b/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java index d3c4fe444..d7283b533 100644 --- a/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java +++ b/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java @@ -331,38 +331,109 @@ void testUrlToRgbaImageInputStreamWithFile() throws IOException { } @Test - @DisplayName("Should get extension with query string") - void testGetExtensionWithQueryString() { - String extension = MediaUtils.getExtension("https://example.com/img.png?id=1&type=png"); - assertEquals("png", extension); + @DisplayName("Should return empty string when path is null, empty or blank") + void testGetExtensionWithNullEmptyOrBlankPath() { + assertEquals("", MediaUtils.getExtension(null)); + assertEquals("", MediaUtils.getExtension("")); + assertEquals("", MediaUtils.getExtension(" ")); + } + + @Test + @DisplayName("Should return empty string when path is invalid") + void testGetExtensionWithInvalidPath() { + assertEquals("", MediaUtils.getExtension("/")); + assertEquals("", MediaUtils.getExtension("\\")); + assertEquals("", MediaUtils.getExtension("/home/user")); + assertEquals("", MediaUtils.getExtension("C:\\Users\\Administrator")); + assertEquals("", MediaUtils.getExtension("https://abc")); + assertEquals("", MediaUtils.getExtension("https://abc[@]123")); + assertEquals("", MediaUtils.getExtension("https://example.com/abc")); + assertEquals("", MediaUtils.getExtension("https://example.com/abc/")); + } + + @Test + @DisplayName("Should get extension with all types") + void testGetExtensionWithAllTypes() { + assertEquals("png", MediaUtils.getExtension("https://example.com/img.png")); + assertEquals("wav", MediaUtils.getExtension("https://example.com/audio.wav")); + assertEquals("mp3", MediaUtils.getExtension("https://example.com/audio.mp3")); + assertEquals("mp4", MediaUtils.getExtension("https://example.com/video.mp4")); + assertEquals("xxx", MediaUtils.getExtension("https://example.com/video.xxx")); } @Test - @DisplayName("Should get extension with no mask query string") - void testGetExtensionWithNoMaskQueryString() { - String extension = MediaUtils.getExtension("https://example.com/img.png&id=1&type=png"); - assertEquals("png", extension); + @DisplayName("Should get last extension when have nested types") + void testGetExtensionWithNestedTypes() { + assertEquals("gz", MediaUtils.getExtension("https://example.com/img.png.tar.gz")); + assertEquals("zip", MediaUtils.getExtension("https://example.com/img.png.zip")); + } + + @Test + @DisplayName("Should get extension with query string") + void testGetExtensionWithQueryString() { + assertEquals("png", MediaUtils.getExtension("https://example.com/img.png?id=1&type=png")); } @Test @DisplayName("Should get extension with fragment") void testGetExtensionWithFragment() { - String extension = MediaUtils.getExtension("https://example.com/img.png#section1"); - assertEquals("png", extension); + assertEquals("png", MediaUtils.getExtension("https://example.com/img.png#section1.1")); } @Test @DisplayName("Should get extension with query string and fragment") void testGetExtensionWithQueryStringAndFragment() { - String extension = - MediaUtils.getExtension("https://example.com/img.png?id=1&type=png#section1"); - assertEquals("png", extension); + assertEquals( + "png", + MediaUtils.getExtension("https://example.com/img.png?id=1&type=png#section1.1")); } @Test @DisplayName("Should get extension with query string and dot params") void testGetExtensionWithQueryStringAndDotValues() { - String extension = MediaUtils.getExtension("https://example.com/img.png?v=2.0.0"); - assertEquals("png", extension); + assertEquals("png", MediaUtils.getExtension("https://example.com/img.png?v=2.0.0")); + } + + @Test + @DisplayName("Should get extension with mask name") + void testGetExtensionWithMaskName() { + assertEquals( + "png", MediaUtils.getExtension("https://example.com/img-123&type=png&v=2.0.0.png")); + assertEquals("png", MediaUtils.getExtension("/home/user/img-123&type=png&v=2.0.0.png")); + assertEquals( + "png", + MediaUtils.getExtension("C:\\Users\\Administrator\\img_123&type=png&v=2.0.0.png")); + } + + @Test + @DisplayName("Should get extension with http protocol") + void testGetExtensionWithHttpProtocol() { + assertEquals( + "png", + MediaUtils.getExtension("http://example.com/img.png?id=1&type=png#section1.1")); + } + + @Test + @DisplayName("Should get extension with oss protocol") + void testGetExtensionWithOssProtocol() { + assertEquals( + "png", + MediaUtils.getExtension("oss://example.com/img.png?id=1&type=png#section1.1")); + } + + @Test + @DisplayName("Should get extension with file protocol") + void testGetExtensionWithFileProtocol() { + assertEquals( + "png", + MediaUtils.getExtension("file://example.com/img.png?id=1&type=png#section1.1")); + } + + @Test + @DisplayName("Should get extension with ftp protocol") + void testGetExtensionWithFtpProtocol() { + assertEquals( + "png", + MediaUtils.getExtension("ftp://example.com/img.png?id=1&type=png#section1.1")); } } From eeb15952082032ae136431a3dd4739edcb5c8d37 Mon Sep 17 00:00:00 2001 From: guanxu <1510424541@qq.com> Date: Thu, 26 Mar 2026 14:04:07 +0800 Subject: [PATCH 6/9] add unit test --- .../test/java/io/agentscope/core/formatter/MediaUtilsTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java b/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java index d7283b533..ce1028c76 100644 --- a/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java +++ b/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java @@ -349,6 +349,7 @@ void testGetExtensionWithInvalidPath() { assertEquals("", MediaUtils.getExtension("https://abc[@]123")); assertEquals("", MediaUtils.getExtension("https://example.com/abc")); assertEquals("", MediaUtils.getExtension("https://example.com/abc/")); + assertEquals("", MediaUtils.getExtension("https://example.com/img.png.")); } @Test From f530244282d38693b1c882b925b3b4333c3e3e96 Mon Sep 17 00:00:00 2001 From: guanxu <1510424541@qq.com> Date: Fri, 27 Mar 2026 14:42:53 +0800 Subject: [PATCH 7/9] Make code more readable, make unit tests more concise. --- .../agentscope/core/formatter/MediaUtils.java | 2 +- .../core/formatter/MediaUtilsTest.java | 59 ++++--------------- 2 files changed, 13 insertions(+), 48 deletions(-) diff --git a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java index 61b8b972c..9af9b8928 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java +++ b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java @@ -379,7 +379,7 @@ public static String getExtension(String path) { String fileName = fileNamePath.toString(); int dotIndex = fileName.lastIndexOf('.'); // Ensure the dot exists and is not the last character - if (dotIndex > -1 && dotIndex < path.length() - 1) { + if (dotIndex != -1 && dotIndex < path.length() - 1) { return fileName.substring(dotIndex + 1); } return ""; diff --git a/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java b/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java index ce1028c76..8bfb93594 100644 --- a/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java +++ b/agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java @@ -331,16 +331,11 @@ void testUrlToRgbaImageInputStreamWithFile() throws IOException { } @Test - @DisplayName("Should return empty string when path is null, empty or blank") - void testGetExtensionWithNullEmptyOrBlankPath() { + @DisplayName("Should return empty string when path is invalid") + void testGetExtensionWithInvalidPath() { assertEquals("", MediaUtils.getExtension(null)); assertEquals("", MediaUtils.getExtension("")); assertEquals("", MediaUtils.getExtension(" ")); - } - - @Test - @DisplayName("Should return empty string when path is invalid") - void testGetExtensionWithInvalidPath() { assertEquals("", MediaUtils.getExtension("/")); assertEquals("", MediaUtils.getExtension("\\")); assertEquals("", MediaUtils.getExtension("/home/user")); @@ -359,7 +354,7 @@ void testGetExtensionWithAllTypes() { assertEquals("wav", MediaUtils.getExtension("https://example.com/audio.wav")); assertEquals("mp3", MediaUtils.getExtension("https://example.com/audio.mp3")); assertEquals("mp4", MediaUtils.getExtension("https://example.com/video.mp4")); - assertEquals("xxx", MediaUtils.getExtension("https://example.com/video.xxx")); + assertEquals("xxx", MediaUtils.getExtension("https://example.com/xxx.xxx")); } @Test @@ -370,36 +365,18 @@ void testGetExtensionWithNestedTypes() { } @Test - @DisplayName("Should get extension with query string") - void testGetExtensionWithQueryString() { + @DisplayName("Should get extension with masks") + void testGetExtensionWithMasks() { assertEquals("png", MediaUtils.getExtension("https://example.com/img.png?id=1&type=png")); - } - - @Test - @DisplayName("Should get extension with fragment") - void testGetExtensionWithFragment() { assertEquals("png", MediaUtils.getExtension("https://example.com/img.png#section1.1")); - } - - @Test - @DisplayName("Should get extension with query string and fragment") - void testGetExtensionWithQueryStringAndFragment() { assertEquals( "png", MediaUtils.getExtension("https://example.com/img.png?id=1&type=png#section1.1")); - } - - @Test - @DisplayName("Should get extension with query string and dot params") - void testGetExtensionWithQueryStringAndDotValues() { assertEquals("png", MediaUtils.getExtension("https://example.com/img.png?v=2.0.0")); - } - - @Test - @DisplayName("Should get extension with mask name") - void testGetExtensionWithMaskName() { assertEquals( "png", MediaUtils.getExtension("https://example.com/img-123&type=png&v=2.0.0.png")); + + // verify the file name include special masks assertEquals("png", MediaUtils.getExtension("/home/user/img-123&type=png&v=2.0.0.png")); assertEquals( "png", @@ -407,32 +384,20 @@ void testGetExtensionWithMaskName() { } @Test - @DisplayName("Should get extension with http protocol") - void testGetExtensionWithHttpProtocol() { + @DisplayName("Should get extension with all supported protocol") + void testGetExtensionWithSupportedProtocol() { assertEquals( "png", MediaUtils.getExtension("http://example.com/img.png?id=1&type=png#section1.1")); - } - - @Test - @DisplayName("Should get extension with oss protocol") - void testGetExtensionWithOssProtocol() { + assertEquals( + "png", + MediaUtils.getExtension("https://example.com/img.png?id=1&type=png#section1.1")); assertEquals( "png", MediaUtils.getExtension("oss://example.com/img.png?id=1&type=png#section1.1")); - } - - @Test - @DisplayName("Should get extension with file protocol") - void testGetExtensionWithFileProtocol() { assertEquals( "png", MediaUtils.getExtension("file://example.com/img.png?id=1&type=png#section1.1")); - } - - @Test - @DisplayName("Should get extension with ftp protocol") - void testGetExtensionWithFtpProtocol() { assertEquals( "png", MediaUtils.getExtension("ftp://example.com/img.png?id=1&type=png#section1.1")); From 6800fc071390f49b831496fd8784043fc3887efa Mon Sep 17 00:00:00 2001 From: guanxu <1510424541@qq.com> Date: Fri, 27 Mar 2026 15:55:10 +0800 Subject: [PATCH 8/9] fix: Get the length of fileName instead of path --- .../src/main/java/io/agentscope/core/formatter/MediaUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java index 9af9b8928..b25ca5be7 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java +++ b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java @@ -379,7 +379,7 @@ public static String getExtension(String path) { String fileName = fileNamePath.toString(); int dotIndex = fileName.lastIndexOf('.'); // Ensure the dot exists and is not the last character - if (dotIndex != -1 && dotIndex < path.length() - 1) { + if (dotIndex != -1 && dotIndex < fileName.length() - 1) { return fileName.substring(dotIndex + 1); } return ""; From 1343f1c6c8b8b8037d11abeac2c208a9d9fa8432 Mon Sep 17 00:00:00 2001 From: guanxu <1510424541@qq.com> Date: Fri, 27 Mar 2026 17:25:22 +0800 Subject: [PATCH 9/9] code clean up --- .../agentscope/core/formatter/MediaUtils.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java index b25ca5be7..6bca3945c 100644 --- a/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java +++ b/agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java @@ -354,22 +354,19 @@ public static String getExtension(String path) { return ""; } - boolean localFile = isLocalFile(path); - Path fileNamePath; - if (localFile) { - // treat as file - fileNamePath = Paths.get(path).normalize().getFileName(); - } else { - // treat as url - URI uri; - try { - uri = URI.create(path).normalize(); - } catch (IllegalArgumentException e) { - log.error("Invalid URL: {}", path); - return ""; + try { + if (isLocalFile(path)) { + // treat as file + fileNamePath = Paths.get(path).normalize().getFileName(); + } else { + // treat as url + URI uri = URI.create(path).normalize(); + fileNamePath = Paths.get(uri.getPath()).getFileName(); } - fileNamePath = Paths.get(uri.getPath()).getFileName(); + } catch (Exception e) { + log.warn("Invalid path: {}", path, e); + return ""; } if (fileNamePath == null) {