Added file support to the paged article. Will not work well with big files at the moment.

This commit is contained in:
Relintai 2021-01-06 03:57:18 +01:00
parent 5ea0559839
commit 8e429cee7d
4 changed files with 53 additions and 6 deletions

View File

@ -22,10 +22,13 @@ void FileCache::wwwroot_refresh_cache() {
wwwroot_evaluate_dir(wwwroot.c_str()); wwwroot_evaluate_dir(wwwroot.c_str());
} }
void FileCache::wwwroot_evaluate_dir(const char *path) { void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist) {
tinydir_dir dir; tinydir_dir dir;
if (tinydir_open(&dir, path) == -1) { if (tinydir_open(&dir, path) == -1) {
if (should_exist)
printf("Error opening wwwroot!\n"); printf("Error opening wwwroot!\n");
return; return;
} }

View File

@ -18,7 +18,7 @@ public:
void wwwroot_deregister_file(const std::string &file_path); void wwwroot_deregister_file(const std::string &file_path);
bool wwwroot_has_file(const std::string &file_path); bool wwwroot_has_file(const std::string &file_path);
void wwwroot_refresh_cache(); void wwwroot_refresh_cache();
void wwwroot_evaluate_dir(const char *path); void wwwroot_evaluate_dir(const char *path, const bool should_exist = true);
bool get_cached_body(const std::string &path, std::string *body); bool get_cached_body(const std::string &path, std::string *body);
void set_cached_body(const std::string &path, const std::string &body); void set_cached_body(const std::string &path, const std::string &body);

View File

@ -24,6 +24,39 @@ void PagedArticle::index(Request *request) {
const std::string rp = request->get_current_path_segment(); const std::string rp = request->get_current_path_segment();
if (request->get_remaining_segment_count() > 1 && rp == "files") {
std::string file_name = "/" + request->get_path_segment(request->get_current_segment_index() + 1);
if (s->file_cache->wwwroot_has_file(file_name)) {
std::string fp = s->file_cache->wwwroot + file_name;
FILE *f = fopen(fp.c_str(), "rb");
if (!f) {
printf("Error: Registered file doesn't exists anymore! %s\n", fp.c_str());
Application::get_instance()->send_error(404, request);
return;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
std::string body;
body.resize(fsize);
fread(&body[0], 1, fsize, f);
fclose(f);
//TODO set mimetype?
request->response->setBody(body);
request->send();
return;
}
}
if (rp == "") { if (rp == "") {
//summary page //summary page
request->body += s->summary_page; request->body += s->summary_page;
@ -78,7 +111,9 @@ void PagedArticle::load() {
std::string np = file.path; std::string np = file.path;
std::string fn = file.name; std::string fn = file.name;
Article *a = load_folder(np, base_path + "/" + fn); std::string ff = base_path + "/" + fn;
Article *a = load_folder(np, ff);
if (a) { if (a) {
@ -87,6 +122,9 @@ void PagedArticle::load() {
a->url = p; a->url = p;
pages[p] = a; pages[p] = a;
} }
a->file_cache->wwwroot = ("." + ff + "/files");
a->file_cache->wwwroot_refresh_cache();
} }
tinydir_next(&dir); tinydir_next(&dir);

View File

@ -5,6 +5,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "core/file_cache.h"
#include "core/object.h" #include "core/object.h"
#include "core/request.h" #include "core/request.h"
@ -13,14 +14,19 @@ struct Article {
std::string url; std::string url;
std::string summary_page; std::string summary_page;
std::map<std::string, std::string *> pages; std::map<std::string, std::string *> pages;
FileCache *file_cache;
Article() {} Article() {
file_cache = new FileCache();
}
~Article() { ~Article() {
for (std::map<std::string, std::string *>::iterator it = pages.begin(); it != pages.end(); ++it) { for (std::map<std::string, std::string *>::iterator it = pages.begin(); it != pages.end(); ++it) {
delete ((*it).second); delete ((*it).second);
} }
pages.clear(); pages.clear();
delete file_cache;
} }
}; };