Skip to content

Commit

Permalink
Give download hints to relevant file extensions
Browse files Browse the repository at this point in the history
#920
The Swift macOS app somehow needs those hints to download certain files that Safari would download from itself.
  • Loading branch information
teusbenschop committed Feb 21, 2024
1 parent bb19211 commit a4d869f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
68 changes: 37 additions & 31 deletions index/listing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include <forward_list>
#include <index/listing.h>
#include <assets/view.h>
#include <assets/page.h>
Expand All @@ -36,26 +37,29 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <pugixml.hpp>
#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) {
Expand All @@ -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 <string> files = filter_url_scandir (directory);
std::vector <std::string> 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<std::string_view, 5> 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");
Expand Down
2 changes: 1 addition & 1 deletion index/listing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit a4d869f

Please sign in to comment.