forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownload_task.h
More file actions
164 lines (133 loc) · 4.16 KB
/
download_task.h
File metadata and controls
164 lines (133 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#pragma once
#include <json/json.h>
#include <filesystem>
#include <optional>
#include <sstream>
#include <string>
enum class DownloadType { Model, Engine, Miscellaneous, CudaToolkit, Cortex };
struct DownloadItem {
std::string id;
std::string downloadUrl;
/**
* An absolute path to where the file is located (locally).
*/
std::filesystem::path localPath;
std::optional<std::string> checksum;
std::optional<uint64_t> bytes;
std::optional<uint64_t> downloadedBytes;
std::string ToString() const {
std::ostringstream output;
output << "DownloadItem{id: " << id << ", downloadUrl: " << downloadUrl
<< ", localContainerPath: " << localPath
<< ", bytes: " << bytes.value_or(0)
<< ", downloadedBytes: " << downloadedBytes.value_or(0)
<< ", checksum: " << checksum.value_or("N/A") << "}";
return output.str();
}
};
inline std::string DownloadTypeToString(DownloadType type) {
switch (type) {
case DownloadType::Model:
return "Model";
case DownloadType::Engine:
return "Engine";
case DownloadType::Miscellaneous:
return "Miscellaneous";
case DownloadType::CudaToolkit:
return "CudaToolkit";
case DownloadType::Cortex:
return "Cortex";
default:
return "Unknown";
}
}
inline DownloadType DownloadTypeFromString(const std::string& str) {
if (str == "Model") {
return DownloadType::Model;
} else if (str == "Engine") {
return DownloadType::Engine;
} else if (str == "Miscellaneous") {
return DownloadType::Miscellaneous;
} else if (str == "CudaToolkit") {
return DownloadType::CudaToolkit;
} else if (str == "Cortex") {
return DownloadType::Cortex;
} else {
return DownloadType::Miscellaneous;
}
}
struct DownloadTask {
enum class Status { Pending, InProgress, Completed, Cancelled, Error };
std::string id;
Status status;
DownloadType type;
std::vector<DownloadItem> items;
std::string ToString() const {
std::ostringstream output;
output << "DownloadTask{id: " << id << ", type: " << static_cast<int>(type)
<< ", items: [";
for (const auto& item : items) {
output << item.ToString() << ", ";
}
output << "]}";
return output.str();
}
Json::Value ToJsonCpp() const {
Json::Value root;
root["id"] = id;
root["type"] = DownloadTypeToString(type);
Json::Value itemsArray(Json::arrayValue);
for (const auto& item : items) {
Json::Value itemObj;
itemObj["id"] = item.id;
itemObj["downloadUrl"] = item.downloadUrl;
itemObj["localPath"] = item.localPath.string();
itemObj["checksum"] = item.checksum.value_or("N/A");
itemObj["bytes"] = Json::Value::UInt64(item.bytes.value_or(0));
itemObj["downloadedBytes"] =
Json::Value::UInt64(item.downloadedBytes.value_or(0));
itemsArray.append(itemObj);
}
root["items"] = itemsArray;
return root;
}
};
namespace common {
inline DownloadItem GetDownloadItemFromJson(const Json::Value item_json) {
DownloadItem item;
if (!item_json["id"].isNull()) {
item.id = item_json["id"].asString();
}
if (!item_json["downloadUrl"].isNull()) {
item.downloadUrl = item_json["downloadUrl"].asString();
}
if (!item_json["localPath"].isNull()) {
item.localPath = std::filesystem::path(item_json["localPath"].asString());
}
if (!item_json["checksum"].isNull()) {
item.checksum = item_json["checksum"].asString();
}
if (!item_json["bytes"].isNull()) {
item.bytes = item_json["bytes"].asUInt64();
}
if (!item_json["downloadedBytes"].isNull()) {
item.downloadedBytes = item_json["downloadedBytes"].asUInt64();
}
return item;
}
inline DownloadTask GetDownloadTaskFromJson(const Json::Value item_json) {
DownloadTask task;
if (!item_json["id"].isNull()) {
task.id = item_json["id"].asString();
}
if (!item_json["type"].isNull()) {
task.type = DownloadTypeFromString(item_json["type"].asString());
}
if (!item_json["items"].isNull() && item_json["items"].isArray()) {
for (auto const& i_json : item_json["items"]) {
task.items.emplace_back(GetDownloadItemFromJson(i_json));
}
}
return task;
}
} // namespace common