diff --git a/index/listing.cpp b/index/listing.cpp index 8d8b05cda..b6d65b89f 100644 --- a/index/listing.cpp +++ b/index/listing.cpp @@ -17,6 +17,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include #include #include #include @@ -36,26 +37,29 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #endif #pragma GCC diagnostic pop -using namespace std; -using namespace pugi; -bool index_listing_match (string url) +static bool index_listing_match (const std::string& url) { - if (url.length () >= 9) if (url.substr (0, 9) == "revisions") return true; - if (url.length () >= 7) if (url.substr (0, 7) == "exports") return true; + if (url.length () >= 9) + if (url.substr (0, 9) == "revisions") + return true; + if (url.length () >= 7) + if (url.substr (0, 7) == "exports") + return true; return false; } -string index_listing_url (string url) +std::string index_listing_url (const std::string& url) { - if (index_listing_match (url)) return url; + if (index_listing_match (url)) + return url; return "\\"; } -bool index_listing_acl (Webserver_Request& webserver_request, string url) +bool index_listing_acl (Webserver_Request& webserver_request, std::string url) { // Bible exports are public. if (url.find ("exports") == 0) { @@ -66,64 +70,66 @@ bool index_listing_acl (Webserver_Request& webserver_request, string url) } -string index_listing (Webserver_Request& webserver_request, string url) +std::string index_listing (Webserver_Request& webserver_request, std::string url) { - string page; - page = assets_page::header (translate ("Bibledit"), webserver_request); + std::string page = assets_page::header (translate ("Bibledit"), webserver_request); // No breadcrumbs because the user can arrive here from more than one place. Assets_View view; url = filter_url_urldecode (url); - url = filter_url_create_path ({string(), url}); + url = filter_url_create_path ({std::string(), url}); url = filter::strings::replace (R"(\)", "/", url); view.set_variable ("url", url); - string parent = filter_url_dirname_web (url); + const std::string parent = filter_url_dirname_web (url); if (parent.length () > 1) { view.enable_zone ("parent"); view.set_variable ("parent", parent); } - string directory = filter_url_create_root_path ({url}); + const std::string directory = filter_url_create_root_path ({url}); if (!file_or_dir_exists (directory) || filter_url_is_dir (directory)) { // The document that contains the listing. - xml_document listing_document; - string listing; + pugi::xml_document listing_document; + std::string listing; // Check the files in this folder. - vector files = filter_url_scandir (directory); + std::vector files = filter_url_scandir (directory); // Handle empty folder. if (files.empty()) { - xml_node span_node = listing_document.append_child("span"); + pugi::xml_node span_node = listing_document.append_child("span"); span_node.text().set(translate ("No files in this folder").c_str()); } // Handle file / folder listing. else { - xml_node table_node = listing_document.append_child("table"); - for (auto & file : files) { + pugi::xml_node table_node = listing_document.append_child("table"); + for (const auto& file : files) { // Open a new row. - xml_node tr_node = table_node.append_child("tr"); + pugi::xml_node tr_node = table_node.append_child("tr"); // Add the link to the file in the first column. - xml_node td_node = tr_node.append_child("td"); - xml_node a_node = td_node.append_child("a"); - string href = filter_url_create_path ({url, file}); + pugi::xml_node td_node = tr_node.append_child("td"); + pugi::xml_node a_node = td_node.append_child("a"); + const std::string href = filter_url_create_path ({url, file}); a_node.append_attribute("href") = href.c_str(); a_node.text().set(file.c_str()); - // Implement force download for USFM files. + // Implement force download for USFM files and other files. // https://github.com/bibledit/cloud/issues/771 - string suffix = filter_url_get_extension (file); - if (suffix == "usfm") { + constexpr std::array downloadable_suffixes { + "bblx", "exp", "odt", "zip", "usfm" + }; + const std::string suffix = filter_url_get_extension (file); + if (std::find (downloadable_suffixes.begin(), downloadable_suffixes.end(), suffix) != downloadable_suffixes.end()) { a_node.append_attribute("download") = file.c_str(); } // Optionally add the file size. - string path = filter_url_create_path ({directory, file}); + const std::string path = filter_url_create_path ({directory, file}); if (!filter_url_is_dir (path)) { td_node = tr_node.append_child("td"); td_node.text().set(filter::strings::convert_to_string (filter_url_filesize (path)).c_str()); } } } - stringstream ss; - listing_document.print (ss, "", format_raw); + std::stringstream ss {}; + listing_document.print (ss, "", pugi::format_raw); view.set_variable ("listing", ss.str()); } else { - string filename = filter_url_create_root_path ({url}); + const std::string filename = filter_url_create_root_path ({url}); return filter_url_file_get_contents (filename); } page += view.render ("index", "listing"); diff --git a/index/listing.h b/index/listing.h index b97872160..94542925c 100644 --- a/index/listing.h +++ b/index/listing.h @@ -23,6 +23,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. class Webserver_Request; -std::string index_listing_url (std::string url); +std::string index_listing_url (const std::string& url); bool index_listing_acl (Webserver_Request& webserver_request, std::string url); std::string index_listing (Webserver_Request& webserver_request, std::string url);