From 99048beee343e0aacf538d53ce91c7b545f09089 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 11 May 2020 20:03:09 +0200 Subject: webbox: added copy function --- plugins/webbox/webbox.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'plugins/webbox/webbox.cpp') diff --git a/plugins/webbox/webbox.cpp b/plugins/webbox/webbox.cpp index 12e62ef..4abb2bf 100644 --- a/plugins/webbox/webbox.cpp +++ b/plugins/webbox/webbox.cpp @@ -576,6 +576,58 @@ protected: } }; +class CopyCommand: public PostCommand +{ +public: + CopyCommand() + { + m_commandName = "copy"; + m_isWriteCommand = true; + } + +protected: + virtual std::string start(CommandParameters& p) + { + std::string result{}; + fs::path targetDir{}; + + readContent(p); + + pt::ptree tree; + std::istringstream ss{m_content}; + pt::read_xml(ss, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); + + try { + auto elements {tree.get_child("request")}; + for (const auto& element: elements) { + if (element.first == "target") { + targetDir = p.m_path / element.second.data(); + } else if (element.first == "file") { + std::string filename{element.second.data()}; + fs::path old_path{p.m_path / filename}; + fs::path new_path{targetDir / filename}; + try { + fs::copy(old_path, new_path, fs::copy_options::overwrite_existing | fs::copy_options::recursive ); + } catch (const std::exception& ex) { + result += "Error copying "s + filename + ": "s + ex.what() + "
"s; + } + } else { + result += "Unknown element: "s + element.first + "
"s; + } + } + } catch (const std::exception& ex) { + return HttpStatus("500", "Reading file list: "s + ex.what(), p); + } + + if (result.empty()) { + result = "OK"; + } + + p.m_SetResponseHeader("content_type", "text/plain"); + return result; + } +}; + class MoveCommand: public PostCommand { public: @@ -829,6 +881,7 @@ webbox_plugin::webbox_plugin() registerCommand(std::make_shared()); registerCommand(std::make_shared()); registerCommand(std::make_shared()); + registerCommand(std::make_shared()); registerCommand(std::make_shared()); registerCommand(std::make_shared()); registerCommand(std::make_shared()); -- cgit v1.2.3