luci-base: luci.js: add LuCI.fspath() helper
authorJo-Philipp Wich <jo@mein.io>
Sun, 12 Apr 2020 20:27:16 +0000 (22:27 +0200)
committerJo-Philipp Wich <jo@mein.io>
Thu, 7 May 2020 17:40:49 +0000 (19:40 +0200)
The LuCI.fspath() function allows constructing absolute filesystem paths
from path segments relative to the document root.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(cherry picked from commit fde144c9be61d7886e81240233328a61d7088d08)

modules/luci-base/htdocs/luci-static/resources/luci.js
modules/luci-base/luasrc/view/header.htm

index 19ff2e1991e8431966a4ecdab3334b4fdcca6a19..954a8aa2b1a4a49e00d2f1cb542037ab3a9b4e6c 100644 (file)
                 */
                env: {},
 
+               /**
+                * Construct an absolute filesystem path relative to the server
+                * document root.
+                *
+                * @instance
+                * @memberof LuCI
+                *
+                * @param {...string} [parts]
+                * An array of parts to join into a path.
+                *
+                * @return {string}
+                * Return the joined path.
+                */
+               fspath: function(/* ... */) {
+                       var path = this.env.documentroot;
+
+                       for (var i = 0; i < arguments.length; i++)
+                               path += '/' + arguments[i];
+
+                       var p = path.replace(/\/+$/, '').replace(/\/+/g, '/').split(/\//),
+                           res = [];
+
+                       for (var i = 0; i < p.length; i++)
+                               if (p[i] == '..')
+                                       res.pop();
+                               else if (p[i] != '.')
+                                       res.push(p[i]);
+
+                       return res.join('/');
+               },
+
                /**
                 * Construct a relative URL path from the given prefix and parts.
                 * The resulting URL is guaranteed to only contain the characters
index 43aedf278fec5fc8ecdb4f37aa6c7c75e56d77a1..387c76f8f887c2ab44a01f588a99e6fe71464f24 100644 (file)
@@ -22,6 +22,7 @@
                resource       = resource,
                scriptname     = luci.http.getenv("SCRIPT_NAME"),
                pathinfo       = luci.http.getenv("PATH_INFO"),
+               documentroot   = luci.http.getenv("DOCUMENT_ROOT"),
                requestpath    = luci.dispatcher.context.requestpath,
                dispatchpath   = luci.dispatcher.context.path,
                pollinterval   = luci.config.main.pollinterval or 5,