2020-12-28 23:17:30 +01:00
|
|
|
#include "paged_article.h"
|
|
|
|
|
2022-02-04 23:33:54 +01:00
|
|
|
#include "core/os/directory.h"
|
2020-12-28 23:17:30 +01:00
|
|
|
#include "core/utils.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2022-01-08 12:53:50 +01:00
|
|
|
void PagedArticle::handle_request_main(Request *request) {
|
2022-02-04 22:05:51 +01:00
|
|
|
const String &rp = request->get_current_path_segment();
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2021-01-06 03:57:18 +01:00
|
|
|
if (request->get_remaining_segment_count() > 1 && rp == "files") {
|
2021-11-01 19:53:35 +01:00
|
|
|
String file_name = "/" + request->get_path_segment(request->get_current_segment_index() + 1);
|
2021-01-06 03:57:18 +01:00
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
if (file_cache->wwwroot_has_file(file_name)) {
|
|
|
|
String fp = file_cache->wwwroot + file_name;
|
2021-01-06 03:57:18 +01:00
|
|
|
|
2021-01-08 02:55:41 +01:00
|
|
|
request->send_file(fp);
|
2021-01-06 03:57:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 23:17:30 +01:00
|
|
|
if (rp == "") {
|
2022-02-04 22:05:51 +01:00
|
|
|
// summary page
|
|
|
|
request->body += summary_page;
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
request->compile_and_send_body();
|
|
|
|
request->pop_path();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
const String *page = pages[rp];
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
if (page == nullptr) {
|
2022-02-04 22:05:51 +01:00
|
|
|
// bad url
|
2021-04-28 22:50:05 +02:00
|
|
|
request->send_error(404);
|
2020-12-28 23:17:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->body += (*page);
|
|
|
|
request->compile_and_send_body();
|
|
|
|
request->pop_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedArticle::load() {
|
2022-02-04 22:05:51 +01:00
|
|
|
ERR_FAIL_COND_MSG(articles_folder == "", "Error: PagedArticle::load called, but a articles_folder is not set!");
|
2021-02-08 11:53:04 +01:00
|
|
|
|
2022-02-04 23:33:54 +01:00
|
|
|
Ref<Directory> dir;
|
|
|
|
dir.instance();
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2022-02-04 23:36:04 +01:00
|
|
|
ERR_FAIL_COND_MSG(!dir->open_dir(articles_folder.c_str(), false), "Error opening PagedArticle::folder! folder: " + articles_folder);
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2022-02-04 23:33:54 +01:00
|
|
|
Vector<String> files;
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2022-02-04 23:33:54 +01:00
|
|
|
while (dir->has_next()) {
|
|
|
|
dir->next();
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2022-02-04 23:33:54 +01:00
|
|
|
if (dir->current_is_file()) {
|
|
|
|
files.push_back(dir->current_get_name());
|
2020-12-28 23:17:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 23:36:04 +01:00
|
|
|
dir->close_dir();
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
if (files.size() == 0) {
|
2022-02-04 22:05:51 +01:00
|
|
|
return;
|
2020-12-28 23:17:30 +01:00
|
|
|
}
|
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
files.sort_inc();
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < files.size(); ++i) {
|
2022-02-04 22:05:51 +01:00
|
|
|
String file_path = articles_folder;
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
if (file_path.size() > 0 && file_path[file_path.size() - 1] != '/') {
|
2020-12-28 23:17:30 +01:00
|
|
|
file_path += "/";
|
2022-02-04 22:05:51 +01:00
|
|
|
}
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
file_path += files[i];
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
String fd;
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2022-02-04 23:33:54 +01:00
|
|
|
ERR_CONTINUE_MSG(!dir->read_file_into(file_path, &fd), "PagedArticle::load_folder: Error opening file! " + file_path);
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
Utils::markdown_to_html(&fd);
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
String pagination;
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
pagination = Utils::get_pagination_links(get_full_uri(), files, i);
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
String *finals = new String();
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
(*finals) += pagination;
|
|
|
|
(*finals) += fd;
|
|
|
|
(*finals) += pagination;
|
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
pages[files[i]] = finals;
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
if (i == 0) {
|
2022-02-04 22:05:51 +01:00
|
|
|
summary_page = (*finals);
|
2020-12-28 23:17:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
file_cache->clear();
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
if (serve_folder != "") {
|
|
|
|
file_cache->wwwroot = serve_folder;
|
|
|
|
file_cache->wwwroot_refresh_cache();
|
2020-12-28 23:17:30 +01:00
|
|
|
}
|
2022-02-04 22:05:51 +01:00
|
|
|
|
|
|
|
generate_summary();
|
2020-12-28 23:17:30 +01:00
|
|
|
}
|
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
void PagedArticle::generate_summary() {
|
|
|
|
if (summary_page != "") {
|
2020-12-28 23:17:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
for (std::map<String, String *>::iterator it = pages.begin(); it != pages.end(); ++it) {
|
2021-11-01 19:53:35 +01:00
|
|
|
String *s = (*it).second;
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
if (s != nullptr) {
|
2022-02-04 22:05:51 +01:00
|
|
|
summary_page = (*s);
|
2020-12-28 23:17:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PagedArticle::PagedArticle() :
|
2022-01-08 12:53:50 +01:00
|
|
|
WebNode() {
|
2022-02-04 22:05:51 +01:00
|
|
|
|
|
|
|
file_cache = new FileCache();
|
2020-12-28 23:17:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PagedArticle::~PagedArticle() {
|
2022-02-04 22:05:51 +01:00
|
|
|
for (std::map<String, String *>::iterator it = pages.begin(); it != pages.end(); ++it) {
|
|
|
|
delete ((*it).second);
|
|
|
|
}
|
|
|
|
|
2020-12-28 23:17:30 +01:00
|
|
|
pages.clear();
|
2022-02-04 22:05:51 +01:00
|
|
|
|
|
|
|
delete file_cache;
|
2020-12-28 23:17:30 +01:00
|
|
|
}
|