From 1f679124bba936e7905c7f4c83186d0c961dca61 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 1 Jan 2023 03:35:15 +0100 Subject: Switch to SQLite storage --- whiteboard.cpp | 55 +++++++++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/whiteboard.cpp b/whiteboard.cpp index 6d07576..2718937 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -7,12 +7,15 @@ #include +#include #include #include #include +#include #include #include #include +#include #include #include @@ -67,33 +70,6 @@ namespace { return "endofcodes"; } - void setFileById(const std::string& data, const std::string& id) - { - fs::path path{data_path / id}; - - // skip if it doesn't exists - if (data.empty() && !fs::exists(path)) - return; - - // clean up immediately, if appropriate - if (data.empty()) { - fs::remove(path); - return; - } - - File::setFile(path, data); - } - - std::string getFileById(const std::string& id) - { - fs::path path{data_path / id}; - - if (!fs::exists(path)) - return {}; - - return File::getFile(path); - } - void usage() { std::cout << "Usage: \n" @@ -105,6 +81,20 @@ namespace { "Without options, whiteboard will be started as FCGI application" << std::endl; } + + std::mutex storage_mutex; + + void storage_cleanup(Storage& storage) + { + while(true) { + { + std::lock_guard lock(storage_mutex); + storage.cleanup(); + } + std::this_thread::sleep_for(std::chrono::minutes(10)); + } + } + } // namespace // the actual main() for testability @@ -125,6 +115,8 @@ int whiteboard(int argc, char* argv[]) } } + std::thread storage_cleanup_thread(storage_cleanup, std::ref(storage)); + Magick::InitializeMagick(NULL); // for qrcode.cpp int result = FCGX_Init(); @@ -148,6 +140,7 @@ int whiteboard(int argc, char* argv[]) while (FCGX_Accept_r(&request) >= 0) { try { + std::lock_guard lock(storage_mutex); char* method = FCGX_GetParam("REQUEST_METHOD", request.envp); // POST for server actions, changes @@ -171,12 +164,12 @@ int whiteboard(int argc, char* argv[]) if (command == "modify") { std::string id {xml.get("request.id")}; std::string data {xml.get("request.data")}; - setFileById(data, id); + storage.setDocument(id, data); FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); } else if (command == "getfile") { std::string id {xml.get("request.id")}; - std::string filedata {getFileById(id)}; + std::string filedata {storage.getDocument(id)}; if (filedata.size() > 30000000) throw std::runtime_error("File too big"); @@ -192,7 +185,7 @@ int whiteboard(int argc, char* argv[]) //std::cout << "Checksum JS: " << checksum_s << std::endl; //std::cout << "Checksum C++: " << checksum32(filedata) << std::endl; - std::string filedata {getFileById(id)}; + std::string filedata {storage.getDocument(id)}; if (checksum != checksum32(filedata)) { //std::cout << "Sending change..." << std::endl; FCGX_PutS("Content-Type: application/octet-stream\r\n", request.out); @@ -235,6 +228,8 @@ int whiteboard(int argc, char* argv[]) } } + storage_cleanup_thread.join(); + return 0; } -- cgit v1.2.3