xrforge-v2/src/page/dir.lua

119 lines
4.7 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 = req.url
local isroot = urlpath == "/" or urlpath:match("^"..rootpath.."[/]?$")
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
local urlhash = urlpath .. util.dump( req.param )
local referrer = util.baseURLify( (isroot and not req.param.url) and "" or urlpath )
if (isroot or urlpath:match("^"..rootpath))
and not urlpath:gsub(".*/",""):match("%.") -- no regular file with extension
then
2026-07-30 22:54:44 +02:00
local redirect = false
if not app.cache.url[ urlhash ] or util.count(req.params) > 0 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)
2026-08-24 13:29:35 +02:00
print("URL=" ..v.url)
2026-08-03 19:19:07 +02:00
if req.authenticated or app.plugin.moderation.isAllowed(v) then
if v.parent then
-- trace( "path = " .. util.stripPath(v.path) .. " " ..
-- "parent = " .. util.stripPath(v.parent.path) .. " " ..
-- "matchpath = " .. util.stripPath(matchpath)
-- )
2026-08-18 15:30:10 +02:00
--if match(v.path, matchpath) then -- add parent folder if any
--res.items[ v.parent.id ] = v.parent
-- if match(v.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-08-03 19:19:07 +02:00
-- add items from dir
if match(v.parent.path, matchpath) then
if v.dir or v.view3D then
local parent = app.cache.items[ v.parent.id ]
res.items[ v.id ] = v
res.parent = parent
end
if isroot then
referrer = app.opts.plugin_federation_parent_url
else
referrer = v.parent.parent.url
2026-08-18 15:30:10 +02:00
end
2026-08-03 19:19:07 +02:00
end
end
end
end)
local data = util.merge({
2026-08-24 13:29:35 +02:00
janusurl = req.param.url, -- query hint to render janus.url in iframe (index.html)
items = {},
isroot = isroot,
referrer = referrer,
2026-08-18 15:30:10 +02:00
parent=res.parent,
template=res.template
},{ opts = app.opts })
local i = 0
foreach( res.items, function(k,v)
-- add extra metadata for janusxr JML (janusxr.html and dir.html)
table.insert(data.items, util.merge({x = i*2, i = i},v) )
i=i+1
if v.view3D then
local isMarkupExperience = function(v)
2026-08-24 13:29:35 +02:00
return v.janusxr and (v.janusxr.jml or v.janusxr.wrap)
end
-- prioritize markup experience above binary 3D files as experience containers
if not data.item or (isMarkupExperience(v) and not isMarkupExperience(data.item)) then
2026-08-18 15:30:10 +02:00
-- update referrer url
data.referrer = util.baseURLify(v.view3D and v.parent.parent.url or v.parent.url)
data.item = v
data.title = data.item.title
data.parent = v.parent
2026-08-24 13:29:35 +02:00
data.janusurl = (v.janusxr and v.janusxr.wrap) and
app.opts.baseurl .. util.stripPath(v.parent.url)
or
app.opts.baseurl .. util.stripPath(v.url)
if v.janusxr and (v.janusxr.jml or v.janusxr.wrap and v.janusxr.wrap.aframe) then
-- what to do here? no generated portals?
data.janusxr = app.tpl( util.readfile(v.path), data )
end
2026-08-02 15:17:43 +02:00
end
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
app.pub("dir", nil, {res=res,data=data, urlpath=urlpath, isroot=isroot, isdir=isdir})
if not redirect then
app.cache.url[ urlhash ] = util.templateFile( res.template, data)
end
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')
res.body( app.cache.url[ urlhash ] )
next()
end
else
next()
end
end
}