|
| 1 | +#include<stdexcept> |
1 | 2 | #include<format> |
| 3 | +#include<fstream> |
2 | 4 | #include<list> |
3 | 5 | #include<sstream> |
4 | 6 | #include<string> |
| 7 | +#include<system_error> |
5 | 8 | #include<curlpp/cURLpp.hpp> |
6 | 9 | #include<curlpp/Easy.hpp> |
7 | 10 | #include<curlpp/Form.hpp> |
@@ -55,26 +58,28 @@ std::string wiki::login(const std::string &api,const std::string &cookie_path,co |
55 | 58 | request.setOpt<curlpp::Options::CookieJar>(cookie_path); |
56 | 59 | init_request( |
57 | 60 | request, |
58 | | - api+"?format=json&action=login", |
59 | | - {{"format","json"},{"action","login"},{"lgname",username},{"lgpassword",password},{"lgtoken",login_token}}, |
| 61 | + api+"?format=json", |
| 62 | + {{"action","login"},{"lgname",username},{"lgpassword",password},{"lgtoken",login_token}}, |
60 | 63 | header |
61 | 64 | ); |
62 | 65 | return wiki::get(request); |
63 | 66 | } |
64 | 67 | nlohmann::json wiki::upload(const std::string &api,const std::string &cookie_path,const std::string &file_name,const std::string &file_path,const std::string &csrf_token,const std::list<std::string> &header){ |
65 | 68 | curlpp::Easy request; |
| 69 | + std::ifstream file{file_path,std::ios::in|std::ios::binary}; |
| 70 | + if(!file) |
| 71 | + throw std::runtime_error{std::format("Error: {} doesn't exist.",file_path)}; |
| 72 | + request.setOpt<curlpp::Options::Url>(api+"?format=json"); |
66 | 73 | request.setOpt<curlpp::Options::CookieFile>(cookie_path); |
67 | | - init_request( |
68 | | - request, |
69 | | - api+"?format=json", |
70 | | - { |
71 | | - {"action","upload"}, |
72 | | - {"filename",file_name}, |
73 | | - {"file",std::string{"@"}+file_path}, |
74 | | - {"token",csrf_token} |
75 | | - }, |
76 | | - header |
77 | | - ); |
| 74 | + request.setOpt<curlpp::Options::ReadStream>(&file); |
| 75 | + request.setOpt<curlpp::Options::Upload>(true); |
| 76 | + curlpp::Forms form; |
| 77 | + form.push_back(new curlpp::FormParts::Content("action","upload")); |
| 78 | + form.push_back(new curlpp::FormParts::Content("filename",file_name)); |
| 79 | + form.push_back(new curlpp::FormParts::File("file",file_path)); |
| 80 | + form.push_back(new curlpp::FormParts::Content("token",csrf_token)); |
| 81 | + request.setOpt<curlpp::Options::HttpPost>(form); |
| 82 | + request.setOpt<curlpp::Options::HttpHeader>(header); |
78 | 83 | nlohmann::json response=nlohmann::json::parse(wiki::get(request)); |
79 | 84 | return response; |
80 | 85 | } |
0 commit comments