diff --git a/web/templates/page.html b/web/templates/page.html index 4b5cab1ed5f..749f1fc4203 100644 --- a/web/templates/page.html +++ b/web/templates/page.html @@ -693,6 +693,16 @@

renderFileTree(nodes); } + function getFullPath(node) { + const path = []; + let currentNode = findParent(files[0], node.id); + while (currentNode) { + path.unshift(currentNode.name); + currentNode = findParent(files[0], currentNode.id); + } + return path.length > 0 ? path.join('/') : ''; + } + function openEditFileNameModal(node) { modalTitle.textContent = 'Edit File Name'; modalBody.innerHTML = ` @@ -707,12 +717,32 @@

const editNameInput = document.getElementById('editNameInput'); saveNameBtn.addEventListener('click', () => { + closeModal(); const newName = editNameInput.value.trim(); if (newName && newName !== node.name) { - node.name = newName; - renderFileTree(currentFolder ? currentFolder.children : files); + const fullPath = getFullPath(node); + const requestUrl = `/app/files/torrent?gid=${urlParams.gid}&pin=${pinInput.value}&mode=rename`; + const body = { + old_path: fullPath ? `${fullPath}/${node.name}` : node.name, + new_path: fullPath ? `${fullPath}/${newName}` : newName, + }; + fetch(requestUrl, { + 'method': 'POST', + 'body': JSON.stringify(body), + }).then(response => { + if (response.ok) { + modalTitle.textContent = 'Success!'; + modalBody.innerHTML = '

Your Rename has been submitted successfully.

'; + node.name = newName; + renderFileTree(currentFolder ? currentFolder.children : files); + } else { + modalTitle.textContent = 'Error'; + modalBody.innerHTML = '

There was an error submitting your Rename. Please try again.

'; + } + modalFooter.innerHTML = ''; + openModal(); + }); } - closeModal(); }); editNameInput.addEventListener('keypress', (e) => { @@ -746,7 +776,7 @@

modalBody.innerHTML = `

Submitting your selection ${selectedCount.innerText} ...

`; modalFooter.innerHTML = ''; openModal(); - const requestUrl = `/app/files/torrent?gid=${urlParams.gid}&pin=${pinInput.value}`; + const requestUrl = `/app/files/torrent?gid=${urlParams.gid}&pin=${pinInput.value}&mode=selection`; fetch(requestUrl, { 'method': 'POST', 'body': JSON.stringify(files) }).then(response => { if (reusableModal.style.display === 'block') { closeModal(); @@ -785,7 +815,7 @@

openModal(); return false; } - const requestUrl = `/app/files/torrent?gid=${urlParams.gid}&pin=${pinInput.value}`; + const requestUrl = `/app/files/torrent?gid=${urlParams.gid}&pin=${pinInput.value}&mode=get`; fetch(requestUrl).then(function (response) { if (response.ok) { return response.json().then(data => { diff --git a/web/wserver.py b/web/wserver.py index 4847b0a04bd..16e14ff2419 100644 --- a/web/wserver.py +++ b/web/wserver.py @@ -8,6 +8,7 @@ app = Flask(__name__) + qbittorrent_client = qbClient( host="localhost", port=8090, @@ -99,6 +100,7 @@ def handle_torrent(): "message": "PIN not specified", } ) + code = "" for nbr in gid: if nbr.isdigit(): @@ -115,16 +117,42 @@ def handle_torrent(): } ) if request.method == "POST": + if not (mode := request.args.get("mode")): + return jsonify( + { + "files": [], + "engine": "", + "error": "Mode is not specified", + "message": "Mode is not specified", + } + ) data = request.get_json(cache=False, force=True) - selected_files, unselected_files = extract_file_ids(data) - if len(gid) > 20: - selected_files = "|".join(selected_files) - unselected_files = "|".join(unselected_files) - set_qbittorrent(gid, selected_files, unselected_files) + if mode == "rename": + if len(gid) > 20: + handle_rename(gid, data) + content = { + "files": [], + "engine": "", + "error": "", + "message": "Rename successfully.", + } + else: + content = { + "files": [], + "engine": "", + "error": "Rename failed.", + "message": "Cannot rename aria2c torrent file", + } else: - selected_files = ",".join(selected_files) - set_aria2(gid, selected_files) - content = { + selected_files, unselected_files = extract_file_ids(data) + if len(gid) > 20: + selected_files = "|".join(selected_files) + unselected_files = "|".join(unselected_files) + set_qbittorrent(gid, selected_files, unselected_files) + else: + selected_files = ",".join(selected_files) + set_aria2(gid, selected_files) + content = { "files": [], "engine": "", "error": "", @@ -149,6 +177,15 @@ def handle_torrent(): return jsonify(content) +def handle_rename(gid, data): + try: + qbittorrent_client.torrents_rename_file(torrent_hash=gid, **data) + except NotFound404Error as e: + raise NotFound404Error from e + except Exception as e: + LOGGER.error(f"{e} Errored in renaming") + + def set_qbittorrent(gid, selected_files, unselected_files): try: qbittorrent_client.torrents_file_priority(