Skip to content

Commit bb7ec38

Browse files
committed
finished file-uploading test
modified: uploader/lib/libwiki.cpp modified: uploader/test/upload_existing.cpp
1 parent 1e9b17a commit bb7ec38

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

uploader/lib/libwiki.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
#include<stdexcept>
12
#include<format>
3+
#include<fstream>
24
#include<list>
35
#include<sstream>
46
#include<string>
7+
#include<system_error>
58
#include<curlpp/cURLpp.hpp>
69
#include<curlpp/Easy.hpp>
710
#include<curlpp/Form.hpp>
@@ -55,26 +58,28 @@ std::string wiki::login(const std::string &api,const std::string &cookie_path,co
5558
request.setOpt<curlpp::Options::CookieJar>(cookie_path);
5659
init_request(
5760
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}},
6063
header
6164
);
6265
return wiki::get(request);
6366
}
6467
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){
6568
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");
6673
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);
7883
nlohmann::json response=nlohmann::json::parse(wiki::get(request));
7984
return response;
8085
}

uploader/test/upload_existing.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ int main(){
2626
"24-25-大学物理(下)-期中-第一题图.svg",
2727
csrf_token
2828
);
29-
std::clog<<result<<std::endl;
30-
//if(result["upload"]["result"]!="Success")
31-
// std::clog<<result<<std::endl;
29+
assert(result["upload"]["result"]=="Warning");
30+
assert(result["upload"].find("warnings")!=result["upload"].end());
3231
}catch(const curlpp::RuntimeError &e){
3332
std::cerr<<e.what()<<std::endl;
3433
assert(false);

0 commit comments

Comments
 (0)