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 = GetPath() 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 if urlpath == "/" or urlpath:match("^"..rootpath) and not urlpath:gsub(".*/",""):match("%.") then local redirect = false if not app.cache.url[ urlpath ] then 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) .. " " .. "matchpath = " .. util.stripPath(matchpath) ) 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 -- add items from dir if match(v.parent.path, matchpath) then local parent = app.cache.items[ v.parent.id ] res.items[ v.id ] = v res.parent = parent end 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 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) if data.item then res.template = 'page/index.html' end 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') res.body( app.cache.url[ urlpath ] ) next() end else next() end end }