Skip to content

Commit

Permalink
configurable on per-title basis upload destinations
Browse files Browse the repository at this point in the history
  • Loading branch information
bakatrouble committed Jan 30, 2020
1 parent 210d30f commit 33ed60f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 10 deletions.
10 changes: 9 additions & 1 deletion config.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[server]
destination_id = undefined
default_destination = default
; url = https://screenuploader.bakatrouble.me/upload/<destid>/
; 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
19 changes: 19 additions & 0 deletions include/inih.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ class INIReader
// Return the list of sections found in ini file
const std::set<std::string>& Sections() const;

// Return the list of fields for a section found in ini file
const std::set<std::string>& 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;
Expand Down Expand Up @@ -357,7 +360,9 @@ class INIReader
int _error;
std::map<std::string, std::string> _values;
std::set<std::string> _sections;
std::map<std::string, std::set<std::string>> _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);
};
Expand Down Expand Up @@ -392,6 +397,11 @@ inline const std::set<std::string>& INIReader::Sections() const
return _sections;
}

inline const std::set<std::string>& 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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand Down
40 changes: 33 additions & 7 deletions src/config.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <inih.h>
#include <set>
#include "config.hpp"

using namespace std;
Expand All @@ -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<string, string> 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;
}
5 changes: 4 additions & 1 deletion src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <string>
#include <switch.h>
#include <map>

using namespace std;

Expand All @@ -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<string, string> m_titleSettings;
};
6 changes: 5 additions & 1 deletion src/upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 33ed60f

Please sign in to comment.