luci-base: luci.js: add LuCI.fspath() helper
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>
This commit is contained in:
parent
e523bfd879
commit
fde144c9be
2 changed files with 32 additions and 0 deletions
|
@ -2596,6 +2596,37 @@
|
||||||
*/
|
*/
|
||||||
env: {},
|
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.
|
* Construct a relative URL path from the given prefix and parts.
|
||||||
* The resulting URL is guaranteed to only contain the characters
|
* The resulting URL is guaranteed to only contain the characters
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
resource = resource,
|
resource = resource,
|
||||||
scriptname = luci.http.getenv("SCRIPT_NAME"),
|
scriptname = luci.http.getenv("SCRIPT_NAME"),
|
||||||
pathinfo = luci.http.getenv("PATH_INFO"),
|
pathinfo = luci.http.getenv("PATH_INFO"),
|
||||||
|
documentroot = luci.http.getenv("DOCUMENT_ROOT"),
|
||||||
requestpath = luci.dispatcher.context.requestpath,
|
requestpath = luci.dispatcher.context.requestpath,
|
||||||
dispatchpath = luci.dispatcher.context.path,
|
dispatchpath = luci.dispatcher.context.path,
|
||||||
pollinterval = luci.config.main.pollinterval or 5,
|
pollinterval = luci.config.main.pollinterval or 5,
|
||||||
|
|
Loading…
Reference in a new issue