Skip to content

Commit

Permalink
feat: can display all files, including subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
helintongh committed Sep 5, 2023
1 parent 35e2225 commit 5ef29fc
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions include/cinatra/http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,24 +601,43 @@ class http_server_ : private noncopyable {
}
}

int get_static_dir_filenames(const std::string &dir,
std::vector<std::string> &filenames,
size_t dir_name_length) {
fs::path path(dir);
if (!fs::exists(path))
return -1;
fs::directory_iterator end_iter;
for (fs::directory_iterator iter(path); iter != end_iter; ++iter) {
if (fs::is_directory(iter->status())) {
get_static_dir_filenames(iter->path().string(), filenames,
dir_name_length);
}
else {
auto u8_path_name = iter->path().u8string();
std::string relative_path(u8_path_name.begin(), u8_path_name.end());
size_t length = relative_path.length();
relative_path = relative_path.substr(dir_name_length + 1, length);
filenames.push_back(relative_path);
}
}
return filenames.size();
}

bool build_http_download_lists_response(request &req,
std::string &&resp_str) {
std::string res_str = "";
std::string href_of_head = "<br><a href=\"http://" +
std::string(req.get_header_value("host")) + "/";
std::string href_of_tail = "\"target=\"_blank\">";
if (req.get_res_path() == download_display_file_dir_) {
std::string file_dir_str = static_dir_;
std::filesystem::path dir_path(file_dir_str);

for (const auto &entry : std::filesystem::directory_iterator(dir_path)) {
if (!entry.is_directory()) {
auto u8_file_name = entry.path().filename().u8string();
std::string file_name_str(u8_file_name.begin(), u8_file_name.end());
std::string full_href = href_of_head + file_name_str + href_of_tail +
file_name_str + "</a></br>";
res_str += full_href;
}
std::vector<std::string> files;
size_t static_dir_length = static_dir_.length();
get_static_dir_filenames(static_dir_, files, static_dir_length);
for (const auto &file : files) {
std::string full_href =
href_of_head + file + href_of_tail + file + "</a></br>";
res_str += full_href;
}
std::string ret =
download_dir_response_head_ + res_str + download_dir_response_tail_;
Expand Down

0 comments on commit 5ef29fc

Please sign in to comment.