mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-05-19 06:08:22 +02:00
After thinking about it, reworked PagedArticle. Before it would evaluate and store data about all directories in the given path. Now it will only process one. The evaluate all directories functionality will be added to a new node.
This commit is contained in:
parent
15c384750f
commit
4b9266c1f3
@ -9,24 +9,13 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void PagedArticle::handle_request_main(Request *request) {
|
void PagedArticle::handle_request_main(Request *request) {
|
||||||
const String r = request->get_current_path_segment();
|
const String &rp = request->get_current_path_segment();
|
||||||
|
|
||||||
Ref<PagedArticleEntry> s = pages[r];
|
|
||||||
|
|
||||||
if (!s.is_valid()) {
|
|
||||||
request->send_error(404);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
request->push_path();
|
|
||||||
|
|
||||||
const String rp = request->get_current_path_segment();
|
|
||||||
|
|
||||||
if (request->get_remaining_segment_count() > 1 && rp == "files") {
|
if (request->get_remaining_segment_count() > 1 && rp == "files") {
|
||||||
String file_name = "/" + request->get_path_segment(request->get_current_segment_index() + 1);
|
String file_name = "/" + request->get_path_segment(request->get_current_segment_index() + 1);
|
||||||
|
|
||||||
if (s->file_cache->wwwroot_has_file(file_name)) {
|
if (file_cache->wwwroot_has_file(file_name)) {
|
||||||
String fp = s->file_cache->wwwroot + file_name;
|
String fp = file_cache->wwwroot + file_name;
|
||||||
|
|
||||||
request->send_file(fp);
|
request->send_file(fp);
|
||||||
return;
|
return;
|
||||||
@ -35,14 +24,14 @@ void PagedArticle::handle_request_main(Request *request) {
|
|||||||
|
|
||||||
if (rp == "") {
|
if (rp == "") {
|
||||||
// summary page
|
// summary page
|
||||||
request->body += s->summary_page;
|
request->body += summary_page;
|
||||||
|
|
||||||
request->compile_and_send_body();
|
request->compile_and_send_body();
|
||||||
request->pop_path();
|
request->pop_path();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const String *page = s->pages[rp];
|
const String *page = pages[rp];
|
||||||
|
|
||||||
if (page == nullptr) {
|
if (page == nullptr) {
|
||||||
// bad url
|
// bad url
|
||||||
@ -56,77 +45,13 @@ void PagedArticle::handle_request_main(Request *request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PagedArticle::load() {
|
void PagedArticle::load() {
|
||||||
if (folder == "") {
|
ERR_FAIL_COND_MSG(articles_folder == "", "Error: PagedArticle::load called, but a articles_folder is not set!");
|
||||||
printf("Error: PagedArticle::load called, but a folder is not set!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (base_path.size() > 0 && base_path[base_path.size() - 1] == '/') {
|
|
||||||
base_path.pop_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (folder.size() > 0 && folder[folder.size() - 1] == '/') {
|
|
||||||
folder.pop_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
tinydir_dir dir;
|
tinydir_dir dir;
|
||||||
if (tinydir_open(&dir, folder.c_str()) == -1) {
|
ERR_FAIL_COND_MSG(tinydir_open(&dir, articles_folder.c_str()) == -1, "Error opening PagedArticle::folder! folder: " + articles_folder);
|
||||||
printf("Error opening PagedArticle::folder! folder: %s\n", folder.c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (dir.has_next) {
|
|
||||||
tinydir_file file;
|
|
||||||
if (tinydir_readfile(&dir, &file) == -1) {
|
|
||||||
tinydir_next(&dir);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file.is_dir) {
|
|
||||||
if (file.name[0] == '.' || (file.name[0] == '.' && file.name[1] == '.')) {
|
|
||||||
tinydir_next(&dir);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String np = file.path;
|
|
||||||
String fn = file.name;
|
|
||||||
|
|
||||||
String ff = folder + "/" + fn;
|
|
||||||
String wp = base_path + "/" + fn;
|
|
||||||
|
|
||||||
Ref<PagedArticleEntry> a = load_folder(np, wp);
|
|
||||||
|
|
||||||
if (a.is_valid()) {
|
|
||||||
|
|
||||||
String p = file.name;
|
|
||||||
|
|
||||||
a->url = p;
|
|
||||||
pages[p] = a;
|
|
||||||
}
|
|
||||||
|
|
||||||
a->file_cache->wwwroot = (ff + "/files");
|
|
||||||
a->file_cache->wwwroot_refresh_cache();
|
|
||||||
}
|
|
||||||
|
|
||||||
tinydir_next(&dir);
|
|
||||||
}
|
|
||||||
|
|
||||||
tinydir_close(&dir);
|
|
||||||
|
|
||||||
generate_summaries();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ref<PagedArticleEntry> PagedArticle::load_folder(const String &folder, const String &path) {
|
|
||||||
printf("PagedArticle: loading: %s\n", folder.c_str());
|
|
||||||
|
|
||||||
Vector<String> files;
|
Vector<String> files;
|
||||||
|
|
||||||
tinydir_dir dir;
|
|
||||||
if (tinydir_open(&dir, folder.c_str()) == -1) {
|
|
||||||
printf("PagedArticle::load_folder: Error opening folder %s!\n", folder.c_str());
|
|
||||||
return Ref<PagedArticleEntry>();
|
|
||||||
}
|
|
||||||
|
|
||||||
while (dir.has_next) {
|
while (dir.has_next) {
|
||||||
tinydir_file file;
|
tinydir_file file;
|
||||||
if (tinydir_readfile(&dir, &file) == -1) {
|
if (tinydir_readfile(&dir, &file) == -1) {
|
||||||
@ -146,29 +71,23 @@ Ref<PagedArticleEntry> PagedArticle::load_folder(const String &folder, const Str
|
|||||||
tinydir_close(&dir);
|
tinydir_close(&dir);
|
||||||
|
|
||||||
if (files.size() == 0) {
|
if (files.size() == 0) {
|
||||||
return Ref<PagedArticleEntry>();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//todo
|
files.sort_inc();
|
||||||
//std::sort(files.begin(), files.end());
|
|
||||||
|
|
||||||
Ref<PagedArticleEntry> article;
|
|
||||||
article.instance();
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < files.size(); ++i) {
|
for (uint32_t i = 0; i < files.size(); ++i) {
|
||||||
String file_path = folder;
|
String file_path = articles_folder;
|
||||||
|
|
||||||
if (file_path.size() > 0 && file_path[file_path.size() - 1] != '/')
|
if (file_path.size() > 0 && file_path[file_path.size() - 1] != '/') {
|
||||||
file_path += "/";
|
file_path += "/";
|
||||||
|
}
|
||||||
|
|
||||||
file_path += files[i].c_str();
|
file_path += files[i];
|
||||||
|
|
||||||
FILE *f = fopen(file_path.c_str(), "r");
|
FILE *f = fopen(file_path.c_str(), "r");
|
||||||
|
|
||||||
if (!f) {
|
ERR_CONTINUE_MSG(!f, "PagedArticle::load_folder: Error opening file! " + file_path);
|
||||||
printf("PagedArticle::load_folder: Error opening file! %s\n", file_path.c_str());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
long fsize = ftell(f);
|
long fsize = ftell(f);
|
||||||
@ -184,7 +103,7 @@ Ref<PagedArticleEntry> PagedArticle::load_folder(const String &folder, const Str
|
|||||||
|
|
||||||
String pagination;
|
String pagination;
|
||||||
|
|
||||||
pagination = Utils::get_pagination_links(path, files, i);
|
pagination = Utils::get_pagination_links(get_full_uri(), files, i);
|
||||||
|
|
||||||
String *finals = new String();
|
String *finals = new String();
|
||||||
|
|
||||||
@ -192,40 +111,49 @@ Ref<PagedArticleEntry> PagedArticle::load_folder(const String &folder, const Str
|
|||||||
(*finals) += fd;
|
(*finals) += fd;
|
||||||
(*finals) += pagination;
|
(*finals) += pagination;
|
||||||
|
|
||||||
article->pages[files[i]] = finals;
|
pages[files[i]] = finals;
|
||||||
|
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
article->summary_page = (*finals);
|
summary_page = (*finals);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return article;
|
file_cache->clear();
|
||||||
|
|
||||||
|
if (serve_folder != "") {
|
||||||
|
file_cache->wwwroot = serve_folder;
|
||||||
|
file_cache->wwwroot_refresh_cache();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PagedArticle::generate_summaries() {
|
generate_summary();
|
||||||
for (std::map<String, Ref<PagedArticleEntry> >::iterator it = pages.begin(); it != pages.end(); ++it) {
|
|
||||||
generate_summary((*it).second);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PagedArticle::generate_summary(Ref<PagedArticleEntry> article) {
|
void PagedArticle::generate_summary() {
|
||||||
if (article->summary_page != "") {
|
if (summary_page != "") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::map<String, String *>::iterator it = article->pages.begin(); it != article->pages.end(); ++it) {
|
for (std::map<String, String *>::iterator it = pages.begin(); it != pages.end(); ++it) {
|
||||||
String *s = (*it).second;
|
String *s = (*it).second;
|
||||||
|
|
||||||
if (s != nullptr) {
|
if (s != nullptr) {
|
||||||
article->summary_page = (*s);
|
summary_page = (*s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PagedArticle::PagedArticle() :
|
PagedArticle::PagedArticle() :
|
||||||
WebNode() {
|
WebNode() {
|
||||||
|
|
||||||
|
file_cache = new FileCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
PagedArticle::~PagedArticle() {
|
PagedArticle::~PagedArticle() {
|
||||||
pages.clear();
|
for (std::map<String, String *>::iterator it = pages.begin(); it != pages.end(); ++it) {
|
||||||
|
delete ((*it).second);
|
||||||
|
}
|
||||||
|
|
||||||
|
pages.clear();
|
||||||
|
|
||||||
|
delete file_cache;
|
||||||
}
|
}
|
@ -18,16 +18,19 @@ public:
|
|||||||
void handle_request_main(Request *request);
|
void handle_request_main(Request *request);
|
||||||
|
|
||||||
void load();
|
void load();
|
||||||
Ref<PagedArticleEntry> load_folder(const String &folder, const String &path);
|
void load_folder(const String &folder, const String &path);
|
||||||
void generate_summaries();
|
void generate_summary();
|
||||||
void generate_summary(Ref<PagedArticleEntry> article);
|
|
||||||
|
|
||||||
PagedArticle();
|
PagedArticle();
|
||||||
~PagedArticle();
|
~PagedArticle();
|
||||||
|
|
||||||
std::map<String, Ref<PagedArticleEntry>> pages;
|
String articles_folder;
|
||||||
String folder;
|
String serve_folder;
|
||||||
String base_path;
|
|
||||||
|
protected:
|
||||||
|
String summary_page;
|
||||||
|
std::map<String, String *> pages;
|
||||||
|
FileCache *file_cache;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,15 +0,0 @@
|
|||||||
#include "paged_article_entry.h"
|
|
||||||
|
|
||||||
PagedArticleEntry::PagedArticleEntry() {
|
|
||||||
file_cache = new FileCache();
|
|
||||||
}
|
|
||||||
|
|
||||||
PagedArticleEntry::~PagedArticleEntry() {
|
|
||||||
for (std::map<String, String *>::iterator it = pages.begin(); it != pages.end(); ++it) {
|
|
||||||
delete ((*it).second);
|
|
||||||
}
|
|
||||||
|
|
||||||
pages.clear();
|
|
||||||
|
|
||||||
delete file_cache;
|
|
||||||
}
|
|
@ -1,27 +0,0 @@
|
|||||||
#ifndef PAGED_ARTICLE_ENTRY_H
|
|
||||||
#define PAGED_ARTICLE_ENTRY_H
|
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include "core/containers/vector.h"
|
|
||||||
#include "core/string.h"
|
|
||||||
|
|
||||||
#include "core/file_cache.h"
|
|
||||||
#include "core/http/web_node.h"
|
|
||||||
|
|
||||||
#include "core/http/request.h"
|
|
||||||
|
|
||||||
#include "core/reference.h"
|
|
||||||
|
|
||||||
class PagedArticleEntry : public Reference {
|
|
||||||
RCPP_OBJECT(PagedArticleEntry, Reference);
|
|
||||||
public:
|
|
||||||
String url;
|
|
||||||
String summary_page;
|
|
||||||
std::map<String, String *> pages;
|
|
||||||
FileCache *file_cache;
|
|
||||||
|
|
||||||
PagedArticleEntry();
|
|
||||||
~PagedArticleEntry();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
Loading…
Reference in New Issue
Block a user