mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-10 00:52:11 +01:00
Added file support to the paged article. Will not work well with big files at the moment.
This commit is contained in:
parent
5ea0559839
commit
8e429cee7d
@ -22,10 +22,13 @@ void FileCache::wwwroot_refresh_cache() {
|
||||
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;
|
||||
if (tinydir_open(&dir, path) == -1) {
|
||||
printf("Error opening wwwroot!\n");
|
||||
|
||||
if (should_exist)
|
||||
printf("Error opening wwwroot!\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ public:
|
||||
void wwwroot_deregister_file(const std::string &file_path);
|
||||
bool wwwroot_has_file(const std::string &file_path);
|
||||
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);
|
||||
void set_cached_body(const std::string &path, const std::string &body);
|
||||
|
@ -24,6 +24,39 @@ void PagedArticle::index(Request *request) {
|
||||
|
||||
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 == "") {
|
||||
//summary page
|
||||
request->body += s->summary_page;
|
||||
@ -78,7 +111,9 @@ void PagedArticle::load() {
|
||||
std::string np = file.path;
|
||||
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) {
|
||||
|
||||
@ -87,6 +122,9 @@ void PagedArticle::load() {
|
||||
a->url = p;
|
||||
pages[p] = a;
|
||||
}
|
||||
|
||||
a->file_cache->wwwroot = ("." + ff + "/files");
|
||||
a->file_cache->wwwroot_refresh_cache();
|
||||
}
|
||||
|
||||
tinydir_next(&dir);
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "core/file_cache.h"
|
||||
#include "core/object.h"
|
||||
|
||||
#include "core/request.h"
|
||||
@ -13,14 +14,19 @@ struct Article {
|
||||
std::string url;
|
||||
std::string summary_page;
|
||||
std::map<std::string, std::string *> pages;
|
||||
FileCache *file_cache;
|
||||
|
||||
Article() {}
|
||||
Article() {
|
||||
file_cache = new FileCache();
|
||||
}
|
||||
~Article() {
|
||||
for (std::map<std::string, std::string *>::iterator it = pages.begin(); it != pages.end(); ++it) {
|
||||
delete ((*it).second);
|
||||
}
|
||||
|
||||
pages.clear();
|
||||
|
||||
delete file_cache;
|
||||
}
|
||||
};
|
||||
|
||||
@ -40,7 +46,7 @@ public:
|
||||
|
||||
std::map<std::string, Article *> pages;
|
||||
std::string folder;
|
||||
std::string base_path;
|
||||
std::string base_path;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user