std::string_view 的使用
This commit is contained in:
@@ -52,28 +52,29 @@ bool Is_Safe_Archive_Extension(std::string_view extension) {
|
||||
});
|
||||
}
|
||||
|
||||
std::string Normalize_Archive_Extension(std::string extension,
|
||||
std::string fallback = "zip") {
|
||||
while (!extension.empty() && extension.front() == '.') {
|
||||
extension.erase(extension.begin());
|
||||
std::string Normalize_Archive_Extension(std::string_view extension,
|
||||
std::string_view fallback = "zip") {
|
||||
auto normalized = std::string(extension);
|
||||
while (!normalized.empty() && normalized.front() == '.') {
|
||||
normalized.erase(normalized.begin());
|
||||
}
|
||||
if (extension.empty()) {
|
||||
extension = std::move(fallback);
|
||||
if (normalized.empty()) {
|
||||
normalized = std::string(fallback);
|
||||
}
|
||||
if (!Is_Safe_Archive_Extension(extension)) {
|
||||
throw std::invalid_argument("invalid archive extension: " + extension);
|
||||
if (!Is_Safe_Archive_Extension(normalized)) {
|
||||
throw std::invalid_argument("invalid archive extension: " + normalized);
|
||||
}
|
||||
return extension;
|
||||
return normalized;
|
||||
}
|
||||
|
||||
std::optional<std::string> Try_Get_String_Field(const JSON &object,
|
||||
const std::string &key) {
|
||||
std::string_view key) {
|
||||
const JSON *value = object.get(key);
|
||||
if (value == nullptr) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (value->valueType != Psc::String) {
|
||||
throw std::invalid_argument(key + " must be a string");
|
||||
throw std::invalid_argument(std::string(key) + " must be a string");
|
||||
}
|
||||
return value->val;
|
||||
}
|
||||
@@ -85,7 +86,7 @@ struct Archive_Export_Request {
|
||||
|
||||
Archive_Export_Request
|
||||
Parse_Archive_Export_Request(const drogon::HttpRequestPtr &req,
|
||||
std::string default_version) {
|
||||
std::string_view default_version) {
|
||||
Archive_Export_Request result;
|
||||
result.version = std::move(default_version);
|
||||
|
||||
@@ -155,7 +156,7 @@ void Global::init_web_server() {
|
||||
static auto &web_server = web_server_config;
|
||||
Web_Server &svr = this->svr;
|
||||
auto &da = drogon::app();
|
||||
auto return_file = [](const std::string &path) {
|
||||
auto return_file = [](std::string_view path) {
|
||||
auto res = drogon::HttpResponse::newHttpResponse();
|
||||
if (std::filesystem::exists(path)) {
|
||||
std::string content;
|
||||
@@ -175,9 +176,9 @@ void Global::init_web_server() {
|
||||
}
|
||||
return res;
|
||||
};
|
||||
auto add_path = [return_file](const std::string &path) {
|
||||
auto add_path = [return_file](std::string_view path) {
|
||||
drogon::app().registerHandlerViaRegex(
|
||||
path,
|
||||
std::string(path),
|
||||
[return_file](
|
||||
const drogon::HttpRequestPtr &req,
|
||||
std::function<void(const drogon::HttpResponsePtr &)> &&callback) {
|
||||
@@ -251,7 +252,7 @@ void Global::init_web_server() {
|
||||
try {
|
||||
auto export_request =
|
||||
Parse_Archive_Export_Request(req, Global::instance()->version);
|
||||
const std::string &name = export_request.version;
|
||||
std::string name = export_request.version;
|
||||
auto version_dir = std::filesystem::path(get_exe_dir() + "/..");
|
||||
std::string data = archive_directory_to_memory(
|
||||
version_dir / name / "logs", {}, export_request.archive);
|
||||
@@ -260,7 +261,7 @@ void Global::init_web_server() {
|
||||
resp->setContentTypeString("application/octet-stream");
|
||||
resp->addHeader("Content-Disposition",
|
||||
"attachment; filename*=UTF-8''" + name + "_logs." +
|
||||
export_request.archive.extension);
|
||||
std::string(export_request.archive.extension));
|
||||
resp->addHeader("ok", "true");
|
||||
cb(resp);
|
||||
}
|
||||
@@ -283,7 +284,7 @@ void Global::init_web_server() {
|
||||
try {
|
||||
auto export_request =
|
||||
Parse_Archive_Export_Request(req, Global::instance()->version);
|
||||
const std::string &name = export_request.version;
|
||||
std::string name = export_request.version;
|
||||
auto version_dir = std::filesystem::path(get_exe_dir() + "/..");
|
||||
|
||||
std::cout << "准备压缩:" << std::endl;
|
||||
@@ -292,7 +293,7 @@ void Global::init_web_server() {
|
||||
{"logs/", "uploads/", "drogon_uploads/",
|
||||
"backups/", // ghw的什么文件路径
|
||||
"stack_trace.txt", "pt.log", "ecap_server.log", "Core", "core",
|
||||
name + ".zip", name + "." + export_request.archive.extension},
|
||||
name + ".zip", name + "." + std::string(export_request.archive.extension)},
|
||||
export_request.archive);
|
||||
std::cout << "压缩成功:" << data.size() << std::endl;
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
@@ -300,7 +301,7 @@ void Global::init_web_server() {
|
||||
resp->setContentTypeString("application/octet-stream");
|
||||
resp->addHeader("Content-Disposition",
|
||||
"attachment; filename*=UTF-8''" + name + "." +
|
||||
export_request.archive.extension);
|
||||
std::string(export_request.archive.extension));
|
||||
resp->addHeader("ok", "true");
|
||||
cb(resp);
|
||||
} catch (const std::exception &e) {
|
||||
@@ -327,10 +328,10 @@ void Global::init_web_server() {
|
||||
&&cb) {
|
||||
|
||||
#ifdef __linux__
|
||||
std::string root_dir = Psc::get_exe_dir() + "/..";
|
||||
std::string_view root_dir = Psc::get_exe_dir() + "/..";
|
||||
|
||||
std::string log_file = fmt::format("{}/restart_device.log", root_dir);
|
||||
std::string cmd = fmt::format(
|
||||
std::string_view log_file = fmt::format("{}/restart_device.log", root_dir);
|
||||
std::string_view cmd = fmt::format(
|
||||
R"(systemd-run --scope --slice=system.slice sudo bash "{}/restart_device.bash" > {} 2>&1 &)",
|
||||
root_dir, log_file);
|
||||
std::cout << "Generated Command: " << cmd << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user