Skip to content

Commit ebcc671

Browse files
committed
some cleanup, and moving the allowed_local_media_path check to the arg parsing on startup instead of when a request comes in
1 parent d6d5e18 commit ebcc671

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

common/arg.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3299,7 +3299,14 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
32993299
{"--allowed-local-media-path"}, "PATH",
33003300
string_format("path from which local media files are allowed to be read from (default: none)"),
33013301
[](common_params & params, const std::string & value) {
3302-
params.allowed_local_media_path = value;
3302+
try {
3303+
params.allowed_local_media_path = std::filesystem::canonical(std::filesystem::path(value));
3304+
if (!std::filesystem::is_directory(params.allowed_local_media_path)) {
3305+
throw std::invalid_argument(string_format("allowed local media path must be a dir: %s", params.allowed_local_media_path.c_str()));
3306+
}
3307+
} catch (std::filesystem::filesystem_error &err) {
3308+
throw std::invalid_argument(string_format("invalid allowed local media path: %s", err.what()));
3309+
}
33033310
}
33043311
).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_ALLOWED_LOCAL_MEDIA_PATH"));
33053312
add_opt(common_arg(

common/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <map>
1111
#include <sstream>
1212
#include <cmath>
13+
#include <filesystem>
1314

1415
#include "ggml-opt.h"
1516
#include "llama-cpp.h"
@@ -433,7 +434,7 @@ struct common_params {
433434

434435
std::string hostname = "127.0.0.1";
435436
std::string public_path = ""; // NOLINT
436-
std::string allowed_local_media_path = ""; // NOLINT
437+
std::filesystem::path allowed_local_media_path; // NOLINT
437438
std::string api_prefix = ""; // NOLINT
438439
std::string chat_template = ""; // NOLINT
439440
bool use_jinja = false; // NOLINT

tools/server/utils.hpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ struct oaicompat_parser_options {
533533
bool allow_audio;
534534
bool enable_thinking = true;
535535
size_t local_media_max_size_mb;
536-
std::string allowed_local_media_path;
536+
std::filesystem::path allowed_local_media_path;
537537
};
538538

539539
// used by /chat/completions endpoint
@@ -643,18 +643,14 @@ static json oaicompat_chat_params_parse(
643643
}
644644

645645
} else if (string_starts_with(url, "file://")) {
646-
// Strip off the leading "file://"
647-
const std::string fname = url.substr(7);
648-
if (opt.allowed_local_media_path == "") {
646+
if (opt.allowed_local_media_path.empty()) {
649647
throw std::runtime_error("Local media paths are not enabled");
650648
}
651-
const std::filesystem::path allowed_local_media_path = std::filesystem::canonical(std::filesystem::path(opt.allowed_local_media_path));
649+
// Strip off the leading "file://"
650+
const std::string fname = url.substr(7);
652651
const std::filesystem::path input_path = std::filesystem::canonical(std::filesystem::path(fname));
653-
if (!std::filesystem::is_directory(allowed_local_media_path)) {
654-
throw std::runtime_error("Invalid local media path: " + opt.allowed_local_media_path);
655-
}
656-
auto [allowed_end, nothing] = std::mismatch(allowed_local_media_path.begin(), allowed_local_media_path.end(), input_path.begin());
657-
if (allowed_end != allowed_local_media_path.end()) {
652+
auto [allowed_end, nothing] = std::mismatch(opt.allowed_local_media_path.begin(), opt.allowed_local_media_path.end(), input_path.begin());
653+
if (allowed_end != opt.allowed_local_media_path.end()) {
658654
throw std::runtime_error("Local media file path not allowed: " + fname);
659655
}
660656
if (!std::filesystem::is_regular_file(input_path)) {

0 commit comments

Comments
 (0)