-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.lua
More file actions
59 lines (51 loc) · 1.52 KB
/
util.lua
File metadata and controls
59 lines (51 loc) · 1.52 KB
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
-- Common utility function
-- Author: Jeffry L <[email protected]>
local m = {}
m.__index = m
-- get the filename given full path
-- @param string path
-- @return string filename
function m.get_filename(path)
if type(path) ~= 'string' then error('Invalid filename') end
return string.match(path, '[%w%d%s%-%._]+%.docx')
end
-- get directory name
function m.get_dirname(path)
local filename = m.get_filename(path)
local start_pos = string.find(path, filename, 1, true) - 2
local dirname = string.sub(path, 1, start_pos)
--ngx.log(ngx.ERR, "filename:" .. filename .. " dirname: " .. dirname)
return dirname
end
-- check if file exists
-- @param string full path to filename
-- @return boolean
function m.file_exists(filename)
if type(filename)~="string" then return false end
if not lfs.attributes(filename) then return false end
return true
end
-- make file writeable
function m.file_set_writeable(file)
return os.execute('chmod +w "' .. file .. '"')
end
-- check if directory/file writeable
function m.file_writeable(file)
if not m.file_exists(file) then return false end
local stat = lfs.attributes(file)
if not stat then error(file .. "do not exists") end
local perm = string.sub(stat.permissions, 8, 8)
return perm == 'w'
end
-- escape xml chars
function m.xml_escape_chars(str)
local xml_chars = {
['<'] = "<",
['>'] = ">",
['&'] = "&",
["'"] = "'", -- single quote
['"'] = """ -- double quote
}
return string.gsub(str, '[<>&\'"]', xml_chars)
end
return m