add upload file

This commit is contained in:
chatlanin 2024-07-25 09:59:47 +03:00
parent b1887ffeac
commit f18d7a4bca
2 changed files with 46 additions and 0 deletions

View File

@ -161,6 +161,50 @@ namespace trs
return r;
}
auto upload(std::string from, std::string to)
{
auto r = cpr::Get(cpr::Url(from));
try
{
if (r.status_code != 200)
{
hack::exception ex;
ex.description("Dont save file, status code not 200");
ex.params("from", from);
ex.params("to", to);
ex.params("status_code", r.status_code);
throw ex;
}
std::ofstream file(to, std::ios::out | std::ios::binary);
file.write(r.text.c_str(), r.text.size());
file.close();
}
catch(hack::exception& ex)
{
throw ex;
}
catch(std::exception& e)
{
hack::exception ex;
ex.description("Dont save file");
ex.params("from", from);
ex.params("to", to);
ex.system_error(e);
throw ex;
}
catch(...)
{
hack::exception ex;
ex.description(var::ALIEN_SYSTEM_ERROR);
ex.params("from", from);
ex.params("to", to);
throw ex;
}
}
private:
std::string m_url;
// httplib::Client m_cli;

View File

@ -50,6 +50,8 @@ auto main(int argc, char* args[]) -> int
auto r2 = cli_2.post("func_name", j);
hack::log()(r1.text, r2.status_code);
cli_2.upload("https://file-examples.com/storage/feaf4c715066a139295358c/2017/11/file_example_MP3_700KB.mp3", "/mnt/raid/projects/trs/test.mp3");
trs::server srv;
srv.init("test service");