local util = require("util") -- todo: clean up code once the code below is no longer in flux return { middleware = function(req,res,next) local tile = require('page/partial/tile') local rootpath = "/" .. app.opts.plugin_diskscan_dirname local urlpath = GetPath() local isdir = urlpath:sub(-1) == "/" local nslashes = util.countChar('/',urlpath) res.items = {} local match = function(a,b) return util.stripPath(a) == util.stripPath(b) end if urlpath:match("^"..rootpath) and not urlpath:gsub(".*/",""):match(".") then local redirect = false if not app.cache.url[ urlpath ] then foreach( app.cache.items, function(k,v) if v.parent then trace( "path = " .. util.stripPath(v.path) .. " " .. "parent = " .. util.stripPath(v.parent.path) .. " " .. "urlpath = " .. util.stripPath(urlpath) ) if match(v.path, urlpath) 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, urlpath) 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 --if parent.parent then -- res.items[ v.id ] = v --else -- res.items[ parent.id ] = parent --end end end end) local data = util.merge({ html_items='', items = {}, parent=res.parent },{ opts = app.opts }) data.tile = tile 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 else data.html_items = data.html_items .. tile({item=v}) end end) if data.item then app.cache.url[ urlpath ] = util.templateFile( "page/item.html", data) else app.cache.url[ urlpath ] = util.templateFile( "page/dir.html", 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; charset=utf-8') res.body( app.cache.url[ urlpath ] ) next() end else next() end end }