Skip to content

Commit 261da51

Browse files
committed
Added support for loading image files from local file:// paths
1 parent b52edd2 commit 261da51

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

tools/server/utils.hpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,29 @@ static json oaicompat_chat_params_parse(
636636
throw std::runtime_error("Failed to download image");
637637
}
638638

639-
} else {
639+
} else if (string_starts_with(url, "file://")) {
640+
// Strip off the leading "file://"
641+
std::string fname = url.substr(7);
642+
// load local file path
643+
raw_buffer buf;
644+
FILE * f = fopen(fname.c_str(), "rb");
645+
if (!f) {
646+
LOG_ERR("Unable to open file %s: %s\n", fname.c_str(), strerror(errno));
647+
throw std::runtime_error("Unable to open image file");
648+
}
649+
fseek(f, 0, SEEK_END);
650+
long file_size = ftell(f);
651+
fseek(f, 0, SEEK_SET);
652+
buf.resize(file_size);
653+
size_t n_read = fread(buf.data(), 1, file_size, f);
654+
fclose(f);
655+
if (n_read != (size_t)file_size) {
656+
LOG_ERR("Failed to read entire file %s", fname.c_str());
657+
throw std::runtime_error("Failed to read entire image file");
658+
}
659+
out_files.push_back(buf);
660+
661+
} else {
640662
// try to decode base64 image
641663
std::vector<std::string> parts = string_split<std::string>(url, /*separator*/ ',');
642664
if (parts.size() != 2) {

0 commit comments

Comments
 (0)