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"
|
2022-02-05 15:30:48 +01:00
|
|
|
#include "web/html/utils.h"
|
2022-02-10 16:17:31 +01:00
|
|
|
#include "web/http/web_permission.h"
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2022-01-08 12:53:50 +01:00
|
|
|
void PagedArticle::handle_request_main(Request *request) {
|
2022-02-10 16:17:31 +01:00
|
|
|
if (_web_permission.is_valid()) {
|
|
|
|
if (_web_permission->activate(request)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05 03:00:40 +01:00
|
|
|
render_menu(request);
|
|
|
|
|
2022-02-14 23:27:26 +01:00
|
|
|
render_index(request);
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
request->compile_and_send_body();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-02-05 03:00:40 +01:00
|
|
|
render_menu(request);
|
2020-12-28 23:17:30 +01:00
|
|
|
request->body += (*page);
|
|
|
|
request->compile_and_send_body();
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:27:26 +01:00
|
|
|
void PagedArticle::render_index(Request *request) {
|
2022-02-14 23:20:47 +01:00
|
|
|
// summary page
|
|
|
|
request->body += index_page;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedArticle::render_preview(Request *request) {
|
|
|
|
}
|
|
|
|
|
2020-12-28 23:17:30 +01:00
|
|
|
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-05 02:45:11 +01:00
|
|
|
ERR_FAIL_COND_MSG(dir->open_dir(articles_folder.c_str()) != OK, "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-05 13:56:30 +01:00
|
|
|
while (dir->next()) {
|
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;
|
2022-02-05 14:53:59 +01:00
|
|
|
file_path.append_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-05 02:45:11 +01:00
|
|
|
ERR_CONTINUE_MSG(dir->read_file_into(file_path, &fd) != OK, "PagedArticle::load_folder: Error opening file! " + file_path);
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
Utils::markdown_to_html(&fd);
|
|
|
|
|
2022-02-05 13:27:05 +01:00
|
|
|
if (files[i] == "summary.md") {
|
|
|
|
summary = fd;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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-05 13:19:34 +01:00
|
|
|
index_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 != "") {
|
2022-02-04 23:39:45 +01:00
|
|
|
if (serve_folder_relative) {
|
2022-02-05 14:53:59 +01:00
|
|
|
file_cache->wwwroot = articles_folder;
|
|
|
|
file_cache->wwwroot.append_path(serve_folder);
|
2022-02-04 23:39:45 +01:00
|
|
|
} else {
|
|
|
|
file_cache->wwwroot = serve_folder;
|
|
|
|
}
|
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
file_cache->wwwroot_refresh_cache();
|
2020-12-28 23:17:30 +01:00
|
|
|
}
|
2022-02-05 13:27:05 +01:00
|
|
|
|
|
|
|
if (summary == "") {
|
|
|
|
generate_summary();
|
|
|
|
}
|
2020-12-28 23:17:30 +01:00
|
|
|
}
|
|
|
|
|
2022-02-05 13:19:34 +01:00
|
|
|
String PagedArticle::get_index_page() {
|
|
|
|
return index_page;
|
2022-02-05 02:45:11 +01:00
|
|
|
}
|
|
|
|
|
2022-02-05 13:27:05 +01:00
|
|
|
String PagedArticle::get_summary() {
|
|
|
|
return summary;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedArticle::generate_summary() {
|
|
|
|
summary = get_uri_segment();
|
|
|
|
}
|
|
|
|
|
2022-02-05 02:45:11 +01:00
|
|
|
void PagedArticle::_notification(const int what) {
|
|
|
|
switch (what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE:
|
|
|
|
load();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2022-02-04 23:39:45 +01:00
|
|
|
|
|
|
|
serve_folder_relative = true;
|
|
|
|
serve_folder = "files";
|
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
|
|
|
}
|