From 3a9a8aa78ada7c5137b4b74c41be7088d2d1fd00 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Sun, 24 Jan 2016 13:45:40 +0300 Subject: server: curl_downloader_module: basic data transfer handling (now able to save file, still need a lot of validation) some tracing code this may be called first working prototype with vary basic functionality implemented. currently udm have working server, working qt client and able to download url via curl module (most of additional events like download progress currently not implemented, code need lots of sanity checks and exception handling) --- .../modules/downloaders/curl/src/curl_download.cpp | 54 +++++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) (limited to 'server/modules/downloaders/curl/src/curl_download.cpp') diff --git a/server/modules/downloaders/curl/src/curl_download.cpp b/server/modules/downloaders/curl/src/curl_download.cpp index 054ce22..d80a6e4 100644 --- a/server/modules/downloaders/curl/src/curl_download.cpp +++ b/server/modules/downloaders/curl/src/curl_download.cpp @@ -21,6 +21,7 @@ #include "curl_download.h" #include +#include size_t curl_w_callback(char *ptr, size_t size, size_t nmemb, void *userdata) @@ -29,10 +30,34 @@ size_t curl_w_callback(char *ptr, size_t size, size_t nmemb, void *userdata) if(d->get_cancel_state()) return -1; //cancel transfer //TODO: implement pause + std::ofstream &of(d->get_ofile()); + if(!of.is_open()) + { + //TODO: sanity check and path validation + try{ + of.open(d->get_download_path(), std::ios::out | std::ios::binary); + } + catch(...) //TODO: handle exception + { + } + } size_t size_ = size * nmemb; if(size_) { - //TODO: write data + if(of.is_open()) + { + try{ + of.write(ptr, size_); + } + catch(...) //TODO: handle exception + { + } + } + else + { + return -1; + //TODO: handle error + } } return size_; @@ -78,6 +103,7 @@ bool curl_download::delete_download() { cancel_transfer = true; curl_easy_cleanup(easy_handle); + //TODO: cleanup metadata return true; //TODO: } @@ -88,6 +114,30 @@ void curl_download::perform_internal() state = core_events::download_error; else state = core_events::download_completed; + ofile.close(); + {//curl debug + char *url; + curl_easy_getinfo(easy_handle, CURLINFO_EFFECTIVE_URL, &url); + BOOST_LOG_TRIVIAL(debug)<<__FILE__<<":"<<__LINE__<<"\t"<<__func__<<"\nCURLINFO_EFFECTIVE_URL:\t"< l; core_events::download_state_info i; i.download_id = id; @@ -99,6 +149,6 @@ void curl_download::perform_internal() curl_download::~curl_download() { - curl_easy_cleanup(easy_handle); + //curl_easy_cleanup(easy_handle); //this crashes for some reason } -- cgit v1.2.3