diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/include/api_module_downloader.h | 14 | ||||
-rw-r--r-- | server/modules/downloaders/curl/main.cpp | 58 | ||||
-rw-r--r-- | server/modules/downloaders/curl/main.h | 7 |
3 files changed, 77 insertions, 2 deletions
diff --git a/server/include/api_module_downloader.h b/server/include/api_module_downloader.h index dbe0d14..dc31811 100644 --- a/server/include/api_module_downloader.h +++ b/server/include/api_module_downloader.h @@ -76,16 +76,18 @@ struct download_content_entry_s { //here is basic content entry description, thi struct download_s { - int id; - std::string name; //id generated via download_add or via session restore and it's unique for current session and current dowloader + int id; //id generated via download_add or via session restore and it's unique for current session and current dowloader + std::string name; int64_t size, downloaded, download_speed; //download size and size of downloaded data, also download speed all vars in bytes std::map<int, std::string> info_map; //any additional info provided by downloader module can be stored here std::list<download_content_entry_s> content; //download content can be set for download, structure described above + std::map<std::string, std::string> metadata; }; class module_downloader : public module_base { public: + //basic actions virtual int add_download(std::map<int, std::string> params) = 0; //add download, this function must return unique for current session and current downloader download id virtual bool stop_download(int download_id) = 0; //stop download by id received via add_download, return true on success, false otherwise virtual bool start_download(int download_id) = 0; //start download by id received via add_download, return true on success, false otherwise @@ -93,6 +95,14 @@ public: virtual bool execute_action_on_download(int download_id, int action_id) = 0; //execute module defined action on download, by id received via add_download virtual std::list<download_s> get_downloads(std::map<int, std::string> params = std::map<int, std::string>()) = 0; //this function must return list of downloads, additional options may be passed via params map virtual download_s get_download(int download_id, std::map<int, std::string> params = std::map<int, std::string>()) = 0; //same as above, except this one is for single download, not a complete list + //metadata manipulation (api for client specified metadata, unused by server itself, for example labels should be implemented using this api) + virtual bool metadata_set(int download_id, std::map<std::string, std::string> metadata) = 0; //replace download metadata with provided one + virtual bool metadata_update(int download_id, std::map<std::string, std::string> metadata) = 0; //updated matching metadata entries, create non existent + virtual bool metadata_drop(int download_id, std::string var = std::string()) = 0; //remove all metadata if no variable specified, otherwise delete specified variable + virtual std::map<std::string, std::string> metadata_get(int download_id) = 0; //get download metadata + virtual std::string metadata_get(int download_id, std::string var) = 0; //get value of specified metadata variable + + protected: downloader_module_info info; }; diff --git a/server/modules/downloaders/curl/main.cpp b/server/modules/downloaders/curl/main.cpp index b3a2f36..f3da19e 100644 --- a/server/modules/downloaders/curl/main.cpp +++ b/server/modules/downloaders/curl/main.cpp @@ -166,6 +166,64 @@ download_s downloader::get_download(int download_id, std::map<int, std::string> return d; } +std::string compute_var_name(int download_id, std::string setting_name) +{ + std::string var_name = "download_"; + char id_s[64]; + snprintf(id_s, 63, "%d", download_id); + var_name += id_s; + var_name += "_"; + var_name += setting_name; + return var_name; +} + +bool downloader::metadata_set(int download_id, std::map<std::string, std::string> metadata) +{ + downloads[download_id].metadata = metadata; + for(auto i : downloads[download_id].metadata) + api->metadata_set(this, compute_var_name(download_id, i.first), std::vector<char>(i.second.begin(), i.second.end())); + return true; +} + +bool downloader::metadata_update(int download_id, std::map<std::string, std::string> metadata) +{ + for(auto i : metadata) + downloads[download_id].metadata[i.first] = i.second; + for(auto i : downloads[download_id].metadata) + api->metadata_set(this, compute_var_name(download_id, i.first), std::vector<char>(i.second.begin(), i.second.end())); + return true; +} + +bool downloader::metadata_drop(int download_id, std::string var) +{ + if(!var.empty()) + { + downloads[download_id].metadata.erase(var); + api->metadata_remove(this, compute_var_name(download_id, var)); + } + else + { + for(auto i : downloads[download_id].metadata) + api->metadata_remove(this, compute_var_name(download_id, i.first)); + downloads[download_id].metadata.clear(); + } + return true; +} +std::map<std::string, std::string> downloader::metadata_get(int download_id) +{ + return downloads[download_id].metadata; +} + +std::string downloader::metadata_get(int download_id, std::string var) +{ + auto i = downloads[download_id].metadata.find(var); + if(i != downloads[download_id].metadata.end()) + return i->second; + return std::string(); +} + + + extern "C" module_downloader* udm_downloader_module_load() { return new downloader; diff --git a/server/modules/downloaders/curl/main.h b/server/modules/downloaders/curl/main.h index 4c4cbe9..f5e589b 100644 --- a/server/modules/downloaders/curl/main.h +++ b/server/modules/downloaders/curl/main.h @@ -48,6 +48,13 @@ public: bool execute_action_on_download(int download_id, int action_id); //execute module defined action on download, by id received via add_download std::list<download_s> get_downloads(std::map<int, std::string> params = std::map<int, std::string>()); //this function must return list of downloads, additional options may be passed via params map download_s get_download(int download_id, std::map<int, std::string> params = std::map<int, std::string>()); //same as above, except this one is for single download, not a complete list + //downloader metadata manipulations + bool metadata_set(int download_id, std::map<std::string, std::string> metadata); //replace download metadata with provided one + bool metadata_update(int download_id, std::map<std::string, std::string> metadata); //updated matching metadata entries, create non existent + bool metadata_drop(int download_id, std::string var = std::string()); //remove all metadata if no variable specified, otherwise delete specified variable + std::map<std::string, std::string> metadata_get(int download_id); //get download metadata + std::string metadata_get(int download_id, std::string var); //get value of specified metadata variable + private: void on_modules_loaded(); std::map<int, my_download> downloads; //map of id, download |