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