add filetype filtering

This commit is contained in:
dreamer 2025-11-07 11:38:57 +01:00
parent acd6162402
commit 5383b15095

View File

@ -23,6 +23,9 @@ local data_dir = config.mount
local data_path = data_dir .. request_path local data_path = data_dir .. request_path
-- setup -- setup
local type_image = { jpg=true, jpeg=true, png=true, gif=true }
local type_media = { mp3=true, flac=true, wav=true, mp4=true }
local type_allowed = { jpg=true, jpeg=true, png=true, gif=true, mp3=true, flac=true, wav=true, mp4=true }
local utils = {} local utils = {}
@ -30,11 +33,9 @@ utils['request_path'] = request_path
utils['data_path'] = data_path utils['data_path'] = data_path
-- we want to know if something is an image -- we want to know if something is an image
utils['match_image'] = function( file ) utils['match_ext'] = function( file, ext )
local filext = file:match("[^.]+$") local filext = file:match("[^.]+$")
local extensions = { jpg=true, jpeg=true, png=true, gif=true } if ext[filext:lower()] then
if extensions[filext:lower()] then
return true return true
else else
return false return false
@ -45,9 +46,11 @@ utils['latest_files'] = function( directory )
local i, t, popen = 0, {}, io.popen local i, t, popen = 0, {}, io.popen
local pfile = popen('find "'..directory..'" -type f ! -name \'*.filepart\' -printf \'%C@ %p\n\'| sort -nr | head -7 | cut -f2- -d" "| sed s:"'..directory..'/"::') local pfile = popen('find "'..directory..'" -type f ! -name \'*.filepart\' -printf \'%C@ %p\n\'| sort -nr | head -7 | cut -f2- -d" "| sed s:"'..directory..'/"::')
for filename in pfile:lines() do for filename in pfile:lines() do
if utils.match_ext ( filename, type_allowed ) then
i = i + 1 i = i + 1
t[i] = filename t[i] = filename
end end
end
pfile:close() pfile:close()
return t return t
end end
@ -57,9 +60,9 @@ utils['these_files'] = function( path )
for file in lfs.dir( path ) do for file in lfs.dir( path ) do
if file ~= "." and file ~= ".." and not string.match(file, ".filepart") then if file ~= "." and file ~= ".." and not string.match(file, ".filepart") then
if lfs.attributes( path .. file, "mode" ) == "file" then if lfs.attributes( path .. file, "mode" ) == "file" then
if utils.match_image( file ) then if utils.match_ext( file, type_image ) then
table.insert( images, file ) table.insert( images, file )
else elseif utils.match_ext( file, type_media ) then
table.insert( files, file ) table.insert( files, file )
end end
elseif lfs.attributes( path .. file, "mode" ) == "directory" then elseif lfs.attributes( path .. file, "mode" ) == "directory" then