commit 37d2ed7efe54fd52c34cba91ca4f05aef81c67ea
parent c6ad905027dab564d9fd4f31130bb8ed3d8641a8
Author: Hugo Soucy <hugo@soucy.cc>
Date: Tue, 30 Mar 2021 08:48:00 -0400
Add an exists
Diffstat:
1 file changed, 26 insertions(+), 7 deletions(-)
diff --git a/satelito/file.lua b/satelito/file.lua
@@ -1,9 +1,24 @@
-- @module file
local file = {}
+--
local lfs = require 'lfs'
local mimetypes = require 'mimetypes'
local lume = require 'satelito.lib.lume.lume'
---
+
+-- Check if a file exists
+function file.exists(filepath)
+ local _file = io.open(filepath, 'r')
+ local is_file = _file ~= nil and true or false
+
+ if _file ~= nil then
+ _file:close()
+
+ return filepath
+ else
+ return false
+ end
+end
+
-- Open & read the content of a file
function file.read(filepath)
local _file = assert(io.open(filepath, 'r'))
@@ -46,9 +61,9 @@ end
-- Get the modified file from a list of paths
function file.get_lastmodified(filelist)
return math.max(table.unpack(lume.map(filelist,
- function(fl)
- return lfs.attributes(fl).modification
- end
+ function(fl)
+ return lfs.attributes(fl).modification
+ end
)))
end
@@ -65,12 +80,16 @@ end
-- Create a dirtree
function file.mkdir(filepath)
local sep, pStr = package.config:sub(1, 1), ''
+ -- Check if the filepath is absolute or relative
+ local _filepath = (string.find(filepath, lfs.currentdir())
+ and filepath
+ or lfs.currentdir() .. '/' .. filepath)
- for dir in filepath:gmatch('[^' .. sep .. ']+') do
+ --
+ for dir in _filepath:gmatch('[^' .. sep .. ']+') do
pStr = pStr .. sep .. dir
-
lfs.mkdir(pStr)
end
end
-
+--
return file