diff --git a/modules/web/file_cache.cpp b/modules/web/file_cache.cpp index cef0c1fa3..a66af7051 100644 --- a/modules/web/file_cache.cpp +++ b/modules/web/file_cache.cpp @@ -1,8 +1,7 @@ #include "file_cache.h" -#include "core/os/directory.h" - -#include +#include "core/os/dir_access.h" +#include "core/os/file_access.h" void FileCache::wwwroot_register_file(const String &file_path) { registered_files.insert(file_path); @@ -13,7 +12,7 @@ void FileCache::wwwroot_deregister_file(const String &file_path) { } bool FileCache::wwwroot_has_file(const String &file_path) { - return registered_files.find(file_path) != registered_files.end(); + return registered_files.has(file_path); } void FileCache::wwwroot_refresh_cache() { @@ -23,30 +22,33 @@ void FileCache::wwwroot_refresh_cache() { wwwroot.path_clean_end_slash(); - wwwroot_evaluate_dir(wwwroot.c_str()); + wwwroot_evaluate_dir(wwwroot); _lock.write_unlock(); } -void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist) { - Ref dir; - dir.instance(); +void FileCache::wwwroot_evaluate_dir(const String &path, const bool should_exist) { + DirAccess *da = DirAccess::open(path); - ERR_FAIL_COND_MSG(dir->open_dir(path) != OK, "Error opening wwwroot! folder: " + String(path)); + ERR_FAIL_COND_MSG(!da, "Error opening wwwroot! folder: " + path); - while (dir->next()) { - if (dir->current_is_file()) { - String np = dir->current_get_path_cstr(); + da->list_dir_begin(); + String f = da->get_next(); + while (f != String()) { + if (!da->current_is_dir()) { + String np = path + "/" + f; np = np.substr(wwwroot.size(), np.size() - wwwroot.size()); - registered_files.insert(np); } else { - wwwroot_evaluate_dir(dir->current_get_path_cstr()); + wwwroot_evaluate_dir(path + "/" + f); } - } - dir->close_dir(); + f = da->get_next(); + } + da->list_dir_end(); + + memdelete(da); } bool FileCache::get_cached_body(const String &path, String *body) { @@ -68,7 +70,7 @@ bool FileCache::get_cached_body(const String &path, String *body) { return false; } - body->append_str(e->body); + body->operator+=(e->body); return true; } @@ -96,40 +98,23 @@ void FileCache::clear() { registered_files.clear(); - for (std::map::iterator E = cache_map.begin(); E != cache_map.end(); E++) { - CacheEntry * ce = E->second; + for (Map::Element *E = cache_map.front(); E; E++) { + CacheEntry *ce = E->get(); if (ce) { delete ce; } } - + cache_map.clear(); _lock.write_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; - } - } - +FileCache::FileCache() { cache_invalidation_time = 1; } FileCache::~FileCache() { registered_files.clear(); - - if (_instance == this) - _instance = nullptr; } - -FileCache *FileCache::get_singleton() { - return _instance; -} - -FileCache *FileCache::_instance = nullptr; diff --git a/modules/web/file_cache.h b/modules/web/file_cache.h index 7009e3c67..424f6726a 100644 --- a/modules/web/file_cache.h +++ b/modules/web/file_cache.h @@ -1,59 +1,51 @@ #ifndef FILE_CACHE_H #define FILE_CACHE_H -#include "core/string.h" +#include "core/map.h" +#include "core/os/os.h" +#include "core/os/rw_lock.h" +#include "core/set.h" +#include "core/ustring.h" -#include -#include -#include -#include "core/threading/rw_lock.h" +#include "core/reference.h" -#include "core/object.h" - - -class FileCache : public Object { - RCPP_OBJECT(FileCache, Object); +class FileCache : public Reference { + GDCLASS(FileCache, Reference); public: - String wwwroot; - int cache_invalidation_time; + String wwwroot; + int cache_invalidation_time; - //Note: file path should be the url you want to access the file with, inculding lead slash - //e.g. http://127.0.0.1/a/b/d.jpg -> /a/b/d.jpg - void wwwroot_register_file(const String &file_path); - void wwwroot_deregister_file(const String &file_path); - bool wwwroot_has_file(const String &file_path); - void wwwroot_refresh_cache(); - void wwwroot_evaluate_dir(const char *path, const bool should_exist = true); + //Note: file path should be the url you want to access the file with, inculding lead slash + //e.g. http://127.0.0.1/a/b/d.jpg -> /a/b/d.jpg + void wwwroot_register_file(const String &file_path); + void wwwroot_deregister_file(const String &file_path); + bool wwwroot_has_file(const String &file_path); + void wwwroot_refresh_cache(); + void wwwroot_evaluate_dir(const String &path, const bool should_exist = true); - bool get_cached_body(const String &path, String *body); - void set_cached_body(const String &path, const String &body); + bool get_cached_body(const String &path, String *body); + void set_cached_body(const String &path, const String &body); - void clear(); + void clear(); - FileCache(bool singleton = false); - virtual ~FileCache(); + FileCache(); + ~FileCache(); - static FileCache *get_singleton(); - - std::set registered_files; + Set registered_files; protected: + struct CacheEntry { + int64_t timestamp; + String body; - struct CacheEntry { - int64_t timestamp; - String body; + CacheEntry() { + timestamp = 0; + } + }; - CacheEntry() { - timestamp = 0; - } - }; - - RWLock _lock; - std::map cache_map; - -private: - static FileCache *_instance; + RWLock _lock; + Map cache_map; }; -#endif \ No newline at end of file +#endif