utils.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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)
  76. return string.match(filepath,"[^/]+$")
  77. end
  78. -- 删除文件
  79. M.delete_file = function(filepath)
  80. if os.remove(fw.website_dir()..filepath) ~= true then
  81. return false
  82. end
  83. return true
  84. end
  85. -- 读取文件内容
  86. M.read_file = function(filepath)
  87. local file = io.open(filepath, "rb")
  88. if not file then
  89. return nil, "无法打开文件: " .. tostring(filepath)
  90. end
  91. local content = file:read("*all")
  92. file:close()
  93. return content
  94. end
  95. -- 保存内容到文件
  96. M.save_file = function(filepath, content)
  97. local file = io.open(filepath, "wb")
  98. if not file then
  99. return false, "无法打开文件: " .. tostring(filepath)
  100. end
  101. file:write(content)
  102. file:close()
  103. return true
  104. end
  105. -- 是否存在文件
  106. M.exists_file = function(filepath)
  107. local file = io.open(filepath, "rb")
  108. if file then
  109. file:close()
  110. return true
  111. else
  112. return false
  113. end
  114. end
  115. -- 取近N个月时间
  116. M.recent_months = function(n)
  117. local mons = {}
  118. local function format_time(y, m, d, h, i, s)
  119. return string.format("%04d-%02d-%02d %02d:%02d:%02d", y, m, d, h, i, s)
  120. end
  121. local now = os.time()
  122. local cur = os.date("*t", now)
  123. for i = n-1, 0, -1 do
  124. local year = cur.year
  125. local month = cur.month - i
  126. while month <= 0 do
  127. month = month + 12
  128. year = year - 1
  129. end
  130. -- 获取该月第一天与最后一天
  131. local first_day = format_time(year, month, 1, 0, 0, 0)
  132. local next_month = month + 1
  133. local next_year = year
  134. if next_month > 12 then
  135. next_month = 1
  136. next_year = year + 1
  137. end
  138. -- next_month 1号的前一天就是当前月最后一天
  139. local last_day_ts = os.time{year=next_year, month=next_month, day=1, hour=0, min=0, sec=0} - 1
  140. local last_day_tm = os.date("*t", last_day_ts)
  141. local last_day = format_time(last_day_tm.year, last_day_tm.month, last_day_tm.day, 23, 59, 59)
  142. local month_str = string.format("%04d-%02d", year, month)
  143. table.insert(mons, month_str)
  144. end
  145. return mons
  146. end
  147. M.recent_months2 = function(n)
  148. local mons = {}
  149. local function format_time(y, m, d, h, i, s)
  150. return string.format("%04d-%02d-%02d %02d:%02d:%02d", y, m, d, h, i, s)
  151. end
  152. local now = os.time()
  153. local cur = os.date("*t", now)
  154. for i = n-1, 0, -1 do
  155. local year = cur.year
  156. local month = cur.month - i
  157. while month <= 0 do
  158. month = month + 12
  159. year = year - 1
  160. end
  161. -- 获取该月第一天与最后一天
  162. local first_day = format_time(year, month, 1, 0, 0, 0)
  163. local next_month = month + 1
  164. local next_year = year
  165. if next_month > 12 then
  166. next_month = 1
  167. next_year = year + 1
  168. end
  169. -- next_month 1号的前一天就是当前月最后一天
  170. local last_day_ts = os.time{year=next_year, month=next_month, day=1, hour=0, min=0, sec=0} - 1
  171. local last_day_tm = os.date("*t", last_day_ts)
  172. local last_day = format_time(last_day_tm.year, last_day_tm.month, last_day_tm.day, 23, 59, 59)
  173. local month_str = string.format("%04d-%02d", year, month)
  174. table.insert(mons, {
  175. month = month_str,
  176. start_time = first_day,
  177. end_time = last_day
  178. })
  179. end
  180. return mons
  181. end
  182. -- 十六进制转字节数组
  183. M.hex_to_bytes = function(hex_str)
  184. -- 移除所有非十六进制字符
  185. hex_str = hex_str:gsub("[^%x]", ""):upper()
  186. -- 补0使长度为偶数
  187. if #hex_str % 2 == 1 then
  188. hex_str = "0" .. hex_str
  189. end
  190. -- 使用 gsub 一次性转换
  191. return (hex_str:gsub("(%x%x)", function(hex)
  192. return string.char(tonumber(hex, 16))
  193. end))
  194. end
  195. --[[
  196. 还原 Redis 字符串(去转义)
  197. @param value 被转义的字符串
  198. @return 返回还原后的字符串
  199. ]]
  200. M.unescape_value = function(value)
  201. if value == nil then
  202. return nil
  203. end
  204. local str = tostring(value)
  205. -- 如果是用双引号包住的,去掉包裹并恢复转义
  206. if #str >= 2 and string.sub(str,1,1) == "\"" and string.sub(str,-1,-1) == "\"" then
  207. str = string.sub(str,2,-2)
  208. str = string.gsub(str, "\\\"", "\"")
  209. str = string.gsub(str, "\\\\", "\\")
  210. return str
  211. end
  212. return str
  213. end
  214. -- 是否为静态资源扩展名
  215. M.is_static_ext_not_html = function(ext)
  216. local static_exts = {
  217. "jpg",
  218. "jpeg",
  219. "png",
  220. "gif",
  221. "bmp",
  222. "ico",
  223. "pdf",
  224. "doc",
  225. "docx",
  226. "xls",
  227. "xlsx",
  228. "ppt",
  229. "pptx",
  230. "txt",
  231. "css",
  232. "js",
  233. "json",
  234. "xml",
  235. "yaml",
  236. "yml",
  237. "zip",
  238. "rar",
  239. "7z",
  240. "tar",
  241. "gz",
  242. "bz2",
  243. "xz",
  244. "mp3",
  245. "wav",
  246. "ogg",
  247. "aac",
  248. "m4a",
  249. "mp4",
  250. "avi",
  251. "mov",
  252. "wmv",
  253. "flv",
  254. "webm",
  255. "mkv",
  256. "avi",
  257. "mov",
  258. "wmv",
  259. "flv",
  260. "webm",
  261. "mkv",
  262. }
  263. for _, v in ipairs(static_exts) do
  264. if v == ext then
  265. return true
  266. end
  267. end
  268. return false
  269. end
  270. -- 遍历目录
  271. M.traverse_dir = function(dirpath)
  272. local files = {}
  273. for file in lfs.dir(dirpath) do
  274. if file ~= "." and file ~= ".." then
  275. table.insert(files, file)
  276. end
  277. end
  278. return files
  279. end
  280. return M