|
| 1 | +#include<cstdlib> |
| 2 | +#include<exception> |
| 3 | +#include<filesystem> |
| 4 | +#include<format> |
| 5 | +#include<fstream> |
| 6 | +#include<iostream> |
| 7 | +#include<string> |
| 8 | +#include<nlohmann/json.hpp> |
| 9 | +#include"wiki.hpp" |
| 10 | +const std::string API="http://localhost:8080/api.php"; |
| 11 | +const std::string LOGIN="/tmp/wiki_login_cookies"; |
| 12 | +const std::string CSRF="/tmp/wiki_csrf_cookies"; |
| 13 | +const std::list<std::string> HEADER{std::format("X-Byrdocs-Token:{}",std::getenv("WIKITOKEN"))}; |
| 14 | +int main(int argc,char *argv[]){ |
| 15 | + try{ |
| 16 | + std::string file_path{argv[1]}; |
| 17 | + std::filesystem::path fs_path{file_path}; |
| 18 | + if(argc!=2){ |
| 19 | + std::cout<<std::format(R"(Usage: {} <path-to-file>)",argv[0])<<std::endl; |
| 20 | + return 1; |
| 21 | + } |
| 22 | + const std::string login_token=wiki::get_login_token(API,LOGIN); |
| 23 | + wiki::login( |
| 24 | + API, |
| 25 | + LOGIN, |
| 26 | + "CppHusky@uploader", |
| 27 | + std::getenv("WIKIPASS_UPLOADER"), |
| 28 | + login_token |
| 29 | + ); |
| 30 | + std::clog<<std::format("Logged in. Started uploading {}...",argv[1])<<std::endl; |
| 31 | + const std::string csrf_token=wiki::get_csrf_token(API,LOGIN,CSRF); |
| 32 | + if(!std::filesystem::exists(fs_path)){ |
| 33 | + std::cerr<<std::format("{}: file does not exist.",file_path)<<std::endl; |
| 34 | + return 2; |
| 35 | + } |
| 36 | + if(std::filesystem::is_directory(fs_path)){ |
| 37 | + std::cerr<<std::format("{}: is a directory.",file_path)<<std::endl; |
| 38 | + return 3; |
| 39 | + } |
| 40 | + std::string filename{std::filesystem::path{file_path}.filename()}; |
| 41 | + nlohmann::json result=wiki::upload( |
| 42 | + API, |
| 43 | + CSRF, |
| 44 | + filename, |
| 45 | + file_path, |
| 46 | + csrf_token, |
| 47 | + HEADER |
| 48 | + ); |
| 49 | + std::clog<<result<<std::endl; |
| 50 | + if(result["upload"]["result"]!="Success") |
| 51 | + return 3; |
| 52 | + }catch(const curlpp::RuntimeError &e){ |
| 53 | + std::cerr<<e.what()<<std::endl; |
| 54 | + assert(false); |
| 55 | + }catch(const curlpp::LogicError &e){ |
| 56 | + std::cerr<<e.what()<<std::endl; |
| 57 | + assert(false); |
| 58 | + }catch(const std::exception &e){ |
| 59 | + std::cerr<<e.what()<<std::endl; |
| 60 | + assert(false); |
| 61 | + } |
| 62 | +} |
0 commit comments