|
5 | 5 | import java.io.InputStream; |
6 | 6 | import java.net.MalformedURLException; |
7 | 7 | import java.net.URI; |
| 8 | +import java.net.URISyntaxException; |
8 | 9 | import java.net.URL; |
9 | 10 | import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
10 | 12 | import java.nio.file.Paths; |
11 | 13 | import java.time.LocalDateTime; |
12 | 14 | import java.time.format.DateTimeFormatter; |
@@ -722,31 +724,30 @@ public void clearHistory() { |
722 | 724 | userMessages.clear(); |
723 | 725 | } |
724 | 726 |
|
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; |
738 | 732 | } |
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) { |
746 | 734 | } |
| 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; |
747 | 745 | List<String> urls = new ArrayList<>(); |
748 | | - urls.add(fileUrl.toString()); |
| 746 | + urls.add(toFileURI(src).toASCIIString()); |
749 | 747 | getResponse(urls); |
| 748 | + } catch(Exception e) { |
| 749 | + error(e); |
| 750 | + } |
750 | 751 | } |
751 | 752 |
|
752 | 753 | public static void main(String[] args) { |
|
0 commit comments