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>
Sun, 12 Apr 2020 20:51:28 +0000 (22:51 +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>
modules/luci-base/htdocs/luci-static/resources/luci.js
modules/luci-base/luasrc/view/header.htm

index 5984ad184afc758dc627f397f367d771bac445b4..e1aa65a34d1afb3bcd31d9df2418a91463354aec 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 4d30729809e1b2c24fecc6627ae71c2baef50bce..9b49f4129fed9f171aa81ae816c191d9cfb1c452 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,