1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
-- acf model for displaying logfiles recusivly
module (..., package.seeall)
-- no initializer in model - use controller.init for that
-- Function to get detailed information on a specific file.
local function file_info ( path )
-- Load dependencys
split = require("split")
-- Get size, changedate...
local file = io.popen("ls -lh " .. path )
local file_info_result = file:read("*a") or "unknown"
file:close()
local size = rawget(split("%s+", file_info_result),5)
local lastmod = rawget(split("%s+", file_info_result),6) .. " " .. rawget(split("%s+", file_info_result),7) .. " " .. rawget(split("%s+", file_info_result),8)
-- Check if file is in use
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 ( ... )
-- Create a header for the view and create variables
local listed_files = { cfe{inuse="Delete", save="Save", view="View",
size="Size", lastmod="Last Modified", name="File"} }
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
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 = cfe{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 = cfe{value="File '" .. filetodelete .. "' has been successfully deleted!", type="text", option=nil, errtxt=err}
else
deletedfile = cfe{value=nil, type="text", option=nil, errtxt=err}
end
else
end
end
end
return deletedfile
end
local function checkfileforview ( path )
local file = {}
-- Get a list of files that could be viewed
local available_files = get()
filecontent = cfe{value=nil, type="longtext", option=nil, errtxt="You are not allowed to view this file!"}
for k,v in pairs(available_files) do
if ( available_files[k].name == path ) then
local file = io.open( path )
local file_result = file:read("*a") or "unknown"
file:close()
filecontent = cfe{value=file_result, type="text", option=nil, name=basename(path), errtxt=nil}
end
end
return filecontent
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
|