Skip to content

Commit 93fdfa0

Browse files
authored
fix(core): fix path check with an invalid path (#394)
## AgentScope-Java Version 1.0.5-SNAPSHOT ## Description * Fixes: #392 * fix path check with an invalid path ## Checklist Please check the following items before code is ready to be reviewed. - [x] Code has been formatted with `mvn spotless:apply` - [x] All tests are passing (`mvn test`) - [x] Javadoc comments are complete and follow project conventions - [x] Related documentation has been updated (e.g. links, examples, etc.) - [x] Code is ready for review
1 parent a3e6f59 commit 93fdfa0

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

agentscope-core/src/main/java/io/agentscope/core/formatter/MediaUtils.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.net.URI;
2727
import java.net.URL;
2828
import java.nio.file.Files;
29+
import java.nio.file.InvalidPathException;
2930
import java.nio.file.Path;
3031
import java.util.Base64;
3132
import java.util.List;
@@ -175,10 +176,9 @@ public static String toFileProtocolUrl(String path) throws IOException {
175176
* @throws IOException If the resource read failed
176177
*/
177178
public static InputStream urlToInputStream(String url) throws IOException {
178-
Path path = Path.of(url);
179-
if (Files.exists(path)) {
179+
if (isFileExists(url)) {
180180
// Treat as local file
181-
return Files.newInputStream(path);
181+
return Files.newInputStream(Path.of(url));
182182
} else {
183183
// Treat as web URL
184184
return URI.create(url).toURL().openStream();
@@ -345,11 +345,18 @@ public static String getExtension(String path) {
345345

346346
/**
347347
* Check if a file exists.
348-
* @param path a local file path
349-
* @return true: exists, false: not exists
348+
* @param path a local file path or URL.
349+
* @return true: exists, false: not exists or path is invalid
350350
*/
351351
public static boolean isFileExists(String path) {
352-
return Files.exists(Path.of(path));
352+
if (!isLocalFile(path)) {
353+
return false;
354+
}
355+
try {
356+
return Files.exists(Path.of(path));
357+
} catch (InvalidPathException e) {
358+
return false;
359+
}
353360
}
354361

355362
/**

agentscope-core/src/test/java/io/agentscope/core/formatter/MediaUtilsTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
2121
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
2223
import static org.junit.jupiter.api.Assertions.assertThrows;
2324
import static org.junit.jupiter.api.Assertions.assertTrue;
2425

@@ -323,6 +324,20 @@ void testUrlToProtocolUrlWithUrl() throws IOException {
323324
assertEquals(url, protocolUrl);
324325
}
325326

327+
@Test
328+
@DisplayName("Should return null or empty string with null or empty path")
329+
void testUrlToProtocolUrlWithEmptyPath() throws IOException {
330+
assertNull(null, MediaUtils.urlToProtocolUrl(null));
331+
assertEquals("", MediaUtils.urlToProtocolUrl(""));
332+
}
333+
334+
@Test
335+
@DisplayName("Should return source path with invalid path")
336+
void testUrlToProtocolUrlWithInvalidPath() throws IOException {
337+
String path = "/path/to/file/<:|/\\>\u0000\b\n\t\0.txt";
338+
assertEquals(path, MediaUtils.urlToProtocolUrl(path));
339+
}
340+
326341
@Test
327342
@DisplayName("To file protocol url with file path")
328343
void testToFileProtocolUrlWithFile() throws IOException {

0 commit comments

Comments
 (0)