| import fs from 'node:fs' |
| import path from 'node:path' |
| import YAML from 'yaml' |
| import multer from 'multer' |
| import rateLimit from 'express-rate-limit' |
|
|
| export default class plugin { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| constructor (data = {}) { |
| |
| this.name = data.name || '' |
| |
| this.dsc = data.dsc || '' |
| |
| this.priority = data.priority || 5 |
| |
| this.route = data.route || '' |
| |
| this.task = { |
| |
| name: '', |
| |
| fnc: data.task?.fnc || '', |
| |
| cron: data.task?.cron || '' |
| } |
| |
| this.rule = data.rule || [] |
| } |
|
|
| |
| readJson (filePath, format = 'json') { |
| try { |
| if (format == 'yaml') { |
| return YAML.parse(fs.readFileSync(filePath, 'utf8')) |
| } |
| return JSON.parse(fs.readFileSync(filePath, 'utf8')) |
| } catch (err) { |
| return false |
| } |
| } |
|
|
| |
| writeJson (savePath, data, format = 'json') { |
| this.mkdir(path.dirname(savePath)) |
| if (format == 'yaml') return fs.writeFileSync(savePath, YAML.stringify(data)) |
| return fs.writeFileSync(savePath, JSON.stringify(data, null, 2)) |
| } |
|
|
| |
| mkdir (dirname) { |
| if (fs.existsSync(dirname)) { |
| return true |
| } else { |
| if (this.mkdir(path.dirname(dirname))) { |
| fs.mkdirSync(dirname) |
| return true |
| } |
| } |
| } |
|
|
| |
| sleep (ms) { |
| return new Promise((resolve) => setTimeout(resolve, ms)) |
| } |
|
|
| |
| limiter (limit = 15, cd = 60, options = {}) { |
| options = { |
| limit: limit, |
| windowMs: cd * 1000, |
| legacyHeaders: false, |
| message: { status: 1, message: 'Frequent requests, please try again later' }, |
| ...options |
| } |
| return rateLimit(options) |
| } |
|
|
| |
| upload (savaPath = './public/upload', filename, options = {}) { |
| options = { |
| storage: multer.diskStorage({ |
| destination: (req, file, cb) => { |
| cb(null, savaPath) |
| }, |
| filename: filename || ((req, file, cb) => { |
| cb(null, Date.now() + path.extname(file.originalname)) |
| }) |
| }), |
| limits: { fileSize: 5 * 1024 * 1024, files: 1 }, |
| ...options |
| } |
| return multer(options) |
| } |
| } |
|
|