From 00320f4299ca76ec4b68d41c1ed7de04db8825b9 Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Mon, 20 Jan 2020 10:22:32 +0300 Subject: [PATCH] configurable album path --- config.ini | 5 +++++ src/config.cpp | 23 ++++++++++++++++++++--- src/config.hpp | 10 ++++++++-- src/main.cpp | 9 ++++++--- src/upload.cpp | 5 ++--- src/upload.hpp | 3 ++- src/utils.cpp | 10 +++++++--- src/utils.hpp | 3 ++- 8 files changed, 52 insertions(+), 16 deletions(-) diff --git a/config.ini b/config.ini index b60dd8f..f82906a 100644 --- a/config.ini +++ b/config.ini @@ -1,3 +1,8 @@ [server] destination_id = undefined ; url = https://screenuploader.bakatrouble.me/upload// +; do not change previous line unless you setup your own server! + +[directories] +; album_path = /Nintendo/Album +; uncomment previous line (remove ";" in the beginning) and change to correct path if detected Album directory is wrong diff --git a/src/config.cpp b/src/config.cpp index 35e6848..b033f16 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -5,12 +5,17 @@ using namespace std; Config Config::load() { + Config conf; + conf.refresh(); + return conf; +} +bool Config::refresh() { INIReader reader("sdmc:/config/sys-screenuploader/config.ini"); if (reader.ParseError() != 0) { cout << "Config parse error " << reader.ParseError() << endl; - return Config(); + return false; } string destid = reader.Get("server", "destination_id", "undefined"); @@ -18,7 +23,19 @@ Config Config::load() { if (url.find(URLplaceholder) != string::npos) { url.replace(url.find(URLplaceholder), URLplaceholder.length(), destid); } + m_url = url; - Config conf = { url }; - return conf; + string albumPath = reader.Get("directories", "album_path", ""); + if (albumPath.length() > 0) + m_albumPath = albumPath; + + return true; +} + +string Config::getUrl() { + return m_url; +} + +string Config::getAlbumPath() { + return m_albumPath; } diff --git a/src/config.hpp b/src/config.hpp index 252ca45..10c8caf 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -10,7 +10,13 @@ const string URLplaceholder = ""; class Config { public: - string url = "https://screenuploader.bakatrouble.me/upload/" + URLplaceholder + "/"; - static Config load(); + bool refresh(); + + string getUrl(); + string getAlbumPath(); + +protected: + string m_url = "https://screenuploader.bakatrouble.me/upload/" + URLplaceholder + "/"; + string m_albumPath = ""; }; diff --git a/src/main.cpp b/src/main.cpp index 36942bb..66ab879 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include #include "upload.hpp" #include "utils.hpp" +#include "config.hpp" using namespace std; @@ -110,19 +111,21 @@ int main(int argc, char **argv) { cout << "=============================" << endl << endl << endl; cout << "ScreenUploader is starting..." << endl; - string tmpItem, lastItem = getLastAlbumItem(); + Config conf = Config::load(); + string tmpItem, lastItem = getLastAlbumItem(conf); cout << "Current last item: " << lastItem << endl; size_t fs; while (true) { - tmpItem = getLastAlbumItem(); + conf.refresh(); + tmpItem = getLastAlbumItem(conf); if (lastItem != tmpItem) { fs = filesize(tmpItem); if (fs > 0) { cout << "=============================" << endl; cout << "New item found: " << tmpItem << endl; cout << "Filesize: " << fs << endl; - if (sendFileToServer(tmpItem, fs)) + if (sendFileToServer(conf, tmpItem, fs)) lastItem = tmpItem; } } diff --git a/src/upload.cpp b/src/upload.cpp index 50d45dc..059a1de 100644 --- a/src/upload.cpp +++ b/src/upload.cpp @@ -28,8 +28,7 @@ static size_t _uploadReadFunction(void *ptr, size_t size, size_t nmemb, void *da return 0; } -bool sendFileToServer(string &path, size_t size) { - Config conf = Config::load(); +bool sendFileToServer(Config &conf, string &path, size_t size) { fs::path fpath(path); FILE *f = fopen(path.c_str(), "rb"); @@ -38,7 +37,7 @@ bool sendFileToServer(string &path, size_t size) { CURL *curl = curl_easy_init(); if (curl) { stringstream url; - url << conf.url << "?filename=" << fpath.filename().string(); + url << conf.getUrl() << "?filename=" << fpath.filename().string(); curl_easy_setopt(curl, CURLOPT_URL, url.str().c_str()); curl_easy_setopt(curl, CURLOPT_POST, 1L); struct curl_slist *chunk = nullptr; diff --git a/src/upload.hpp b/src/upload.hpp index b0a7630..ea7b13b 100644 --- a/src/upload.hpp +++ b/src/upload.hpp @@ -1,7 +1,8 @@ #pragma once #include #include +#include "config.hpp" using namespace std; -bool sendFileToServer(string &path, size_t size); +bool sendFileToServer(Config &conf, string &path, size_t size); diff --git a/src/utils.cpp b/src/utils.cpp index 6d28677..f62f437 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -27,6 +27,10 @@ Result smcGetEmummcConfig(emummc_mmc_t mmc_id, emummc_config_t *out_cfg, void *o } string getAlbumPath() { + // workaround for SX OS + if (fs::exists("sdmc:/Emutendo/Album")) + return "sdmc:/Emutendo/Album"; + string out = "Nintendo/Album"; static struct { char storage_path[0x7F + 1]; @@ -47,10 +51,10 @@ bool isDigitsOnly(const string &str) { return str.find_first_not_of("0123456789") == string::npos; } -string getLastAlbumItem() { +string getLastAlbumItem(Config &conf) { vector years, months, days, files; - string albumPath = getAlbumPath(); - if (!fs::is_directory(albumPath)) return "no album directory"; + string albumPath = conf.getAlbumPath().length() > 0 ? conf.getAlbumPath() : getAlbumPath(); + if (!fs::is_directory(albumPath)) return ""; for (auto &entry : fs::directory_iterator(albumPath)) if (entry.is_directory() && isDigitsOnly(entry.path().filename()) && entry.path().filename().string().length() == 4) diff --git a/src/utils.hpp b/src/utils.hpp index 36ac7e2..23b1df3 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -1,10 +1,11 @@ #pragma once #include +#include "config.hpp" using namespace std; namespace fs = filesystem; -string getLastAlbumItem(); +string getLastAlbumItem(Config &conf); size_t filesize(string &path);