xrforge-v2/src/page/dir.lua

91 lines
3 KiB
Lua
Raw Normal View History

2026-07-28 08:50:24 +02:00
local util = require("util")
-- todo: clean up code once the code below is no longer in flux
2026-07-28 08:50:24 +02:00
return {
2026-07-28 08:50: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'
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
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
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-30 22:54:44 +02:00
if match(v.path, matchpath) then -- show parent folder if any
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
-- add items from dir
2026-07-30 22:54:44 +02:00
if match(v.parent.path, matchpath) then
local parent = app.cache.items[ v.parent.id ]
res.items[ v.id ] = v
res.parent = parent
if v.thumbnail then
print_r(v.name .. " => " .. v.thumbnail)
end
end
2026-07-30 22:54:44 +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
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'
end
2026-07-30 22:54:44 +02:00
app.cache.url[ urlpath ] = util.templateFile( res.template, data)
end
if redirect then
print("REDIRECT! " .. redirect )
res.status(303)
res.header('Location', redirect )
next()
else
res.status(200)
res.header('content-type','text/html; charset=utf-8')
res.body( app.cache.url[ urlpath ] )
next()
end
else
next()
end
end
}