dflzm
Version:
x
40 lines (33 loc) • 740 B
JavaScript
const fs = require('fs')
const file = {}
file.isFile = async (ctx, path) => new Promise(async resolve => fs.stat(
path,
(e, stat) => {
if (e) {
ctx.logger.error(e.toString())
resolve(false)
}
if (!stat || !stat.isFile()) {
ctx.logger.error(path, ' 文件格式错误')
resolve(false)
}
resolve(true)
},
))
file.read = async (ctx, path) => new Promise(
async (resolve) => {
const isFile = await file.isFile(ctx, path)
if (isFile) {
fs.readFile(path, (e, stat) => {
if (e) {
ctx.logger.error(e.toString())
resolve(false)
}
resolve(stat.toString())
})
} else {
resolve(false)
}
},
)
module.exports = file