feat: 添加自動刪除功能

This commit is contained in:
2025-01-27 17:46:41 +08:00
parent b78ca6a793
commit 82ab65fa02
3 changed files with 16 additions and 7 deletions

View File

@@ -1 +1,2 @@
path: string path: string
deleteAfter: number

View File

@@ -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()
@@ -43,7 +43,10 @@ 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(() => {
fs.unlinkSync(jpath)
setTimeout(() => { setTimeout(() => {
lock.splice(lock.indexOf(filename), 1) lock.splice(lock.indexOf(filename), 1)
}, 300) }, 300)
}, config.deleteAfter * 1000)
}) })

View File

@@ -12,3 +12,8 @@ export interface IrequestData {
} }
Processed?: boolean Processed?: boolean
} }
export interface Iconfig {
path: string
deleteAfter: number
}