From d3986ca14a1adae1148a6e1c48fa1800dbeddf41 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Fri, 11 Dec 2020 08:37:37 +0100 Subject: [PATCH] Server side file browser --- cps/admin.py | 35 ++++++---- cps/static/js/main.js | 110 +++++++++++-------------------- cps/templates/config_edit.html | 18 ++--- cps/templates/modal_dialogs.html | 3 +- cps/web.py | 2 +- 5 files changed, 74 insertions(+), 94 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index eaf4613f..b4d199b2 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -520,13 +520,21 @@ def list_restriction(res_type): response.headers["Content-Type"] = "application/json; charset=utf-8" return response +@admi.route("/basicconfig/pathchooser/") +@unconfigured +def config_pathchooser(): + return pathchooser() -@admi.route("/ajax/pathchooser/", endpoint="pathchooser") -@admi.route("/ajax/filechooser/", endpoint="filechooser") +@admi.route("/ajax/pathchooser/") @login_required @admin_required +def ajax_pathchooser(): + return pathchooser() + def pathchooser(): - browse_for = "folder" if request.endpoint == "admin.pathchooser" else "file" + browse_for = "folder" # if request.endpoint == "admin.pathchooser" else "file" + folder_only = request.args.get('folder', False) == "true" + file_filter = request.args.get('filter', "") path = os.path.normpath(request.args.get('path', "")) if os.path.isfile(path): @@ -538,11 +546,11 @@ def pathchooser(): abs = False if os.path.isdir(path): - if os.path.isabs(path): - cwd = os.path.realpath(path) - abs = True - else: - cwd = os.path.relpath(path) + #if os.path.isabs(path): + cwd = os.path.realpath(path) + abs = True + #else: + # cwd = os.path.relpath(path) else: cwd = os.getcwd() @@ -564,18 +572,19 @@ def pathchooser(): folders = [] files = [] - locale = get_locale() + # locale = get_locale() for f in folders: try: data = {"name": f, "fullpath": os.path.join(cwd, f)} data["sort"] = data["fullpath"].lower() - data["modified"] = format_datetime(datetime.fromtimestamp(int(os.path.getmtime(os.path.join(cwd, f)))), - format='short', locale=locale) - data["ext"] = os.path.splitext(f)[1] except Exception: continue if os.path.isfile(os.path.join(cwd, f)): + if folder_only: + continue + if file_filter != "" and file_filter != f: + continue data["type"] = "file" data["size"] = os.path.getsize(os.path.join(cwd, f)) @@ -604,7 +613,7 @@ def pathchooser(): return json.dumps(context) -@admi.route("/config", methods=["GET", "POST"]) +@admi.route("/basicconfig", methods=["GET", "POST"]) @unconfigured def basic_configuration(): logout_user() diff --git a/cps/static/js/main.js b/cps/static/js/main.js index 0414a63c..3fbaed88 100644 --- a/cps/static/js/main.js +++ b/cps/static/js/main.js @@ -213,22 +213,33 @@ $(function() { }); } - function fillFileTable(path, type) { - if (type === "dir") { - var request_path = "/../../ajax/pathchooser/"; + function fillFileTable(path, type, folder, filt) { + if (window.location.pathname.endsWith("/basicconfig")) { + var request_path = "/../basicconfig/pathchooser/"; } else { - var request_path = "/../../ajax/filechooser/"; + var request_path = "/../../ajax/pathchooser/"; } $.ajax({ dataType: "json", data: { path: path, + folder: folder, + filter: filt }, url: window.location.pathname + request_path, success: function success(data) { + if ($("#element_selected").text() ==="") { + $("#element_selected").text(data.cwd); + } $("#file_table > tbody > tr").each(function () { if ($(this).attr("id") !== "parent") { $(this).closest("tr").remove(); + } else { + if(data.absolute && data.parentdir !== "") { + $(this)[0].attributes['data-path'].value = data.parentdir; + } else { + $(this)[0].attributes['data-path'].value = ".."; + } } }); if (data.parentdir !== "") { @@ -444,83 +455,42 @@ $(function() { $("#fileModal").on("show.bs.modal", function(e) { - //get data-id attribute of the clicked element and store in button - //var submit = true; - //var cwd = "{{oldfile|default(cwd, True)|abspath|replace('\\', '\\\\')}}"; - //var isabsolute = true; - fillFileTable("","dir"); + var target = $(e.relatedTarget); + var path = $("#" + target.data("link"))[0].value; + var folder = target.data("folderonly"); + var filter = target.data("filefilter"); + $("#element_selected").text(path); + $("#file_confirm")[0].attributes["data-link"].value = target.data("link"); + $("#file_confirm")[0].attributes["data-folderonly"].value = (typeof folder === 'undefined') ? false : true; + $("#file_confirm")[0].attributes["data-filefilter"].value = (typeof filter === 'undefined') ? "" : filter; + $("#file_confirm")[0].attributes["data-newfile"].value = target.data("newfile"); + fillFileTable(path,"dir", folder, filter); }); - //(".tr-clickable").on("click", - $(document).on("click", ".tr-clickable", function() { - var path = this.attributes['data-path'].value; - var type = this.attributes['data-type'].value; - fillFileTable(path, type); + $("#file_confirm").click(function() { + $("#" + $(this).data("link"))[0].value = $("#element_selected").text() }); - - /*{% if type == 'folder' %} {# browsing for folder #} - var abspath = "{{url_for('app.pathchooser') + '?path=' + cwd|abspath|quote_plus}}"; - var relpath = "{{url_for('app.pathchooser') + '?path=' + cwd|relpath|quote_plus}}"; - {% else %} {# browsing for file #} - var abspath = "{{url_for('app.filechooser') + '?path=' + oldfile|default(cwd, True)|abspath|quote_plus}}"; - var relpath = "{{url_for('app.filechooser') + '?path=' + oldfile|default(cwd, True)|relpath|quote_plus}}"; - {% endif %}*/ - /*document.addEventListener("readystatechange", function(event) { - if (this.readyState === "complete") { - document.getElementById("tbody").style.height = (window.innerHeight - 25) + "px"; - window.onresize = function (event) { - document.getElementById("tbody").style.height = (window.innerHeight - 25) + "px"; - }; - var clickables = document.getElementsByClassName("tr-clickable"); - for (var i = 0; i < clickables.length; i++) { - clickables[i].onclick = (function () { - var onclick = clickables[i].onclick; - return function (e) { - if (onclick != null && !onclick()) { - return false - } - if (this.dataset.href !== undefined && this.dataset.href !== "#") { - window.location.href = this.dataset.href; - return false - } else { - return true; - } - } - })(); - } - } - }); - function updateParent() - { - if (window.top.SettingsUI !== undefined) { - window.top.SettingsUI.prototype.pathchooserChanged(this); - } - } - function setInvalid() { - submit = false; - cwd = ""; - updateParent(); + $(document).on("click", ".tr-clickable", function() { + var path = this.attributes["data-path"].value; + var type = this.attributes["data-type"].value; + var folder = $(file_confirm).data("folderonly"); + var filter = $(file_confirm).data("filefilter"); + var newfile = $(file_confirm).data("newfile"); + if (newfile !== 'undefined') { + $("#element_selected").text(path + $("#new_file".text())); + } else { + $("#element_selected").text(path); } - function setValid() { - submit = true; - updateParent(); + if(type === "dir") { + fillFileTable(path, type, folder, filter); } - function setFile(fullpath, name) - { - cwd = fullpath; - /*{*% if type == "file" %} {# browsing for file #} - abspath = "{{url_for('app.filechooser')}}?path={{cwd|abspath|quote_plus}}" + encodeURIComponent(name); - relpath = "{{url_for('app.filechooser')}}?path={{cwd|relpath|quote_plus}}" + encodeURIComponent(name); - {% endif %}*/ - /*setValid(); - }*/ + }); $("#btndeletetoken").click(function() { //get data-id attribute of the clicked element var pathname = document.getElementsByTagName("script"), src = pathname[pathname.length - 1].src; var path = src.substring(0, src.lastIndexOf("/")); - // var domainId = $(this).value("domainId"); $.ajax({ method:"get", url: path + "/../../kobo_auth/deleteauthtoken/" + this.value, diff --git a/cps/templates/config_edit.html b/cps/templates/config_edit.html index b66499dc..b86fd36b 100644 --- a/cps/templates/config_edit.html +++ b/cps/templates/config_edit.html @@ -19,7 +19,7 @@
- +
{% if feature_support['gdrive'] %} @@ -94,14 +94,14 @@
- +
- +
@@ -268,21 +268,21 @@
- +
- +
- +
@@ -384,7 +384,7 @@
- +
@@ -395,7 +395,7 @@
- +
{% if feature_support['rar'] %} @@ -403,7 +403,7 @@
- +
{% endif %} diff --git a/cps/templates/modal_dialogs.html b/cps/templates/modal_dialogs.html index 307322b3..038681b4 100644 --- a/cps/templates/modal_dialogs.html +++ b/cps/templates/modal_dialogs.html @@ -94,7 +94,8 @@
diff --git a/cps/web.py b/cps/web.py index 33bb9a91..f6e84339 100644 --- a/cps/web.py +++ b/cps/web.py @@ -304,7 +304,7 @@ def before_request(): g.shelves_access = ub.session.query(ub.Shelf).filter( or_(ub.Shelf.is_public == 1, ub.Shelf.user_id == current_user.id)).order_by(ub.Shelf.name).all() if not config.db_configured and request.endpoint not in ( - 'admin.basic_configuration', 'login') and '/static/' not in request.path: + 'admin.basic_configuration', 'login', "admin.config_pathchooser") and '/static/' not in request.path: return redirect(url_for('admin.basic_configuration'))