drmr.nl/code/drmr/utils.lua
2025-10-12 21:35:48 +02:00

56 lines
1.5 KiB
Lua

local lfs = require("lfs_ffi")
local cjson = require("cjson")
local utils = {}
-- we want to list the contents of a dir
function utils:dirlist( path )
local files, dirs, images, audio = {}, {}, {}, {}
for file in lfs.dir( path ) do
local filepath = path .. "/" .. file
local attribute = lfs.attributes( filepath, "mode" ) or "nil"
if file ~= "." and file ~= ".." and not string.match(file, ".filepart") then
if lfs.attributes( filepath, "mode" ) == "file" then
if utils:match_image( file ) then
table.insert( images, file )
elseif utils:match_audio( file) then
table.insert( audio, file )
else
table.insert( files, file )
end
elseif lfs.attributes( filepath, "mode" ) == "directory" then
table.insert( dirs, file )
end
end
end
table.sort( images )
table.sort( files )
table.sort( dirs )
table.sort( audio )
local stuff = {
files = files,
dirs = dirs,
images = images,
audio = audio
}
return stuff
end
-- open a json file and return the table
function utils:opendata( path )
local data = io.open(path)
if data then
local data_file = data:read("a")
data:close()
local data_decode = cjson.decode(data_file)
return data_decode
else
return nil
end
end
return utils