diff --git a/core/file_cache.cpp b/core/file_cache.cpp index 15cea3e..f0629e3 100644 --- a/core/file_cache.cpp +++ b/core/file_cache.cpp @@ -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; } diff --git a/core/file_cache.h b/core/file_cache.h index 2678eec..4f57740 100644 --- a/core/file_cache.h +++ b/core/file_cache.h @@ -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); diff --git a/modules/paged_article/paged_article.cpp b/modules/paged_article/paged_article.cpp index 7a56ffa..96dd95f 100644 --- a/modules/paged_article/paged_article.cpp +++ b/modules/paged_article/paged_article.cpp @@ -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); diff --git a/modules/paged_article/paged_article.h b/modules/paged_article/paged_article.h index f4d9db4..e0a9fd1 100644 --- a/modules/paged_article/paged_article.h +++ b/modules/paged_article/paged_article.h @@ -5,6 +5,7 @@ #include #include +#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 pages; + FileCache *file_cache; - Article() {} + Article() { + file_cache = new FileCache(); + } ~Article() { for (std::map::iterator it = pages.begin(); it != pages.end(); ++it) { delete ((*it).second); } pages.clear(); + + delete file_cache; } }; @@ -40,7 +46,7 @@ public: std::map pages; std::string folder; - std::string base_path; + std::string base_path; }; #endif \ No newline at end of file