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
|
2026-08-17 13:42:27 +02:00
|
|
|
local urlpath = req.url
|
|
|
|
|
local isroot = urlpath == "/" or urlpath:match("^"..rootpath.."[/]?$")
|
|
|
|
|
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-08-10 13:16:30 +02:00
|
|
|
local urlhash = urlpath .. util.dump( req.param )
|
|
|
|
|
|
2026-08-17 13:42:27 +02:00
|
|
|
if (isroot or urlpath:match("^"..rootpath))
|
|
|
|
|
and not urlpath:gsub(".*/",""):match("%.") -- no regular file with extension
|
2026-08-11 17:47:40 +02:00
|
|
|
and not req.param.url then
|
2026-07-30 22:54:44 +02:00
|
|
|
|
2026-07-29 22:44:24 +02:00
|
|
|
local redirect = false
|
2026-08-10 13:16:30 +02:00
|
|
|
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
|
2026-07-29 22:44:24 +02:00
|
|
|
foreach( app.cache.items, function(k,v)
|
2026-08-03 19:19:07 +02:00
|
|
|
if req.authenticated or app.plugin.moderation.isAllowed(v) then
|
|
|
|
|
if v.parent then
|
2026-08-17 13:42:27 +02:00
|
|
|
-- trace( "path = " .. util.stripPath(v.path) .. " " ..
|
|
|
|
|
-- "parent = " .. util.stripPath(v.parent.path) .. " " ..
|
|
|
|
|
-- "matchpath = " .. util.stripPath(matchpath)
|
|
|
|
|
-- )
|
2026-08-03 19:19:07 +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
|
2026-08-11 17:47:40 +02:00
|
|
|
trace("-- portal redirect")
|
2026-08-03 19:19:07 +02:00
|
|
|
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
|
2026-07-29 22:44:24 +02:00
|
|
|
end
|
2026-07-28 08:50:24 +02:00
|
|
|
|
2026-08-03 19:19:07 +02:00
|
|
|
-- add items from dir
|
2026-08-11 17:47:40 +02:00
|
|
|
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
|
2026-08-03 19:19:07 +02:00
|
|
|
end
|
2026-07-29 22:44:24 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end)
|
2026-08-11 17:47:40 +02:00
|
|
|
|
2026-07-29 22:44:24 +02:00
|
|
|
local data = util.merge({
|
2026-08-11 17:47:40 +02:00
|
|
|
url = req.param.url, -- query hint to render janus.url in iframe (index.html)
|
2026-07-29 22:44:24 +02:00
|
|
|
items = {},
|
|
|
|
|
parent=res.parent
|
|
|
|
|
},{ opts = app.opts })
|
|
|
|
|
local i = 0
|
|
|
|
|
foreach( res.items, function(k,v)
|
2026-08-17 13:42:27 +02:00
|
|
|
-- add extra metadata for janusxr JML (janusxr.html and dir.html)
|
2026-07-29 22:44:24 +02:00
|
|
|
table.insert(data.items, util.merge({x = i*2, i = i},v) )
|
|
|
|
|
i=i+1
|
|
|
|
|
if v.view3D then
|
2026-08-17 13:42:27 +02:00
|
|
|
local isMarkupExperience = function(v)
|
|
|
|
|
return v.janusxr and (v.janusxr.jml or v.janusxr.aframe)
|
|
|
|
|
end
|
|
|
|
|
-- prioritize markup experience above binary 3D files as experience containers
|
|
|
|
|
if not data.item or (isMarkupExperience(v) and not isMarkupExperience(data.item)) then
|
|
|
|
|
data.item = v
|
|
|
|
|
data.title = data.item.title
|
|
|
|
|
data.parent = v.parent
|
|
|
|
|
data.url = app.opts.baseurl .. util.stripPath(v.url) -- because we are running in iframe
|
|
|
|
|
if v.janusxr and (v.janusxr.jml or v.janusxr.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
|
2026-07-29 22:44:24 +02:00
|
|
|
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-08-17 13:42:27 +02:00
|
|
|
app.pub("dir", nil, {res=res,data=data, urlpath=urlpath, isroot=isroot, isdir=isdir})
|
2026-08-05 10:24:11 +02:00
|
|
|
if not redirect then
|
2026-08-10 13:16:30 +02:00
|
|
|
app.cache.url[ urlhash ] = util.templateFile( res.template, data)
|
2026-08-05 10:24:11 +02:00
|
|
|
end
|
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-08-10 13:16:30 +02:00
|
|
|
res.body( app.cache.url[ urlhash ] )
|
2026-07-29 22:44:24 +02:00
|
|
|
next()
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
next()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
}
|