2026-07-28 08:50:24 +02:00
|
|
|
local util = require("util")
|
|
|
|
|
|
2026-07-29 22:44:24 +02:00
|
|
|
-- todo: clean up code once the code below is no longer in flux
|
2026-07-28 08:50:24 +02:00
|
|
|
|
2026-07-29 22:44:24 +02:00
|
|
|
return {
|
2026-07-28 08:50:24 +02:00
|
|
|
|
2026-07-29 22:44:24 +02:00
|
|
|
middleware = function(req,res,next)
|
|
|
|
|
local rootpath = "/" .. app.opts.plugin_diskscan_dirname
|
|
|
|
|
local urlpath = GetPath()
|
2026-07-30 22:54:44 +02:00
|
|
|
res.items = {}
|
|
|
|
|
res.template = res.template or 'page/dir.html'
|
2026-07-29 22:44:24 +02:00
|
|
|
local isdir = urlpath:sub(-1) == "/"
|
|
|
|
|
local nslashes = util.countChar('/',urlpath)
|
|
|
|
|
local match = function(a,b) return util.stripPath(a) == util.stripPath(b) end
|
2026-07-30 22:54:44 +02:00
|
|
|
if urlpath == "/"
|
|
|
|
|
or urlpath:match("^"..rootpath)
|
|
|
|
|
and not urlpath:gsub(".*/",""):match("%.") then
|
|
|
|
|
|
2026-07-29 22:44:24 +02:00
|
|
|
local redirect = false
|
|
|
|
|
if not app.cache.url[ urlpath ] then
|
2026-07-30 22:54:44 +02:00
|
|
|
local matchpath = urlpath
|
|
|
|
|
if urlpath == '/' then matchpath = rootpath end -- generate janusxr footer for root url
|
2026-07-29 22:44:24 +02:00
|
|
|
foreach( app.cache.items, function(k,v)
|
|
|
|
|
if v.parent then
|
|
|
|
|
trace( "path = " .. util.stripPath(v.path) .. " " ..
|
|
|
|
|
"parent = " .. util.stripPath(v.parent.path) .. " " ..
|
2026-07-30 22:54:44 +02:00
|
|
|
"matchpath = " .. util.stripPath(matchpath)
|
2026-07-29 22:44:24 +02:00
|
|
|
)
|
2026-07-30 22:54:44 +02:00
|
|
|
if match(v.path, matchpath) then -- show parent folder if any
|
2026-07-29 22:44:24 +02:00
|
|
|
local parent = app.cache.items[ v.parent.id ]
|
|
|
|
|
if v.portal then
|
|
|
|
|
redirect = v.portal.url
|
|
|
|
|
end
|
|
|
|
|
if match(parent.path, app.opts.plugin_diskscan_dir) then -- add rootfolder if needed
|
|
|
|
|
local root = app.cache.items[ app.opts.plugin_diskscan_dir ]
|
|
|
|
|
--res.items[ root.id ] = root
|
|
|
|
|
end
|
|
|
|
|
end
|
2026-07-28 08:50:24 +02:00
|
|
|
|
2026-07-29 22:44:24 +02:00
|
|
|
-- add items from dir
|
2026-08-02 14:00:56 +02:00
|
|
|
if match(v.parent.path, matchpath) and (v.dir or v.view3D) then
|
2026-07-29 22:44:24 +02:00
|
|
|
local parent = app.cache.items[ v.parent.id ]
|
|
|
|
|
res.items[ v.id ] = v
|
|
|
|
|
res.parent = parent
|
|
|
|
|
end
|
2026-07-30 22:54:44 +02:00
|
|
|
|
2026-07-29 22:44:24 +02:00
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
local data = util.merge({
|
|
|
|
|
html_items='',
|
|
|
|
|
items = {},
|
|
|
|
|
parent=res.parent
|
|
|
|
|
},{ opts = app.opts })
|
|
|
|
|
local i = 0
|
|
|
|
|
foreach( res.items, function(k,v)
|
|
|
|
|
-- position items for janusxr JML (dir.html)
|
|
|
|
|
table.insert(data.items, util.merge({x = i*2, i = i},v) )
|
|
|
|
|
i=i+1
|
|
|
|
|
if v.view3D then
|
2026-07-30 20:53:12 +02:00
|
|
|
data.item = v
|
|
|
|
|
data.title = data.item.title
|
2026-07-29 22:44:24 +02:00
|
|
|
local port = ':' .. GetPort()
|
|
|
|
|
if port == 80 then port = '' end
|
|
|
|
|
data.url = GetScheme() .. '://' .. GetHost() .. port .. v.url
|
|
|
|
|
end
|
|
|
|
|
end)
|
2026-07-30 20:53:12 +02:00
|
|
|
if data.item then
|
2026-07-30 22:54:44 +02:00
|
|
|
res.template = 'page/index.html'
|
2026-07-29 22:44:24 +02:00
|
|
|
end
|
2026-07-30 22:54:44 +02:00
|
|
|
app.cache.url[ urlpath ] = util.templateFile( res.template, data)
|
2026-07-29 22:44:24 +02:00
|
|
|
end
|
|
|
|
|
if redirect then
|
|
|
|
|
print("REDIRECT! " .. redirect )
|
|
|
|
|
res.status(303)
|
|
|
|
|
res.header('Location', redirect )
|
|
|
|
|
next()
|
|
|
|
|
else
|
|
|
|
|
res.status(200)
|
2026-07-31 19:32:19 +02:00
|
|
|
res.header('content-type','text/html')
|
2026-07-29 22:44:24 +02:00
|
|
|
res.body( app.cache.url[ urlpath ] )
|
|
|
|
|
next()
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
next()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
}
|