2020-12-28 23:17:30 +01:00
|
|
|
#include "paged_article.h"
|
|
|
|
|
2021-05-15 13:54:28 +02:00
|
|
|
#include "core/database/query_builder.h"
|
|
|
|
#include "core/database/query_result.h"
|
|
|
|
#include "core/database/table_builder.h"
|
2020-12-28 23:17:30 +01:00
|
|
|
#include "core/utils.h"
|
|
|
|
|
|
|
|
#include <tinydir/tinydir.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
|
|
|
|
2020-12-28 23:17:30 +01:00
|
|
|
tinydir_dir dir;
|
2022-02-04 22:05:51 +01:00
|
|
|
ERR_FAIL_COND_MSG(tinydir_open(&dir, articles_folder.c_str()) == -1, "Error opening PagedArticle::folder! folder: " + articles_folder);
|
2020-12-28 23:17:30 +01:00
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
Vector<String> files;
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
while (dir.has_next) {
|
|
|
|
tinydir_file file;
|
|
|
|
if (tinydir_readfile(&dir, &file) == -1) {
|
|
|
|
tinydir_next(&dir);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file.is_dir) {
|
2021-11-01 19:53:35 +01:00
|
|
|
String np = file.name;
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
files.push_back(np);
|
|
|
|
}
|
|
|
|
|
|
|
|
tinydir_next(&dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
tinydir_close(&dir);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
FILE *f = fopen(file_path.c_str(), "r");
|
|
|
|
|
2022-02-04 22:05:51 +01:00
|
|
|
ERR_CONTINUE_MSG(!f, "PagedArticle::load_folder: Error opening file! " + file_path);
|
2020-12-28 23:17:30 +01:00
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
long fsize = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
String fd;
|
2020-12-28 23:17:30 +01:00
|
|
|
fd.resize(fsize);
|
|
|
|
|
|
|
|
fread(&fd[0], 1, fsize, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
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
|
|
|
}
|