-- acf model for displaying logfiles recusivly module (..., package.seeall) require("fs") -- no initializer in model - use controller.init for that -- Function to get detailed information on a specific file. local function file_info ( path ) -- Check if file is in use local st = fs.stat(path) local size = st.size local lastmod = st.mtime local file_inuse = io.popen("fuser " .. path ) local fileinuseresult = file_inuse:read("*a") or "unkown" file_inuse:close() if fileinuseresult == "" then fileinuseresult = "Delete" fileinuseurl = "/delete?name=" .. path else fileinuseresult = "in use" fileinuseurl = nil end return lastmod,size,fileinuseresult,fileinuseurl end -- Function to recursively inserts all filenames in a dir into an array local function recursedir(path, filearray) local k,v for k,v in pairs(posix.dir(path) or {}) do -- Ignore files that begins with a '.' if not string.match(v, "^%.") then local f = path .. "/" .. v -- If subfolder exists, list files in this subfolder if (posix.stat(f).type == "directory") then recursedir(f, filearray) else table.insert(filearray, f) end end end end -- Function to list available files for view/delete local function list_files ( ... ) local listed_files = {} local open_files = {} local files = {} local k,v -- Generate a singe table with all the files for k,v in pairs{...} do recursedir(v, files) end -- Loop through each file and present its info for k,v in pairs(files) do -- Get info on this specific file and put it in a table local lastmod,size,fileinuseresult,fileinuseurl = file_info(v) table.insert ( listed_files , cfe{inuse=fileinuseresult, del=fileinuseurl, save="/download?name=" .. v, view="/view?name=" .. v, size=size, lastmod=lastmod, name=v, type="", id=k} ) end table.sort(listed_files, function (a,b) return (a.name < b.name) end ) return listed_files end -- Function to check if a file is deletable, and if it is, then delete it. local function checkfilefordelete ( filetodelete ) local deletedfile = {} deletedfile = {value=nil, type="text", option=nil, errtxt="File '" .. filetodelete .. "' has not been deleted!"} -- Get a list of files that could be deleted local available_files = get() for k,v in pairs(available_files) do if ( available_files[k].name == filetodelete ) then -- Check if file is deletable (or in use) if ( available_files[k].del ) then local status, err = os.remove( filetodelete ) if not ( err ) then deletedfile = {value="File '" .. filetodelete .. "' has been successfully deleted!", type="text"} else deletedfile = {value=nil, type="text", option=nil, errtxt=err} end else end end end return deletedfile end local function checkfileforview ( path ) local filecontent = nil local available_files = get() for k,v in pairs(available_files) do if ( available_files[k].name == path ) then filecontent = path end end return filecontent end function get_filedetails(self,path) local filedetails = {} if (checkfileforview(path)) then filedetails.details = fs.stat(path) filedetails.value = fs.read_file(path) filedetails.name = basename(path) end return filedetails end get = function (self) -- These folders (and their subfolers) are going to be listed return list_files( "/var/log", "/tmp/squid/log" ) end delete = function (self,filetodelete) return checkfilefordelete( filetodelete ) end view = function (self,filetoview) return checkfileforview( filetoview ) end