From 2e58a2adc109ccb6f4edbe4a712347800cd25871 Mon Sep 17 00:00:00 2001 From: amcmanu3 Date: Sun, 4 Jun 2023 23:08:27 -0400 Subject: [PATCH] Remove files from ajax --- app/classes/shared/helpers.py | 81 ---------- app/classes/web/ajax_handler.py | 18 --- .../templates/panel/server_files.html | 145 ++++++++++-------- 3 files changed, 83 insertions(+), 161 deletions(-) diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index 65bc853a..e150cd0b 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -1092,87 +1092,6 @@ class Helpers: return data - def generate_tree(self, folder, output=""): - dir_list = [] - unsorted_files = [] - file_list = os.listdir(folder) - for item in file_list: - if os.path.isdir(os.path.join(folder, item)): - dir_list.append(item) - elif str(item) != self.ignored_names: - unsorted_files.append(item) - file_list = sorted(dir_list, key=str.casefold) + sorted( - unsorted_files, key=str.casefold - ) - for raw_filename in file_list: - filename = html.escape(raw_filename) - rel = os.path.join(folder, raw_filename) - dpath = os.path.join(folder, filename) - if os.path.isdir(rel): - if filename not in self.ignored_names: - output += f"""
  • - \n
    - - - - {filename} - -
  • - \n""" - else: - if filename not in self.ignored_names: - output += f"""
  • - {filename}
  • """ - return output - - def generate_dir(self, folder, output=""): - dir_list = [] - unsorted_files = [] - file_list = os.listdir(folder) - for item in file_list: - if os.path.isdir(os.path.join(folder, item)): - dir_list.append(item) - elif str(item) != self.ignored_names: - unsorted_files.append(item) - file_list = sorted(dir_list, key=str.casefold) + sorted( - unsorted_files, key=str.casefold - ) - output += f"""\n" - return output - @staticmethod def generate_zip_tree(folder, output=""): file_list = os.listdir(folder) diff --git a/app/classes/web/ajax_handler.py b/app/classes/web/ajax_handler.py index 9419d82d..f3b22d32 100644 --- a/app/classes/web/ajax_handler.py +++ b/app/classes/web/ajax_handler.py @@ -108,24 +108,6 @@ class AjaxHandler(BaseHandler): ) self.finish() - elif page == "get_dir": - server_id = self.get_argument("id", None) - path = self.get_argument("path", None) - - if not self.check_server_id(server_id, "get_tree"): - return - server_id = bleach.clean(server_id) - - if Helpers.validate_traversal( - self.controller.servers.get_server_data_by_id(server_id)["path"], path - ): - self.write( - Helpers.get_os_understandable_path(path) - + "\n" - + Helpers.generate_dir(path) - ) - self.finish() - @tornado.web.authenticated def post(self, page): api_key, _, exec_user = self.current_user diff --git a/app/frontend/templates/panel/server_files.html b/app/frontend/templates/panel/server_files.html index 7dbcebb6..f3fa1179 100644 --- a/app/frontend/templates/panel/server_files.html +++ b/app/frontend/templates/panel/server_files.html @@ -882,68 +882,86 @@ }); } - function getTreeView(event) { - const path = $('#root_dir').data('path');; + function getDirView(event){ + let path = event.target.parentElement.getAttribute("data-path"); + if (document.getElementById(path).classList.contains('clicked')) { + return; + }else{ + getTreeView(path); + } - $.ajax({ - type: "GET", - url: "/files/get_tree?id=" + serverId + "&path=" + path, - dataType: 'text', - success: function (data) { - console.log("got response:"); + } + async function getTreeView(path){ + var token = getCookie("_xsrf"); + let res = await fetch(`/api/v2/servers/${serverId}/files`, { + method: 'POST', + headers: { + 'X-XSRFToken': token + }, + body: JSON.stringify({"page": "files", "folder": path}), + }); + let responseData = await res.json(); + if (responseData.status === "ok") { + console.log(responseData); + process_tree_response(responseData); - dataArr = data.split('\n'); - serverDir = dataArr.shift(); // Remove & return first element (server directory) - text = dataArr.join('\n'); + } else { - try { - document.getElementById(path).innerHTML += text; - event.target.parentElement.classList.add("clicked"); + bootbox.alert({ + title: responseData.status, + message: responseData.error + }); + } + } + + function process_tree_response(response) { + let path = response.data.root_path.path; + let text = ``; + if (!response.data.root_path.top){ + text = ``; + } + if(response.data.root_path.top){ + try { + document.getElementById('main-tree-div').innerHTML += text; + document.getElementById('main-tree').parentElement.classList.add("clicked"); } catch { document.getElementById('files-tree').innerHTML = text; } - - - document.getElementsByClassName('files-tree-title')[0].setAttribute('data-path', serverDir); - document.getElementsByClassName('files-tree-title')[0].setAttribute('data-name', 'Files'); - - setTimeout(function () { setTreeViewContext() }, 1000); - }, - }); - } - - function getToggleMain(event) { - path = event.target.parentElement.getAttribute('data-path'); - document.getElementById("files-tree").classList.toggle("d-block"); - document.getElementById(path + "span").classList.toggle("tree-caret-down"); - document.getElementById(path + "span").classList.toggle("tree-caret"); - } - - function getDirView(event) { - let path = event.target.parentElement.getAttribute('data-path'); - - if (document.getElementById(path).classList.contains('clicked')) { - - var toggler = document.getElementById(path + "span"); - - if (toggler.classList.contains('files-tree-title')) { - document.getElementById(path + "ul").classList.toggle("d-block"); - document.getElementById(path + "span").classList.toggle("tree-caret-down"); - } - return; - } else { - $.ajax({ - type: "GET", - url: "/files/get_dir?id=" + serverId + "&path=" + path, - dataType: 'text', - success: function (data) { - console.log("got response:"); - - dataArr = data.split('\n'); - serverDir = dataArr.shift(); // Remove & return first element (server directory) - text = dataArr.join('\n'); - - try { + }else{ + try { document.getElementById(path + "span").classList.add('tree-caret-down'); document.getElementById(path).innerHTML += text; document.getElementById(path).classList.add("clicked"); @@ -951,9 +969,7 @@ console.log("Bad") } - setTimeout(function () { setTreeViewContext() }, 1000); - - var toggler = document.getElementById(path); + var toggler = document.getElementById(path + "span"); if (toggler.classList.contains('files-tree-title')) { document.getElementById(path + "span").addEventListener("click", function caretListener() { @@ -961,9 +977,14 @@ document.getElementById(path + "span").classList.toggle("tree-caret-down"); }); } - }, - }); } + setTimeout(function () { setTreeViewContext() }, 1000); + } + function getToggleMain(event) { + path = event.target.parentElement.getAttribute('data-path'); + document.getElementById("files-tree").classList.toggle("d-block"); + document.getElementById(path + "span").classList.toggle("tree-caret-down"); + document.getElementById(path + "span").classList.toggle("tree-caret"); } function setTreeViewContext() { @@ -1172,7 +1193,7 @@ }); } - getTreeView(); + getTreeView($('#root_dir').data('path')); setTreeViewContext(); function setKeyboard(target) {