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

Commit 05d67be

Browse files
authored
Merge pull request #1179 from janhq/j/update-download-lib-to-curl
adding libcurl to support pause/resume download
2 parents efb426c + c3c3946 commit 05d67be

22 files changed

+628
-396
lines changed

engine/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ find_package(CLI11 CONFIG REQUIRED)
106106
find_package(unofficial-minizip CONFIG REQUIRED)
107107
find_package(LibArchive REQUIRED)
108108
find_package(tabulate CONFIG REQUIRED)
109+
find_package(CURL REQUIRED)
109110

110111
# Build using CMAKE-JS
111112
if(DEFINED CMAKE_JS_INC)
@@ -150,6 +151,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE CLI11::CLI11)
150151
target_link_libraries(${PROJECT_NAME} PRIVATE unofficial::minizip::minizip)
151152
target_link_libraries(${PROJECT_NAME} PRIVATE LibArchive::LibArchive)
152153
target_link_libraries(${PROJECT_NAME} PRIVATE tabulate::tabulate)
154+
target_link_libraries(${PROJECT_NAME} PRIVATE CURL::libcurl)
153155

154156
# Build using CMAKE-JS
155157
if(DEFINED CMAKE_JS_INC)

engine/commands/cortex_upd_cmd.cc

Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// clang-format off
2-
#include "utils/cortex_utils.h"
3-
// clang-format on
41
#include "cortex_upd_cmd.h"
52
#include "httplib.h"
63
#include "nlohmann/json.hpp"
@@ -10,11 +7,10 @@
107
#include "utils/file_manager_utils.h"
118
#include "utils/logging_utils.h"
129
#include "utils/system_info_utils.h"
10+
#include "utils/url_parser.h"
1311

1412
namespace commands {
1513

16-
CortexUpdCmd::CortexUpdCmd() {}
17-
1814
void CortexUpdCmd::Exec(std::string v) {
1915
{
2016
auto config = file_manager_utils::GetCortexConfig();
@@ -38,14 +34,7 @@ void CortexUpdCmd::Exec(std::string v) {
3834
}
3935

4036
bool CortexUpdCmd::GetStableAndBeta(const std::string& v) {
41-
// Check if the architecture and OS are supported
4237
auto system_info = system_info_utils::GetSystemInfo();
43-
if (system_info.arch == system_info_utils::kUnsupported ||
44-
system_info.os == system_info_utils::kUnsupported) {
45-
CTL_ERR("Unsupported OS or architecture: " << system_info.os << ", "
46-
<< system_info.arch);
47-
return false;
48-
}
4938
CTL_INF("OS: " << system_info.os << ", Arch: " << system_info.arch);
5039

5140
// Download file
@@ -84,38 +73,35 @@ bool CortexUpdCmd::GetStableAndBeta(const std::string& v) {
8473
for (auto& asset : assets) {
8574
auto asset_name = asset["name"].get<std::string>();
8675
if (asset_name == matched_variant) {
87-
std::string host{"https://github.com"};
88-
89-
auto full_url = asset["browser_download_url"].get<std::string>();
90-
std::string path = full_url.substr(host.length());
91-
92-
auto fileName = asset["name"].get<std::string>();
93-
CTL_INF("URL: " << full_url);
94-
95-
auto download_task = DownloadTask{.id = "cortex",
96-
.type = DownloadType::Cortex,
97-
.error = std::nullopt,
98-
.items = {DownloadItem{
99-
.id = "cortex",
100-
.host = host,
101-
.fileName = fileName,
102-
.type = DownloadType::Cortex,
103-
.path = path,
104-
}}};
105-
106-
DownloadService download_service;
107-
download_service.AddDownloadTask(
108-
download_task,
109-
[this](const std::string& absolute_path, bool unused) {
76+
auto download_url =
77+
asset["browser_download_url"].get<std::string>();
78+
auto file_name = asset["name"].get<std::string>();
79+
CTL_INF("Download url: " << download_url);
80+
81+
auto local_path =
82+
file_manager_utils::GetExecutableFolderContainerPath() /
83+
"cortex" / asset_name;
84+
auto download_task{DownloadTask{.id = "cortex",
85+
.type = DownloadType::Cortex,
86+
.items = {DownloadItem{
87+
.id = "cortex",
88+
.downloadUrl = download_url,
89+
.localPath = local_path,
90+
}}}};
91+
92+
DownloadService().AddDownloadTask(
93+
download_task, [](const DownloadTask& finishedTask) {
11094
// try to unzip the downloaded file
111-
std::filesystem::path download_path{absolute_path};
112-
CTL_INF("Downloaded engine path: " << download_path.string());
95+
CTL_INF("Downloaded engine path: "
96+
<< finishedTask.items[0].localPath.string());
11397

114-
std::filesystem::path extract_path =
115-
download_path.parent_path().parent_path();
98+
auto extract_path = finishedTask.items[0]
99+
.localPath.parent_path()
100+
.parent_path();
116101

117-
archive_utils::ExtractArchive(download_path.string(),
118-
extract_path.string());
102+
archive_utils::ExtractArchive(
103+
finishedTask.items[0].localPath.string(),
104+
extract_path.string());
119105

120106
CTL_INF("Finished!");
121107
});
@@ -145,56 +131,59 @@ bool CortexUpdCmd::GetStableAndBeta(const std::string& v) {
145131
}
146132

147133
bool CortexUpdCmd::GetNightly(const std::string& v) {
148-
// Check if the architecture and OS are supported
149134
auto system_info = system_info_utils::GetSystemInfo();
150-
if (system_info.arch == system_info_utils::kUnsupported ||
151-
system_info.os == system_info_utils::kUnsupported) {
152-
CTL_ERR("Unsupported OS or architecture: " << system_info.os << ", "
153-
<< system_info.arch);
154-
return false;
155-
}
156135
CTL_INF("OS: " << system_info.os << ", Arch: " << system_info.arch);
157136

158137
// Download file
159138
std::string version = v.empty() ? "latest" : std::move(v);
160-
std::ostringstream release_path;
161-
release_path << "cortex/" << version << "/" << system_info.os << "-"
162-
<< system_info.arch << "/" << kNightlyFileName;
163-
CTL_INF("Engine release path: " << kNightlyHost << "/" << release_path.str());
164-
165-
auto download_task = DownloadTask{.id = "cortex",
166-
.type = DownloadType::Cortex,
167-
.error = std::nullopt,
168-
.items = {DownloadItem{
169-
.id = "cortex",
170-
.host = kNightlyHost,
171-
.fileName = kNightlyFileName,
172-
.type = DownloadType::Cortex,
173-
.path = release_path.str(),
174-
}}};
175-
176-
DownloadService download_service;
177-
download_service.AddDownloadTask(
178-
download_task, [this](const std::string& absolute_path, bool unused) {
139+
std::string os_arch{system_info.os + "-" + system_info.arch};
140+
const char* paths[] = {
141+
"cortex",
142+
version.c_str(),
143+
os_arch.c_str(),
144+
kNightlyFileName,
145+
};
146+
std::vector<std::string> path_list(paths, std::end(paths));
147+
auto url_obj = url_parser::Url{
148+
.protocol = "https",
149+
.host = kNightlyHost,
150+
.pathParams = path_list,
151+
};
152+
153+
CTL_INF("Engine release path: " << url_parser::FromUrl(url_obj));
154+
155+
std::filesystem::path localPath =
156+
file_manager_utils::GetExecutableFolderContainerPath() / "cortex" /
157+
path_list.back();
158+
auto download_task =
159+
DownloadTask{.id = "cortex",
160+
.type = DownloadType::Cortex,
161+
.items = {DownloadItem{
162+
.id = "cortex",
163+
.downloadUrl = url_parser::FromUrl(url_obj),
164+
.localPath = localPath,
165+
}}};
166+
167+
DownloadService().AddDownloadTask(
168+
download_task, [](const DownloadTask& finishedTask) {
179169
// try to unzip the downloaded file
180-
std::filesystem::path download_path{absolute_path};
181-
CTL_INF("Downloaded engine path: " << download_path.string());
170+
CTL_INF("Downloaded engine path: "
171+
<< finishedTask.items[0].localPath.string());
182172

183-
std::filesystem::path extract_path =
184-
download_path.parent_path().parent_path();
173+
auto extract_path =
174+
finishedTask.items[0].localPath.parent_path().parent_path();
185175

186-
archive_utils::ExtractArchive(download_path.string(),
176+
archive_utils::ExtractArchive(finishedTask.items[0].localPath.string(),
187177
extract_path.string());
188178

189179
CTL_INF("Finished!");
190180
});
191181

192-
// Replace binay file
182+
// Replace binary file
193183
auto executable_path = file_manager_utils::GetExecutableFolderContainerPath();
194184
auto src = std::filesystem::temp_directory_path() / "cortex" / kCortexBinary /
195185
GetCortexBinary();
196186
auto dst = executable_path / GetCortexBinary();
197187
return ReplaceBinaryInflight(src, dst);
198188
}
199-
200-
} // namespace commands
189+
} // namespace commands

engine/commands/cortex_upd_cmd.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#pragma once
2-
#include <optional>
32
#include <string>
43

54
#include "httplib.h"
@@ -11,7 +10,7 @@ namespace commands {
1110
#ifndef CORTEX_VARIANT
1211
#define CORTEX_VARIANT file_manager_utils::kProdVariant
1312
#endif
14-
constexpr const auto kNightlyHost = "https://delta.jan.ai";
13+
constexpr const auto kNightlyHost = "delta.jan.ai";
1514
constexpr const auto kNightlyFileName = "cortex-nightly.tar.gz";
1615
const std::string kCortexBinary = "cortex";
1716

@@ -113,12 +112,10 @@ inline bool ReplaceBinaryInflight(const std::filesystem::path& src,
113112

114113
class CortexUpdCmd {
115114
public:
116-
CortexUpdCmd();
117115
void Exec(std::string version);
118116

119117
private:
120118
bool GetStableAndBeta(const std::string& v);
121119
bool GetNightly(const std::string& v);
122120
};
123-
124-
} // namespace commands
121+
} // namespace commands

0 commit comments

Comments
 (0)