2020-11-25 00:20:41 +01:00
|
|
|
#include "file_cache.h"
|
|
|
|
|
|
|
|
#include <tinydir/tinydir.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
void FileCache::wwwroot_register_file(const String &file_path) {
|
2020-11-25 00:20:41 +01:00
|
|
|
registered_files.insert(file_path);
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
void FileCache::wwwroot_deregister_file(const String &file_path) {
|
2020-11-25 00:20:41 +01:00
|
|
|
registered_files.erase(file_path);
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
bool FileCache::wwwroot_has_file(const String &file_path) {
|
2020-11-25 13:22:43 +01:00
|
|
|
return registered_files.find(file_path) != registered_files.end();
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 14:53:53 +01:00
|
|
|
void FileCache::wwwroot_refresh_cache() {
|
2020-11-25 00:20:41 +01:00
|
|
|
registered_files.clear();
|
|
|
|
|
2020-11-25 14:53:53 +01:00
|
|
|
wwwroot_evaluate_dir(wwwroot.c_str());
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 03:57:18 +01:00
|
|
|
void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist) {
|
2020-11-25 00:20:41 +01:00
|
|
|
tinydir_dir dir;
|
|
|
|
if (tinydir_open(&dir, path) == -1) {
|
2021-01-06 03:57:18 +01:00
|
|
|
|
|
|
|
if (should_exist)
|
2021-02-08 11:53:04 +01:00
|
|
|
printf("Error opening wwwroot! folder: %s\n", path);
|
2021-01-06 03:57:18 +01:00
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.path;
|
2020-11-25 00:20:41 +01:00
|
|
|
np = np.substr(wwwroot.size(), np.size() - wwwroot.size());
|
|
|
|
|
|
|
|
registered_files.insert(np);
|
|
|
|
} else {
|
|
|
|
if (file.name[0] == '.' && file.name[1] == '\0' || file.name[0] == '.' && file.name[1] == '.') {
|
|
|
|
tinydir_next(&dir);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-25 14:53:53 +01:00
|
|
|
wwwroot_evaluate_dir(file.path);
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tinydir_next(&dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
tinydir_close(&dir);
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
bool FileCache::get_cached_body(const String &path, String *body) {
|
2020-11-25 14:53:53 +01:00
|
|
|
//TODO ERROR MACRO body == null
|
|
|
|
|
|
|
|
//this shouldn't need mutexes
|
|
|
|
|
|
|
|
CacheEntry *e = cache_map[path];
|
|
|
|
|
|
|
|
if (!e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t current_timestamp = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
|
|
|
|
|
|
|
int64_t diff = current_timestamp - e->timestamp;
|
|
|
|
|
|
|
|
if (diff > cache_invalidation_time) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
body->append_str(e->body);
|
2020-11-25 14:53:53 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-01 19:53:35 +01:00
|
|
|
void FileCache::set_cached_body(const String &path, const String &body) {
|
2020-11-25 14:53:53 +01:00
|
|
|
cache_mutex.lock();
|
|
|
|
|
|
|
|
CacheEntry *e = cache_map[path];
|
|
|
|
|
|
|
|
if (!e) {
|
|
|
|
e = new CacheEntry();
|
|
|
|
cache_map[path] = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t current_timestamp = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
|
|
|
|
|
|
|
e->timestamp = current_timestamp;
|
|
|
|
e->body = body;
|
|
|
|
|
|
|
|
cache_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileCache::FileCache(bool singleton) {
|
|
|
|
if (singleton) {
|
|
|
|
if (_instance) {
|
|
|
|
printf("FileCache: Filecache instance is set as singleton, but an another FileCache instance is already set up as singleton! Ignoring setting!\n");
|
|
|
|
} else {
|
|
|
|
_instance = this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cache_invalidation_time = 1;
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileCache::~FileCache() {
|
|
|
|
registered_files.clear();
|
|
|
|
|
2020-11-25 14:53:53 +01:00
|
|
|
if (_instance == this)
|
|
|
|
_instance = nullptr;
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 14:53:53 +01:00
|
|
|
FileCache *FileCache::get_singleton() {
|
2020-11-25 00:20:41 +01:00
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileCache *FileCache::_instance = nullptr;
|