diff --git a/config.ini b/config.ini index c020ec0..d55cae3 100644 --- a/config.ini +++ b/config.ini @@ -1,4 +1,12 @@ [server] -destination_id = undefined +default_destination = default ; url = https://screenuploader.bakatrouble.me/upload// ; do not change previous line unless you setup your own server! + +[title_settings] +; example: use destination "discord" for title ID DB1426D1DFD034027CECDE9C2DD914B8 (Album) +; title ID is a second part of capture filename +; DB1426D1DFD034027CECDE9C2DD914B8 = discord + +[destinations] +default = 2574edc6-f3ac-4f49-8d8e-35657ac0fce4 diff --git a/include/inih.h b/include/inih.h index 52adf43..30a2791 100644 --- a/include/inih.h +++ b/include/inih.h @@ -330,6 +330,9 @@ class INIReader // Return the list of sections found in ini file const std::set& Sections() const; + // Return the list of fields for a section found in ini file + const std::set& Fields(std::string section) const; + // Get a string value from INI file, returning default_value if not found. std::string Get(std::string section, std::string name, std::string default_value) const; @@ -357,7 +360,9 @@ class INIReader int _error; std::map _values; std::set _sections; + std::map> _sectionValues; static std::string MakeKey(std::string section, std::string name); + static std::string MakeKey(std::string section); static int ValueHandler(void* user, const char* section, const char* name, const char* value); }; @@ -392,6 +397,11 @@ inline const std::set& INIReader::Sections() const return _sections; } +inline const std::set& INIReader::Fields(std::string section) const +{ + return _sectionValues.at(MakeKey(section)); +} + inline std::string INIReader::Get(std::string section, std::string name, std::string default_value) const { std::string key = MakeKey(section, name); @@ -447,6 +457,14 @@ inline std::string INIReader::MakeKey(std::string section, std::string name) return key; } +inline std::string INIReader::MakeKey(std::string section) +{ + std::string key = section; + // Convert to lower case to make section lookups case-insensitive + std::transform(key.begin(), key.end(), key.begin(), ::tolower); + return key; +} + inline int INIReader::ValueHandler(void* user, const char* section, const char* name, const char* value) { @@ -456,6 +474,7 @@ inline int INIReader::ValueHandler(void* user, const char* section, const char* reader->_values[key] += "\n"; reader->_values[key] += value; reader->_sections.insert(section); + reader->_sectionValues[MakeKey(section)].insert(name); return 1; } diff --git a/src/config.cpp b/src/config.cpp index b8a62ea..15e5fbf 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "config.hpp" using namespace std; @@ -18,16 +19,41 @@ bool Config::refresh() { return false; } - string destid = reader.Get("server", "destination_id", "undefined"); - string url = reader.Get("server", "url", "https://screenuploader.bakatrouble.me/upload/" + URLplaceholder + "/"); - if (url.find(URLplaceholder) != string::npos) { - url.replace(url.find(URLplaceholder), URLplaceholder.length(), destid); + m_defaultDestID = reader.Get("server", "destination_id", "undefined"); + + if (reader.Sections().count("destinations") > 0) { + map destinations; + for (auto &destName : reader.Fields("destinations")) { + destinations[destName] = reader.Get("destinations", destName, m_defaultDestID); + } + + if (reader.Sections().count("title_settings") > 0) { + for (auto &tid : reader.Fields("title_settings")) { + string destName = reader.Get("title_settings", tid, ";"); // ";" is guaranteed to not be in a field name? + m_titleSettings[tid] = !destinations[destName].empty() ? destinations[destName] : m_defaultDestID; + } + } + + string defaultDestName = reader.Get("server", "default_destination", ";"); + if (!destinations[defaultDestName].empty()) { + m_defaultDestID = destinations[defaultDestName]; + } } - m_url = url; + + m_url = reader.Get("server", "url", "https://screenuploader.bakatrouble.me/upload/" + URLplaceholder + "/"); return true; } -string Config::getUrl() { - return m_url; +string Config::getUrl(string &tid) { + string destID, + url = m_url; + if (!m_titleSettings[tid].empty()) + destID = m_titleSettings[tid]; + else + destID = m_defaultDestID; + if (url.find(URLplaceholder) != string::npos) { + url.replace(url.find(URLplaceholder), URLplaceholder.length(), destID); + } + return url; } diff --git a/src/config.hpp b/src/config.hpp index e53bd5e..fcfb071 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -2,6 +2,7 @@ #include #include +#include using namespace std; @@ -13,8 +14,10 @@ class Config { static Config load(); bool refresh(); - string getUrl(); + string getUrl(string &tid); protected: string m_url = "https://screenuploader.bakatrouble.me/upload/" + URLplaceholder + "/"; + string m_defaultDestID; + map m_titleSettings; }; diff --git a/src/upload.cpp b/src/upload.cpp index 0961d8e..1c7c1d8 100644 --- a/src/upload.cpp +++ b/src/upload.cpp @@ -40,10 +40,14 @@ bool sendFileToServer(Config &conf, string &path, size_t size) { struct upload_info ui = { f, size }; + string tid = path.substr(path.length() - 36, 32); + cout << "Title ID: " << tid << endl; + CURL *curl = curl_easy_init(); if (curl) { stringstream url; - url << conf.getUrl() << "?filename=" << fpath.filename().string(); + url << conf.getUrl(tid) << "?filename=" << fpath.filename().string(); + cout << "Upload URL: " << url.str() << endl; curl_easy_setopt(curl, CURLOPT_URL, url.str().c_str()); curl_easy_setopt(curl, CURLOPT_POST, 1L); struct curl_slist *chunk = nullptr;