diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-05-11 20:03:09 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-05-11 20:03:09 +0200 |
commit | 99048beee343e0aacf538d53ce91c7b545f09089 (patch) | |
tree | 8b810067649a7762350b81edd3498ccfdaefe18e | |
parent | 15b2be158ac0147982dd30382251b3ce83e219c7 (diff) |
webbox: added copy function
-rw-r--r-- | plugins/webbox/TODO | 1 | ||||
-rw-r--r-- | plugins/webbox/html/index.html | 10 | ||||
-rw-r--r-- | plugins/webbox/html/webbox.js | 65 | ||||
-rw-r--r-- | plugins/webbox/webbox.cpp | 53 |
4 files changed, 128 insertions, 1 deletions
diff --git a/plugins/webbox/TODO b/plugins/webbox/TODO index 108b645..288cf38 100644 --- a/plugins/webbox/TODO +++ b/plugins/webbox/TODO @@ -4,7 +4,6 @@ Prio 1 (for next version) provide index for nested directories, also if requested via url gallery Info if not selected: all -Copy function Prio 2 (for future versions) ====== diff --git a/plugins/webbox/html/index.html b/plugins/webbox/html/index.html index cc1cf12..01d1a5a 100644 --- a/plugins/webbox/html/index.html +++ b/plugins/webbox/html/index.html @@ -44,6 +44,7 @@ <tr><td class="entry" onclick="hideMenu(); download();">Download</td></tr> <tr class="writecommand"><td class="entry" onclick="hideMenu(); upload();">Upload</td></tr> <tr class="writecommand"><td class="entry" onclick="hideMenu(); deleteItems();">Delete</td></tr> + <tr class="writecommand"><td class="entry" onclick="hideMenu(); copy();">Copy</td></tr> <tr class="writecommand"><td class="entry" onclick="hideMenu(); move();">Move</td></tr> <tr class="writecommand"><td class="entry" onclick="hideMenu(); rename();">Rename</td></tr> <tr><td class="entry" onclick="hideMenu(); info();">Info</td></tr> @@ -67,6 +68,15 @@ Download multiple files as ZIP?<br> </div> + <div id="copy-dialog" hidden> + Copying files to folder:<br> + <input type="text" id="copydir" class="textinput"></input> + <br/> + Files to copy: + <br/> + <div id="copyfiles"></div> + </div> + <div id="move-dialog" hidden> Moving files to folder:<br> <input type="text" id="movedir" class="textinput"></input> diff --git a/plugins/webbox/html/webbox.js b/plugins/webbox/html/webbox.js index e056c16..80ee8ed 100644 --- a/plugins/webbox/html/webbox.js +++ b/plugins/webbox/html/webbox.js @@ -626,6 +626,71 @@ function deleteItems() { } } +function copy() { + showDialog(); + + var files = getSelectedFiles(); + if (files.length == 0) { + document.getElementById("dialog").innerHTML = "No files selected."; + document.getElementById("okbutton").onclick = hideDialog; + return; + } + + var message = ""; + for (var i = 0; i < files.length; i++) { + message += files[i] + "<br/>"; + } + + document.getElementById("dialog").innerHTML = document.getElementById("copy-dialog").innerHTML; + document.getElementById("copydir").focus(); + document.getElementById("copydir").onkeydown = function(evt) { + if (evt.key == "Enter") { + document.getElementById("okbutton").click(); + } + } + + document.getElementById("copyfiles").innerHTML = message; + + document.getElementById("okbutton").onclick = function() { + + var xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = function() { + if (this.readyState != 4) { + return; + } + if (this.status != 200) { + document.getElementById("dialog").innerHTML = "HTTP error"; + } else { + document.getElementById("dialog").innerHTML = xhr.responseText; + } + + document.getElementById("okbutton").onclick = hideDialog; + document.getElementById("okbutton").focus(); + loadContents(currentDir); // load new file list with deleted items + } + + var parser = new DOMParser(); + var xmlDocument = parser.parseFromString("<request></request>", "text/xml"); + var filesElement = xmlDocument.getElementsByTagName("request")[0]; + + var targetElement = xmlDocument.createElement("target"); + targetElement.appendChild(document.createTextNode(document.getElementById("copydir").value)); + filesElement.appendChild(targetElement); + + for (var i = 0; i < files.length; i++) { + var fileElement = xmlDocument.createElement("file"); + fileElement.appendChild(document.createTextNode(files[i])); + filesElement.appendChild(fileElement); + } + + xhr.open("POST", relativePath(currentDir) + "?command=copy", true); + xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); + xhr.setRequestHeader("Content-type", "text/xml"); + xhr.send(xmlDocument); + } +} + function move() { showDialog(); 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() + "<br>"s; + } + } else { + result += "Unknown element: "s + element.first + "<br>"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<InfoCommand>()); registerCommand(std::make_shared<DownloadZipCommand>()); registerCommand(std::make_shared<DeleteCommand>()); + registerCommand(std::make_shared<CopyCommand>()); registerCommand(std::make_shared<MoveCommand>()); registerCommand(std::make_shared<RenameCommand>()); registerCommand(std::make_shared<UploadCommand>()); |