feat: 添加自動刪除功能
This commit is contained in:
@@ -1 +1,2 @@
|
||||
path: string
|
||||
deleteAfter: number
|
||||
15
index.ts
15
index.ts
@@ -2,15 +2,15 @@ import fs from 'fs'
|
||||
import got, {HTTPError, Options} from 'got'
|
||||
import YAML from 'yaml'
|
||||
|
||||
import {IrequestData} from './types.js'
|
||||
import {Iconfig, IrequestData} from './types.js'
|
||||
|
||||
const path: string = YAML.parse(fs.readFileSync('../config.yml', 'utf-8')).path
|
||||
const config: Iconfig = YAML.parse(fs.readFileSync('../config.yml', 'utf-8'))
|
||||
const lock: string[] = []
|
||||
|
||||
fs.watch(path, async function (event, filename) {
|
||||
fs.watch(config.path, async function (event, filename) {
|
||||
if (!(filename && event === 'change') || lock.includes(filename)) return
|
||||
lock.push(filename)
|
||||
const jpath: string = `${path}/${filename}`
|
||||
const jpath: string = `${config.path}/${filename}`
|
||||
const requestData: IrequestData = JSON.parse(fs.readFileSync(jpath).toString())
|
||||
if (requestData.Processed === true) return
|
||||
const method: any = requestData.Request.Mode.toUpperCase()
|
||||
@@ -44,6 +44,9 @@ fs.watch(path, async function (event, filename) {
|
||||
requestData.Processed = true
|
||||
fs.writeFileSync(jpath, JSON.stringify(requestData))
|
||||
setTimeout(() => {
|
||||
lock.splice(lock.indexOf(filename), 1)
|
||||
}, 300)
|
||||
fs.unlinkSync(jpath)
|
||||
setTimeout(() => {
|
||||
lock.splice(lock.indexOf(filename), 1)
|
||||
}, 300)
|
||||
}, config.deleteAfter * 1000)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user