diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | logfiles-controller.lua | 21 | ||||
-rw-r--r-- | logfiles-model.lua | 107 | ||||
-rw-r--r-- | logfiles-status-html.lsp | 23 | ||||
-rw-r--r-- | logfiles-view-html.lsp | 8 |
5 files changed, 74 insertions, 86 deletions
@@ -4,7 +4,6 @@ VERSION=0.3.4 APP_DIST=\ alpine-baselayout.roles \ - template-logfiles-download-html.lsp \ alpineversion* \ health* \ hostname* \ diff --git a/logfiles-controller.lua b/logfiles-controller.lua index 3a7900a..400756e 100644 --- a/logfiles-controller.lua +++ b/logfiles-controller.lua @@ -5,14 +5,14 @@ default_action = "status" -- Public methods status = function (self ) - return ({logfile = self.model:get(), url = self.conf.script .. self.conf.prefix .. self.conf.controller} ) + return self.model.get() end delete = function (self) local filetodelete = self.clientdata.name or "" - local me = ({deletestatus = self.model:delete(filetodelete)}) - if (me.deletestatus.errtxt) then - return me + local deletestatus = self.model.delete(filetodelete) + if (deletestatus.errtxt) then + return deletestatus else redirect(self) end @@ -20,12 +20,19 @@ end view = function (self) local filetoview = self.clientdata.name or "" - local content = { logfile=self.model:get_filedetails(filetoview)} - if (filetoview ~= "") and (content.logfile.name ~= nil) then + local content = self.model.get_filedetails(filetoview) + if (filetoview ~= "") and content then return content else redirect(self) end end -download = view +download = function (self) + local filestatus = view(self) + local filecontent = filestatus.value.filecontent + filecontent.label = basename(filestatus.value.filename.value) + self.conf.viewtype = "stream" + + return filecontent +end diff --git a/logfiles-model.lua b/logfiles-model.lua index f00f7f4..6ef6a1d 100644 --- a/logfiles-model.lua +++ b/logfiles-model.lua @@ -1,9 +1,7 @@ --- acf model for displaying logfiles recusivly +-- acf model for displaying logfiles 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 @@ -11,20 +9,15 @@ local function file_info ( path ) local size = st.size local lastmod = st.mtime local file_inuse = io.popen("fuser " .. path ) - local fileinuseresult = file_inuse:read("*a") or "unkown" + local fileinuseresult = file_inuse:read("*a") or "unknown" file_inuse:close() - if fileinuseresult == "" then - fileinuseresult = "Delete" - fileinuseurl = "/delete?name=" .. path - else - fileinuseresult = "in use" - fileinuseurl = nil - end - return lastmod,size,fileinuseresult,fileinuseurl + fileinuseresult = (fileinuseresult == "") + return lastmod,size,fileinuseresult end --- Function to recursively inserts all filenames in a dir into an array +-- Function to recursively insert all filenames in a dir into an array local function recursedir(path, filearray) + filearray = filearray or {} local k,v for k,v in pairs(posix.dir(path) or {}) do -- Ignore files that begins with a '.' @@ -38,6 +31,7 @@ local function recursedir(path, filearray) end end end + return filearray end -- Function to list available files for view/delete @@ -46,76 +40,63 @@ local function list_files ( ... ) local open_files = {} local files = {} local k,v - -- Generate a singe table with all the files + -- Generate a single 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} ) + local lastmod,size,fileinuseresult = file_info(v) + local filename = cfe({ value=v, label="File name" }) + local filesize = cfe({ value=size, label="File size" }) + local mtime = cfe({ value=lastmod, label="File date" }) + local inuse = cfe({ type="boolean", value=fileinuseresult, label="File in use" }) + table.insert ( listed_files , cfe({ type="group", value={filename=filename, filesize=filesize, mtime=mtime, + inuse=inuse, label="File details"} }) ) + end + table.sort(listed_files, function (a,b) return (a.value.filename.value < b.value.filename.value) end ) + return cfe({ type="list", value=listed_files, label="Log files" }) +end + +get_filedetails = function (path) + local filedetails + local available_files = get() + for i,file in ipairs(available_files.value) do + if ( file.value.filename.value == path ) then + filedetails = file + filedetails.value.filecontent = cfe({ type="longtext", value=fs.read_file(path), label="File content" }) + break + end end - table.sort(listed_files, function (a,b) return (a.name < b.name) end ) - return listed_files + return filedetails +end + + +get = function () + -- These folders (and their subfolers) are going to be listed + return list_files( "/var/log", "/tmp/squid/log" ) 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!"} +delete = function (filetodelete) + local deletedfile = cfe({ label="Delete result", value="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 + for i,file in ipairs(available_files.value) do + if ( file.value.filename.value == filetodelete ) then -- Check if file is deletable (or in use) - if ( available_files[k].del ) then + if ( not file.value.inuse.value ) then local status, err = os.remove( filetodelete ) if not ( err ) then - deletedfile = {value="File '" .. filetodelete .. "' has been successfully deleted!", type="text"} + deletedfile.value = "File '"..filetodelete.."' has been successfully deleted!" else - deletedfile = {value=nil, type="text", option=nil, errtxt=err} + deletedfile.errtxt = err end - else end + break 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 - diff --git a/logfiles-status-html.lsp b/logfiles-status-html.lsp index e1cebd9..8ba5113 100644 --- a/logfiles-status-html.lsp +++ b/logfiles-status-html.lsp @@ -1,4 +1,5 @@ <? local view = ... ?> + <html> <body> <h1>Available Logfiles</h1> @@ -6,26 +7,26 @@ <TABLE> <TR style="background:#eee;font-weight:bold;"> <TD style="padding-right:20px;white-space:nowrap;" class="header">Delete</TD> - <TD style="padding-right:20px;white-space:nowrap;" class="header">Save</TD> <TD style="padding-right:20px;white-space:nowrap;" class="header">View</TD> + <TD style="padding-right:20px;white-space:nowrap;" class="header">Save</TD> <TD style="padding-right:20px;white-space:nowrap;text-align:right;" class="header">Size</TD> <TD style="padding-right:20px;white-space:nowrap;" class="header">Last Modified</TD> <TD style="white-space:nowrap;" WIDTH="90%" class="header">File</TD> </TR> -<? for i = 1, table.maxn(view.logfile) do ?> - <TR > +<? for i,file in ipairs(view.value) do ?> + <TR> <TD style="padding-right:20px;white-space:nowrap;"> - <? if view.logfile[i].del then ?> - <?= html.link{value = view.url .. view.logfile[i].del, label=view.logfile[i].inuse } ?> + <? if file.value.inuse.value then ?> + in use <? else ?> - <?= view.logfile[i].inuse ?> + <?= html.link{value = "delete?name="..file.value.filename.value, label="Delete" } ?> <? end ?></TD> - <TD style="padding-right:20px;white-space:nowrap;"><?= html.link{value = view.url .. view.logfile[i].view, label="View" } ?></TD> - <TD style="padding-right:20px;white-space:nowrap;"><?= html.link{value = view.url .. view.logfile[i].save, label="Download" } ?></TD> - <TD style="padding-right:20px;white-space:nowrap;text-align:right"><?= view.logfile[i].size ?></TD> - <TD style="padding-right:20px;white-space:nowrap;"><?= view.logfile[i].lastmod ?></TD> - <TD style="white-space:nowrap;"><?= view.logfile[i].name ?></TD> + <TD style="padding-right:20px;white-space:nowrap;"><?= html.link{value = "view?name="..file.value.filename.value, label="View" } ?></TD> + <TD style="padding-right:20px;white-space:nowrap;"><?= html.link{value = "download?name="..file.value.filename.value, label="Download" } ?></TD> + <TD style="padding-right:20px;white-space:nowrap;text-align:right"><?= file.value.filesize.value ?></TD> + <TD style="padding-right:20px;white-space:nowrap;"><?= file.value.mtime.value ?></TD> + <TD style="white-space:nowrap;"><?= file.value.filename.value ?></TD> </TR> <? end ?> </TABLE> diff --git a/logfiles-view-html.lsp b/logfiles-view-html.lsp index 299e895..4d30f14 100644 --- a/logfiles-view-html.lsp +++ b/logfiles-view-html.lsp @@ -6,15 +6,15 @@ <dl> <dt>File name</dt> -<dd><?= view.logfile.details.path ?></dd> +<dd><?= view.value.filename.value ?></dd> <dt>File size</dt> -<dd><?= view.logfile.details.size ?></dd> +<dd><?= view.value.filesize.value ?></dd> <dt>Last modified</dt> -<dd><?= view.logfile.details.mtime ?></dd> +<dd><?= view.value.mtime.value ?></dd> </dl> <h2>Content</h2> -<textarea name=""><? io.write(view.logfile.value) ?></textarea> +<textarea name=""><? io.write(view.value.filecontent.value) ?></textarea> |