local util = require("util") -- todo: clean up code once the code below is no longer in flux return { 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 local redirect = false if not app.cache.url[ urlhash ] or util.count(req.params) > 0 then local matchpath = urlpath if urlpath == '/' then matchpath = rootpath end -- generate janusxr footer for root url foreach( app.cache.items, function(k,v) print("URL=" ..v.url) 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) -- ) --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 -- 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 end end end end end) local data = util.merge({ janusurl = req.param.url, -- query hint to render janus.url in iframe (index.html) items = {}, isroot = isroot, referrer = referrer, 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) 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 -- 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 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 end end end) if data.item then 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) res.header('content-type','text/html') res.body( app.cache.url[ urlhash ] ) next() end else next() end end }