moving log file if its not from today

This commit is contained in:
mrfry 2021-07-02 20:25:17 +02:00
parent 2d6baa2549
commit 28af84148e
2 changed files with 35 additions and 0 deletions

View file

@ -51,6 +51,22 @@ const usersDBPath = './data/dbs/users.db'
const logFile = logger.logDir + logger.logFileName const logFile = logger.logDir + logger.logFileName
const vlogFile = logger.vlogDir + logger.logFileName const vlogFile = logger.vlogDir + logger.logFileName
function moveLogIfNotFromToday(path, to) {
if (utils.FileExists(path)) {
const today = new Date()
const stat = utils.statFile(path)
if (
today.getFullYear() !== stat.mtime.getFullYear() ||
today.getMonth() !== stat.mtime.getMonth() ||
today.getDate() !== stat.mtime.getDate()
) {
utils.renameFile(path, to + utils.GetDateString(stat.mtime))
}
}
}
moveLogIfNotFromToday(logFile, logger.logDir)
moveLogIfNotFromToday(vlogFile, logger.vlogDir)
idStats.Load() idStats.Load()
logger.Load() logger.Load()

View file

@ -13,6 +13,8 @@ export default {
formatUrl: formatUrl, formatUrl: formatUrl,
deleteFile: deleteFile, deleteFile: deleteFile,
uploadFile: uploadFile, uploadFile: uploadFile,
statFile: statFile,
renameFile: renameFile,
} }
import fs from 'fs' import fs from 'fs'
@ -232,3 +234,20 @@ function uploadFile(req: Request, path: string): Promise<any> {
} }
}) })
} }
function statFile(file: string): any {
if (FileExists(file)) {
return fs.statSync(file)
} else {
return null
}
}
function renameFile(oldPath: string, newPath: string): any {
if (FileExists(oldPath)) {
fs.renameSync(oldPath, newPath)
return newPath
} else {
return null
}
}