From def52539028cb024b4e9e9767796face08d645f6 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 14 May 2020 18:21:04 +0200 Subject: Webbox: Support subdirectory via URL. This makes webbox viable as index page --- plugins/webbox/html/webbox.js | 69 ++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 21 deletions(-) (limited to 'plugins/webbox/html') diff --git a/plugins/webbox/html/webbox.js b/plugins/webbox/html/webbox.js index f1c01f5..b262f6f 100644 --- a/plugins/webbox/html/webbox.js +++ b/plugins/webbox/html/webbox.js @@ -1,4 +1,5 @@ var currentDir = "/"; +var rootDir = "/"; var listElements; var numberOfSelectedRows = 0; var username = "notaname"; @@ -11,6 +12,24 @@ function relativePath(path) { return path; } +function absolutePath(path) { + var result = rootDir + "/" + relativePath(path); + + if (rootDir.startsWith("//")) { + rootDir = rootDir.substr(1); + } + + if (rootDir != "/" && rootDir.endsWith("/")) { + rootDir = rootDir.substr(0, rootDir.length - 1); + } + + return result; +} + +function setRootDir(dir) { + rootDir = dir; +} + function clearContents() { var result = ""; // empty list @@ -50,15 +69,10 @@ function loadContents(dir) { full_path = full_path.substr(1); } - // make relative path - while (full_path.substr(0, 1) == "/") { - full_path = full_path.substr(1); - } - var name_td; if (type == "file") { type = ""; - name_td = ""; + name_td = ""; } else if (type == "dir") { type = ""; name_td = ""; @@ -87,7 +101,7 @@ function loadContents(dir) { } - xhr.open("GET", relativePath(dir) + "?command=list", true); + xhr.open("GET", absolutePath(dir) + "?command=list", true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr.send(); } @@ -337,6 +351,12 @@ function initMainpage() { var readOnly = serverInfo.getElementsByTagName("readonly")[0].textContent; prepareReadOnly(readOnly); + var rootDir = serverInfo.getElementsByTagName("rootdir")[0].textContent; + setRootDir(rootDir); + + // fill file list initially + setCurrentDir(serverInfo.getElementsByTagName("currentdir")[0].textContent); + // if successful: continue loading initMainpage2(); } @@ -349,8 +369,6 @@ function initMainpage() { // deferred initialization after successful login function initMainpage2() { - // fill file list initially - setCurrentDir("/"); // load footer var xhrFooter = new XMLHttpRequest(); @@ -404,6 +422,13 @@ function addDirectoryLinks(path) { } function setCurrentDir(newDir) { + if (newDir.endsWith("/")) { + newDir = newDir.substr(0, newDir.length - 1); + } + if (!newDir.startsWith("/")) { + newDir = "/" + newDir; + } + currentDir = newDir; loadContents(newDir); @@ -462,7 +487,7 @@ function download(filename) { filesElement.appendChild(fileElement); } - xhr.open("POST", relativePath(currentDir) + "?command=download-zip", true); + xhr.open("POST", absolutePath(currentDir) + "?command=download-zip", true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr.setRequestHeader("Content-type", "text/xml"); xhr.responseType = 'blob'; @@ -489,7 +514,7 @@ function download(filename) { dir += "/" } progressOn(); - xhr.open("GET", relativePath(dir) + filename, true); + xhr.open("GET", absolutePath(dir) + filename, true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr.responseType = 'blob'; xhr.send(); @@ -524,7 +549,7 @@ function createDir() { dirElement.appendChild(document.createTextNode(document.getElementById("newdir").value)); - xhr.open("POST", relativePath(currentDir) + "?command=newdir", true); + xhr.open("POST", absolutePath(currentDir) + "?command=newdir", true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr.setRequestHeader("Content-type", "text/xml"); xhr.send(xmlDocument); @@ -588,7 +613,7 @@ function onUploadFile() { formData.append("uploadfile", uploadfile.files[i]); } - xhr.open("POST", relativePath(currentDir) + "?command=upload", true); + xhr.open("POST", absolutePath(currentDir) + "?command=upload", true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr.send(formData); } @@ -638,7 +663,7 @@ function deleteItems() { filesElement.appendChild(fileElement); } - xhr.open("POST", relativePath(currentDir) + "?command=delete", true); + xhr.open("POST", absolutePath(currentDir) + "?command=delete", true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr.setRequestHeader("Content-type", "text/xml"); xhr.send(xmlDocument); @@ -703,7 +728,7 @@ function copy() { filesElement.appendChild(fileElement); } - xhr.open("POST", relativePath(currentDir) + "?command=copy", true); + xhr.open("POST", absolutePath(currentDir) + "?command=copy", true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr.setRequestHeader("Content-type", "text/xml"); xhr.send(xmlDocument); @@ -768,7 +793,7 @@ function move() { filesElement.appendChild(fileElement); } - xhr.open("POST", relativePath(currentDir) + "?command=move", true); + xhr.open("POST", absolutePath(currentDir) + "?command=move", true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr.setRequestHeader("Content-type", "text/xml"); xhr.send(xmlDocument); @@ -832,7 +857,7 @@ function rename() { newnameElement.appendChild(document.createTextNode(document.getElementById("renamenew").value)); filesElement.appendChild(newnameElement); - xhr.open("POST", relativePath(currentDir) + "?command=rename", true); + xhr.open("POST", absolutePath(currentDir) + "?command=rename", true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr.setRequestHeader("Content-type", "text/xml"); xhr.send(xmlDocument); @@ -878,12 +903,14 @@ function info() { var filesElement = xmlDocument.getElementsByTagName("files")[0]; for (var i = 0; i < files.length; i++) { - var fileElement = xmlDocument.createElement("file"); - fileElement.appendChild(document.createTextNode(files[i])); - filesElement.appendChild(fileElement); + if (files[i] != "..") { + var fileElement = xmlDocument.createElement("file"); + fileElement.appendChild(document.createTextNode(files[i])); + filesElement.appendChild(fileElement); + } } - xhr.open("POST", relativePath(currentDir) + "?command=info", true); + xhr.open("POST", absolutePath(currentDir) + "?command=info", true); xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password)); xhr.setRequestHeader("Content-type", "text/xml"); xhr.send(xmlDocument); -- cgit v1.2.3
" + filename + "
" + filename + "
" + filename + "