tests: add httpcommon tests and add new file_handler methods (#2712)
Co-authored-by: Mariotaku <mariotaku.lee@gmail.com>
This commit is contained in:
@@ -1255,9 +1255,7 @@ namespace config {
|
||||
bool config_loaded = false;
|
||||
try {
|
||||
// Create appdata folder if it does not exist
|
||||
if (!boost::filesystem::exists(platf::appdata().string())) {
|
||||
boost::filesystem::create_directories(platf::appdata().string());
|
||||
}
|
||||
file_handler::make_directory(platf::appdata().string());
|
||||
|
||||
// Create empty config file if it does not exist
|
||||
if (!fs::exists(sunshine.config_file)) {
|
||||
|
||||
@@ -501,9 +501,7 @@ namespace confighttp {
|
||||
auto url = inputTree.get("url", "");
|
||||
|
||||
const std::string coverdir = platf::appdata().string() + "/covers/";
|
||||
if (!boost::filesystem::exists(coverdir)) {
|
||||
boost::filesystem::create_directories(coverdir);
|
||||
}
|
||||
file_handler::make_directory(coverdir);
|
||||
|
||||
std::basic_string path = coverdir + http::url_escape(key) + ".png";
|
||||
if (!url.empty()) {
|
||||
|
||||
@@ -12,6 +12,37 @@
|
||||
#include "logging.h"
|
||||
|
||||
namespace file_handler {
|
||||
/**
|
||||
* @breif Get the parent directory of a file or directory.
|
||||
* @param path The path of the file or directory.
|
||||
* @return `std::string` : The parent directory.
|
||||
*/
|
||||
std::string
|
||||
get_parent_directory(const std::string &path) {
|
||||
// remove any trailing path separators
|
||||
std::string trimmed_path = path;
|
||||
while (!trimmed_path.empty() && trimmed_path.back() == '/') {
|
||||
trimmed_path.pop_back();
|
||||
}
|
||||
|
||||
std::filesystem::path p(trimmed_path);
|
||||
return p.parent_path().string();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Make a directory.
|
||||
* @param path The path of the directory.
|
||||
* @return `bool` : `true` on success, `false` on failure.
|
||||
*/
|
||||
bool
|
||||
make_directory(const std::string &path) {
|
||||
// first, check if the directory already exists
|
||||
if (std::filesystem::exists(path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return std::filesystem::create_directories(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read a file to string.
|
||||
|
||||
@@ -7,8 +7,15 @@
|
||||
#include <string>
|
||||
|
||||
namespace file_handler {
|
||||
std::string
|
||||
get_parent_directory(const std::string &path);
|
||||
|
||||
bool
|
||||
make_directory(const std::string &path);
|
||||
|
||||
std::string
|
||||
read_file(const char *path);
|
||||
|
||||
int
|
||||
write_file(const char *path, const std::string_view &contents);
|
||||
} // namespace file_handler
|
||||
|
||||
@@ -200,6 +200,14 @@ namespace http {
|
||||
BOOST_LOG(error) << "Couldn't create CURL instance";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string file_dir = file_handler::get_parent_directory(file);
|
||||
if (!file_handler::make_directory(file_dir)) {
|
||||
BOOST_LOG(error) << "Couldn't create directory ["sv << file_dir << ']';
|
||||
curl_easy_cleanup(curl);
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE *fp = fopen(file.c_str(), "wb");
|
||||
if (!fp) {
|
||||
BOOST_LOG(error) << "Couldn't open ["sv << file << ']';
|
||||
|
||||
Reference in New Issue
Block a user