Skip to content

Commit 22552b7

Browse files
committed
fixed file uris
1 parent 2c6fd9c commit 22552b7

File tree

1 file changed

+22
-21
lines changed
  • src/main/java/org/myrobotlab/service

1 file changed

+22
-21
lines changed

src/main/java/org/myrobotlab/service/LLM.java

+22-21
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import java.io.InputStream;
66
import java.net.MalformedURLException;
77
import java.net.URI;
8+
import java.net.URISyntaxException;
89
import java.net.URL;
910
import java.nio.file.Files;
11+
import java.nio.file.Path;
1012
import java.nio.file.Paths;
1113
import java.time.LocalDateTime;
1214
import java.time.format.DateTimeFormatter;
@@ -722,31 +724,30 @@ public void clearHistory() {
722724
userMessages.clear();
723725
}
724726

725-
@Override
726-
public void onImage(ImageData img) {
727-
StringBuilder fileUrl = new StringBuilder();
728-
if (img.encoding == null && img.src != null && (!img.src.startsWith("file://") && !img.src.startsWith("http://") && !img.src.startsWith("https://"))) {
729-
if (img.src.startsWith("/") || img.src.contains(":\\")) {
730-
// absolute path already
731-
fileUrl.append("file://");
732-
// Windows :()
733-
if (img.src != null) {
734-
fileUrl.append(img.src.replace("\\", "/"));
735-
}
736-
if (img.source != null) {
737-
fileUrl.append(img.source.replace("\\", "/"));
727+
public static URI toFileURI(String filePath) throws URISyntaxException {
728+
try {
729+
URI uri = new URI(filePath);
730+
if (uri.isAbsolute()) {
731+
return uri;
738732
}
739-
740-
} else {
741-
// assume relative
742-
File file = new File(img.src);
743-
fileUrl.append("file://");
744-
fileUrl.append(file.getAbsolutePath());
745-
}
733+
} catch (URISyntaxException e) {
746734
}
735+
736+
Path path = Paths.get(filePath);
737+
738+
return path.toUri();
739+
}
740+
741+
@Override
742+
public void onImage(ImageData img) {
743+
try {
744+
String src = (img.src != null)?img.src:img.source;
747745
List<String> urls = new ArrayList<>();
748-
urls.add(fileUrl.toString());
746+
urls.add(toFileURI(src).toASCIIString());
749747
getResponse(urls);
748+
} catch(Exception e) {
749+
error(e);
750+
}
750751
}
751752

752753
public static void main(String[] args) {

0 commit comments

Comments
 (0)