utils.lua 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. local lfs = require("lfs")
  2. local M = {}
  3. -- 创建目录
  4. M.create_dir = function(dirpath)
  5. -- 检查操作系统类型
  6. local os_type = package.config:sub(1,1)
  7. if os_type == "\\" then
  8. -- Windows 系统
  9. os.execute("mkdir \"" .. dirpath:gsub("/", "\\") .. "\" /p")
  10. else
  11. -- Unix/Linux/Mac 系统
  12. os.execute("mkdir -p \"" .. dirpath .. "\"")
  13. end
  14. end
  15. -- 转换为整数
  16. M.tointeger = function(data)
  17. local num = tonumber(data)
  18. if num == nil then
  19. return nil
  20. end
  21. return math.floor(num)
  22. end
  23. -- 转为时间戳
  24. M.to_timestamp = function(time_str,pattern)
  25. if pattern == nil then
  26. pattern = "(%d+)%-(%d+)%-(%d+)%s+(%d+):(%d+):(%d+)"
  27. end
  28. local y, m, d, h, min, s = time_str:match(pattern)
  29. local timestamp = os.time({
  30. year = tonumber(y),
  31. month = tonumber(m),
  32. day = tonumber(d),
  33. hour = tonumber(h),
  34. min = tonumber(min),
  35. sec = tonumber(s)
  36. })
  37. return timestamp
  38. end
  39. -- 取扩展名
  40. M.ext = function(filepath)
  41. -- 取扩展名
  42. return string.match(filepath,"%.([^.]+)$")
  43. end
  44. -- 复制文件
  45. -- 增加第三个参数 replace,是否替换目标文件,默认为 false
  46. M.copy_file = function(src, dst, replace)
  47. replace = replace or false
  48. -- 检查目标文件是否存在
  49. local dst_file = io.open(dst, "r")
  50. if dst_file ~= nil then
  51. dst_file:close()
  52. if not replace then
  53. return true
  54. end
  55. end
  56. -- 打开源文件
  57. local file = io.open(src, "rb")
  58. if not file then
  59. print("ERR 2,src:",src)
  60. return false
  61. end
  62. local content = file:read("*all")
  63. file:close()
  64. -- 写入目标文件
  65. local file = io.open(dst, "wb")
  66. if not file then
  67. print("ERR 3")
  68. return false
  69. end
  70. file:write(content)
  71. file:close()
  72. return true
  73. end
  74. -- 取路径文件名(带扩展名)
  75. M.filename = function(filepath, have_ext)
  76. local filename = string.match(filepath, "[^/]+$")
  77. if have_ext == false then
  78. local name = string.match(filename, "^(.*)%.") -- get name before last dot
  79. if name ~= nil then
  80. return name
  81. else
  82. return filename -- no extension
  83. end
  84. end
  85. return filename
  86. end
  87. -- 删除文件
  88. M.delete_file = function(filepath)
  89. if os.remove(fw.website_dir()..filepath) ~= true then
  90. return false
  91. end
  92. return true
  93. end
  94. -- 读取文件内容
  95. M.read_file = function(filepath)
  96. local file = io.open(filepath, "rb")
  97. if not file then
  98. return nil, "无法打开文件: " .. tostring(filepath)
  99. end
  100. local content = file:read("*all")
  101. file:close()
  102. return content
  103. end
  104. -- 保存内容到文件
  105. M.save_file = function(filepath, content)
  106. local file = io.open(filepath, "wb")
  107. if not file then
  108. return false, "无法打开文件: " .. tostring(filepath)
  109. end
  110. file:write(content)
  111. file:close()
  112. return true
  113. end
  114. -- 是否存在文件
  115. M.exists_file = function(filepath)
  116. local file = io.open(filepath, "rb")
  117. if file then
  118. file:close()
  119. return true
  120. else
  121. return false
  122. end
  123. end
  124. -- 取近N个月时间
  125. M.recent_months = function(n)
  126. local mons = {}
  127. local function format_time(y, m, d, h, i, s)
  128. return string.format("%04d-%02d-%02d %02d:%02d:%02d", y, m, d, h, i, s)
  129. end
  130. local now = os.time()
  131. local cur = os.date("*t", now)
  132. for i = n-1, 0, -1 do
  133. local year = cur.year
  134. local month = cur.month - i
  135. while month <= 0 do
  136. month = month + 12
  137. year = year - 1
  138. end
  139. -- 获取该月第一天与最后一天
  140. local first_day = format_time(year, month, 1, 0, 0, 0)
  141. local next_month = month + 1
  142. local next_year = year
  143. if next_month > 12 then
  144. next_month = 1
  145. next_year = year + 1
  146. end
  147. -- next_month 1号的前一天就是当前月最后一天
  148. local last_day_ts = os.time{year=next_year, month=next_month, day=1, hour=0, min=0, sec=0} - 1
  149. local last_day_tm = os.date("*t", last_day_ts)
  150. local last_day = format_time(last_day_tm.year, last_day_tm.month, last_day_tm.day, 23, 59, 59)
  151. local month_str = string.format("%04d-%02d", year, month)
  152. table.insert(mons, month_str)
  153. end
  154. return mons
  155. end
  156. M.recent_months2 = function(n)
  157. local mons = {}
  158. local function format_time(y, m, d, h, i, s)
  159. return string.format("%04d-%02d-%02d %02d:%02d:%02d", y, m, d, h, i, s)
  160. end
  161. local now = os.time()
  162. local cur = os.date("*t", now)
  163. for i = n-1, 0, -1 do
  164. local year = cur.year
  165. local month = cur.month - i
  166. while month <= 0 do
  167. month = month + 12
  168. year = year - 1
  169. end
  170. -- 获取该月第一天与最后一天
  171. local first_day = format_time(year, month, 1, 0, 0, 0)
  172. local next_month = month + 1
  173. local next_year = year
  174. if next_month > 12 then
  175. next_month = 1
  176. next_year = year + 1
  177. end
  178. -- next_month 1号的前一天就是当前月最后一天
  179. local last_day_ts = os.time{year=next_year, month=next_month, day=1, hour=0, min=0, sec=0} - 1
  180. local last_day_tm = os.date("*t", last_day_ts)
  181. local last_day = format_time(last_day_tm.year, last_day_tm.month, last_day_tm.day, 23, 59, 59)
  182. local month_str = string.format("%04d-%02d", year, month)
  183. table.insert(mons, {
  184. month = month_str,
  185. start_time = first_day,
  186. end_time = last_day
  187. })
  188. end
  189. return mons
  190. end
  191. -- 十六进制转字节数组
  192. M.hex_to_bytes = function(hex_str)
  193. -- 移除所有非十六进制字符
  194. hex_str = hex_str:gsub("[^%x]", ""):upper()
  195. -- 补0使长度为偶数
  196. if #hex_str % 2 == 1 then
  197. hex_str = "0" .. hex_str
  198. end
  199. -- 使用 gsub 一次性转换
  200. return (hex_str:gsub("(%x%x)", function(hex)
  201. return string.char(tonumber(hex, 16))
  202. end))
  203. end
  204. --[[
  205. 还原 Redis 字符串(去转义)
  206. @param value 被转义的字符串
  207. @return 返回还原后的字符串
  208. ]]
  209. M.unescape_value = function(value)
  210. if value == nil then
  211. return nil
  212. end
  213. local str = tostring(value)
  214. -- 如果是用双引号包住的,去掉包裹并恢复转义
  215. if #str >= 2 and string.sub(str,1,1) == "\"" and string.sub(str,-1,-1) == "\"" then
  216. str = string.sub(str,2,-2)
  217. str = string.gsub(str, "\\\"", "\"")
  218. str = string.gsub(str, "\\\\", "\\")
  219. return str
  220. end
  221. return str
  222. end
  223. -- 是否为静态资源扩展名
  224. M.is_static_ext_not_html = function(ext)
  225. local static_exts = {
  226. "jpg",
  227. "jpeg",
  228. "png",
  229. "gif",
  230. "bmp",
  231. "ico",
  232. "pdf",
  233. "doc",
  234. "docx",
  235. "xls",
  236. "xlsx",
  237. "ppt",
  238. "pptx",
  239. "txt",
  240. "css",
  241. "js",
  242. "json",
  243. "xml",
  244. "yaml",
  245. "yml",
  246. "zip",
  247. "rar",
  248. "7z",
  249. "tar",
  250. "gz",
  251. "bz2",
  252. "xz",
  253. "mp3",
  254. "wav",
  255. "ogg",
  256. "aac",
  257. "m4a",
  258. "mp4",
  259. "avi",
  260. "mov",
  261. "wmv",
  262. "flv",
  263. "webm",
  264. "mkv",
  265. "avi",
  266. "mov",
  267. "wmv",
  268. "flv",
  269. "webm",
  270. "mkv",
  271. }
  272. for _, v in ipairs(static_exts) do
  273. if v == ext then
  274. return true
  275. end
  276. end
  277. return false
  278. end
  279. -- 遍历目录
  280. M.traverse_dir = function(dirpath)
  281. local files = {}
  282. for file in lfs.dir(dirpath) do
  283. if file ~= "." and file ~= ".." then
  284. table.insert(files, file)
  285. end
  286. end
  287. return files
  288. end
  289. return M