| 227 | return wav; |
| 228 | } |
| 229 | |
| 230 | std::unordered_map<std::string, std::string> options_from_object(const Value * value); |
| 231 | |
| 232 | std::string safe_upload_name(std::string value) { |
| 233 | value = std::filesystem::path(value).filename().string(); |
| 234 | for (char & ch : value) { |
| 235 | const auto uch = static_cast<unsigned char>(ch); |
| 236 | if (!(std::isalnum(uch) || ch == '.' || ch == '-' || ch == '_')) { |
| 237 | ch = '_'; |
| 238 | } |
| 239 | } |
| 240 | if (value.empty() || value == "." || value == "..") { |
| 241 | value = "audio.wav"; |
| 242 | } |
| 243 | return value; |
| 244 | } |
| 245 | |
| 246 | ServerModelConfig model_config_from_json( |
| 247 | const Value & body, |
| 248 | const std::filesystem::path & request_base, |
| 249 | bool lazy) { |
| 250 | ServerModelConfig model; |
| 251 | model.id = engine::io::json::require_string(body, "id"); |
| 252 | model.path = resolve_path(request_base, engine::io::json::require_string(body, "path")); |
| 253 | model.family = engine::io::json::require_string(body, "family"); |
| 254 | model.task = engine::io::json::optional_string(body, "task", model.task); |
| 255 | model.mode = engine::io::json::optional_string(body, "mode", model.mode); |
| 256 | model.lazy = lazy; |
| 257 | if (const auto * value = body.find("model_spec_override")) { |
| 258 | model.model_spec_override = resolve_path(request_base, value->as_string()); |
| 259 | } |
| 260 | if (const auto * value = body.find("config")) { |
| 261 | model.config_id = value->as_string(); |
| 262 | } |
| 263 | if (const auto * value = body.find("weight")) { |
| 264 | model.weight_id = value->as_string(); |
| 265 | } |
| 266 | model.load_options = options_from_object(body.find("load_options")); |
| 267 | model.session_options = options_from_object(body.find("session_options")); |
| 268 | return model; |
| 269 | } |
| 270 | |
| 271 | // Minimal application/x-www-form-urlencoded query string lookup, e.g. |
| 272 | // query_param("model=pocket-tts&foo=bar", "model") -> "pocket-tts". |
| 273 | std::string query_param(const std::string & query, const std::string & key) { |
| 274 | size_t pos = 0; |
| 275 | while (pos < query.size()) { |
| 276 | const size_t amp = query.find('&', pos); |
| 277 | const std::string pair = query.substr(pos, amp == std::string::npos ? std::string::npos : amp - pos); |
| 278 | const auto eq = pair.find('='); |
| 279 | const std::string name = pair.substr(0, eq); |
| 280 | if (name == key) { |
| 281 | return eq == std::string::npos ? "" : pair.substr(eq + 1); |
| 282 | } |
| 283 | if (amp == std::string::npos) { |
| 284 | break; |
| 285 | } |
| 286 | pos = amp + 1; |
no test coverage detected