Skip to content

Commit

Permalink
File rename Implemented in only qbittorrent
Browse files Browse the repository at this point in the history
  • Loading branch information
junedkh committed Dec 5, 2024
1 parent 69da540 commit 3ee6343
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 13 deletions.
40 changes: 35 additions & 5 deletions web/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,16 @@ <h2 id="modalTitle" class="text-xl font-bold mb-4"></h2>
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 = `
Expand All @@ -707,12 +717,32 @@ <h2 id="modalTitle" class="text-xl font-bold mb-4"></h2>
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 = '<p>Your Rename has been submitted successfully.</p>';
node.name = newName;
renderFileTree(currentFolder ? currentFolder.children : files);
} else {
modalTitle.textContent = 'Error';
modalBody.innerHTML = '<p>There was an error submitting your Rename. Please try again.</p>';
}
modalFooter.innerHTML = '<button class="btn btn-primary" onclick="closeModal()">Okay</button>';
openModal();
});
}
closeModal();
});

editNameInput.addEventListener('keypress', (e) => {
Expand Down Expand Up @@ -746,7 +776,7 @@ <h2 id="modalTitle" class="text-xl font-bold mb-4"></h2>
modalBody.innerHTML = `<p>Submitting your selection ${selectedCount.innerText} ... </p>`;
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();
Expand Down Expand Up @@ -785,7 +815,7 @@ <h2 id="modalTitle" class="text-xl font-bold mb-4"></h2>
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 => {
Expand Down
53 changes: 45 additions & 8 deletions web/wserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

app = Flask(__name__)


qbittorrent_client = qbClient(
host="localhost",
port=8090,
Expand Down Expand Up @@ -99,6 +100,7 @@ def handle_torrent():
"message": "PIN not specified",
}
)

code = ""
for nbr in gid:
if nbr.isdigit():
Expand All @@ -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": "",
Expand All @@ -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(
Expand Down

0 comments on commit 3ee6343

Please sign in to comment.