Skip to content

Commit

Permalink
configurable album path
Browse files Browse the repository at this point in the history
  • Loading branch information
bakatrouble committed Jan 20, 2020
1 parent 6ba1fdc commit 00320f4
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 16 deletions.
5 changes: 5 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[server]
destination_id = undefined
; url = https://screenuploader.bakatrouble.me/upload/<destid>/
; 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
23 changes: 20 additions & 3 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,37 @@
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");
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_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;
}
10 changes: 8 additions & 2 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ const string URLplaceholder = "<destid>";

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 = "";
};
9 changes: 6 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <iostream>
#include "upload.hpp"
#include "utils.hpp"
#include "config.hpp"

using namespace std;

Expand Down Expand Up @@ -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;
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/upload.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include <switch.h>
#include <string>
#include "config.hpp"

using namespace std;

bool sendFileToServer(string &path, size_t size);
bool sendFileToServer(Config &conf, string &path, size_t size);
10 changes: 7 additions & 3 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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<string> 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 "<No album directory: " + albumPath + ">";

for (auto &entry : fs::directory_iterator(albumPath))
if (entry.is_directory() && isDigitsOnly(entry.path().filename()) && entry.path().filename().string().length() == 4)
Expand Down
3 changes: 2 additions & 1 deletion src/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include <filesystem>
#include "config.hpp"

using namespace std;
namespace fs = filesystem;


string getLastAlbumItem();
string getLastAlbumItem(Config &conf);
size_t filesize(string &path);

0 comments on commit 00320f4

Please sign in to comment.