From f18d7a4bcad7492aac20bf40b174cd8f52f1ab3b Mon Sep 17 00:00:00 2001 From: chatlanin Date: Thu, 25 Jul 2024 09:59:47 +0300 Subject: [PATCH] add upload file --- src/trs/trs.hpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ tests/main.cpp | 2 ++ 2 files changed, 46 insertions(+) diff --git a/src/trs/trs.hpp b/src/trs/trs.hpp index 15eddde..0b14ccc 100644 --- a/src/trs/trs.hpp +++ b/src/trs/trs.hpp @@ -160,6 +160,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; diff --git a/tests/main.cpp b/tests/main.cpp index b69ae5f..6cd985c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -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");