peachy/assets/dirlist.lua

22 lines
692 B
Lua
Raw Normal View History

2024-08-06 04:46:43 +00:00
local r = fm.getRequest()
local function isSpecial(name)
return name == "." or name == ".." or string.sub(name, 1, 1) == "$"
end
local dirpath = r.params.dirpath
local files, directories = {}, {}
for name, kind in assert(unix.opendir(dirpath)) do
if not isSpecial(name) then
local file = { name = name, path = path.join(dirpath, name) }
if kind == unix.DT_REG then
table.insert(files, file)
elseif kind == unix.DT_DIR then
table.insert(directories, file)
end
end
end
local function compareName(f1, f2)
return f1.name < f2.name
end
table.sort(directories, compareName)
table.sort(files, compareName)
fm.render("fragment/dirlist", { files = files, directories = directories })