Skip to content

Commit

Permalink
upload every pic only once
Browse files Browse the repository at this point in the history
  • Loading branch information
HookedBehemoth committed Sep 9, 2019
1 parent a5ae9fb commit 3365a60
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace fs = std::filesystem;
std::vector<fs::path> getDirectoryFiles(const std::string & dir, const std::vector<std::string> & extensions);
std::string getAlbumPath();
std::string getUrl(fs::path path);
std::string uploadFile(std::string filePath);
Result smcGetEmummcConfig(emummc_mmc_t mmc_id, emummc_config_t *out_cfg, void *out_paths);
size_t WriteCallback(const char *contents, size_t size, size_t nmemb, std::string *userp);
Expand Down
2 changes: 1 addition & 1 deletion source/ListLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "utils.hpp"

extern appcolor theme;
extern std::string path;
extern fs::path path;
extern MainApplication *mainApp;
std::vector<fs::path> *filePaths = new std::vector<fs::path>;
ListLayout::ListLayout() {
Expand Down
11 changes: 8 additions & 3 deletions source/MainApplication.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "MainApplication.hpp"

MainApplication *mainApp;
std::string path;
fs::path path;
MainApplication::MainApplication() {
mainApp = this;
this->listLayout = ListLayout::New();
Expand All @@ -28,8 +28,13 @@ void MainApplication::onInput_upload(u64 Down, u64 Up, u64 Held) {
list();
}
if(Down & KEY_A) {
this->uploadLayout->showUrl(uploadFile(path));
this->uploadLayout->SetOnInput(std::bind(&MainApplication::onInput_back, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
std::string url = getUrl(path);
if(url.compare("")) {
this->uploadLayout->showUrl(url);
this->uploadLayout->SetOnInput(std::bind(&MainApplication::onInput_back, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
} else {
this->uploadLayout->showUrl("FAILED!");
}
}
}
void MainApplication::onInput_back(u64 Down, u64 Up, u64 Held) {
Expand Down
38 changes: 38 additions & 0 deletions source/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,44 @@ Result smcGetEmummcConfig(emummc_mmc_t mmc_id, emummc_config_t *out_cfg, void *o
}
}
return rc;
}
// https://github.com/AtlasNX/Kosmos-Updater/blob/master/source/FileManager.cpp#L34
bool writeFile(std::string filename, std::string data) {
FILE * file = fopen(filename.c_str(), "w");
if (!file) {
return false;
}
size_t result = fwrite(data.c_str(), sizeof(char), data.size(), file);
fflush(file);
fclose(file);
return (result == data.size());
}
// https://github.com/AtlasNX/Kosmos-Updater/blob/master/source/FileManager.cpp#L58
bool fileExists(std::string filename) {
FILE * file = fopen(filename.c_str(), "r");
if (file) {
fflush(file);
fclose(file);
return true;
}
return false;
}

std::string getUrl(fs::path path) {
std::string txtloc = path.string() + ".txt";
std::string url;
if(!fileExists(txtloc)) { //Upload if file doesn't exist
url = uploadFile(path.string());
if(url.compare("")) writeFile(txtloc, url);//fwrite(url.c_str(), sizeof(char), url.size(), file);
} else { //read url from string
FILE * file = fopen(txtloc.c_str(), "r");
char line[1024];
fgets(line, 1024, file);
url = line;
fflush(file);
fclose(file);
}
return url;
}

std::string uploadFile(std::string filePath) {
Expand Down

0 comments on commit 3365a60

Please sign in to comment.