UNPKG

koishi-plugin-kbot

Version:
52 lines (51 loc) 1.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.imageRoutes = void 0; /* * @Author: Kabuda-czh * @Date: 2023-06-28 17:34:17 * @LastEditors: Kabuda-czh * @LastEditTime: 2023-07-24 12:02:32 * @FilePath: \KBot-App\plugins\kbot\src\plugins\guildManage\router\image.ts * @Description: * * Copyright (c) 2023 by Kabuda-czh, All Rights Reserved. */ const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const node_url_1 = require("node:url"); const config_1 = __importDefault(require("../../../config")); function getImageInfo(context) { return async (ctx) => { const imageBase64 = ctx.request.body.image; if (!imageBase64) { ctx.body = null; } else { const { kbotDir } = config_1.default.getInstance(context.baseDir).getGeneratePathData(); // 将 base64 数据存入临时文件 const tempPath = node_path_1.default.resolve(kbotDir, 'temp'); if (!node_fs_1.default.existsSync(tempPath)) await node_fs_1.default.promises.mkdir(tempPath); const tempFilePath = node_path_1.default.join(tempPath, `${Date.now()}.jpg`); const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, ''); // eslint-disable-next-line n/prefer-global/buffer const dataBuffer = Buffer.from(base64Data, 'base64'); await node_fs_1.default.promises.writeFile(tempFilePath, dataBuffer); ctx.body = { code: 0, data: { path: (0, node_url_1.pathToFileURL)(tempFilePath), }, }; } }; } exports.imageRoutes = { '/imageInfo': function (context) { return getImageInfo(context); }, };