Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 92d3254

Browse files
authored
Merge pull request #1382 from janhq/j/fix-validate-before-pull
fix: validate url before pull
2 parents 9d39a50 + bc17643 commit 92d3254

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

engine/services/model_service.cc

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "model_service.h"
22
#include <filesystem>
33
#include <iostream>
4+
#include <optional>
45
#include <ostream>
56
#include "config/gguf_parser.h"
67
#include "config/yaml_config.h"
@@ -58,8 +59,7 @@ cpp::result<DownloadTask, std::string> GetDownloadTask(
5859
httplib::Client cli(url.GetProtocolAndHost());
5960
auto res = cli.Get(url.GetPathAndQuery());
6061
if (res->status != httplib::StatusCode::OK_200) {
61-
auto err = res.error();
62-
return cpp::fail("HTTP error: " + httplib::to_string(err));
62+
return cpp::fail("Model " + modelId + " not found");
6363
}
6464
auto jsonResponse = json::parse(res->body);
6565

@@ -103,7 +103,6 @@ cpp::result<std::string, std::string> ModelService::DownloadModel(
103103
}
104104

105105
if (string_utils::StartsWith(input, "https://")) {
106-
// TODO: better name, for example handle url
107106
return HandleUrl(input, async);
108107
}
109108

@@ -195,13 +194,27 @@ cpp::result<std::string, std::string> ModelService::HandleUrl(
195194
auto file_name{url_obj.pathParams.back()};
196195

197196
if (author == "cortexso") {
198-
// TODO: try to get the branch
199197
return DownloadModelFromCortexso(model_id);
200198
}
201199

200+
if (url_obj.pathParams.size() < 5) {
201+
if (url_obj.pathParams.size() < 2) {
202+
return cpp::fail("Invalid url: " + url);
203+
}
204+
return DownloadHuggingFaceGgufModel(author, model_id, std::nullopt, async);
205+
}
206+
202207
std::string huggingFaceHost{kHuggingFaceHost};
203-
std::string unique_model_id{huggingFaceHost + "/" + author + "/" + model_id +
204-
"/" + file_name};
208+
std::string unique_model_id{author + ":" + model_id + ":" + file_name};
209+
210+
cortex::db::Models modellist_handler;
211+
auto model_entry = modellist_handler.GetModelInfo(unique_model_id);
212+
213+
if (model_entry.has_value()) {
214+
CLI_LOG("Model already downloaded: " << unique_model_id);
215+
return cpp::fail("Please delete the model before downloading again");
216+
}
217+
205218
auto local_path{file_manager_utils::GetModelsContainerPath() /
206219
"huggingface.co" / author / model_id / file_name};
207220

@@ -240,7 +253,7 @@ cpp::result<std::string, std::string> ModelService::HandleUrl(
240253
} else {
241254
auto result = download_service_.AddDownloadTask(downloadTask, on_finished);
242255
if (result.has_error()) {
243-
// CTL_ERR(result.error());
256+
CTL_ERR(result.error());
244257
return cpp::fail(result.error());
245258
} else if (result && result.value()) {
246259
CLI_LOG("Model " << model_id << " downloaded successfully!")
@@ -519,4 +532,4 @@ cpp::result<bool, std::string> ModelService::GetModelStatus(
519532
return cpp::fail("Fail to get model status with ID '" + model_handle +
520533
"': " + e.what());
521534
}
522-
}
535+
}

0 commit comments

Comments
 (0)