diff --git a/plugins/sandbox/package.json b/plugins/sandbox/package.json index 55131a39..25b801d4 100644 --- a/plugins/sandbox/package.json +++ b/plugins/sandbox/package.json @@ -1,7 +1,7 @@ { "name": "@koishijs/plugin-sandbox", "description": "Test Your Virtual Bot in Console", - "version": "3.4.0", + "version": "3.4.1", "main": "lib/index.js", "typings": "lib/index.d.ts", "files": [ diff --git a/plugins/sandbox/src/index.ts b/plugins/sandbox/src/index.ts index 4f32cf99..219b5148 100644 --- a/plugins/sandbox/src/index.ts +++ b/plugins/sandbox/src/index.ts @@ -1,8 +1,11 @@ import { $, Context, Dict, Random, Schema, Universal, User } from 'koishi' import { Client, DataService } from '@koishijs/console' -import { resolve } from 'path' +import {} from '@koishijs/plugin-server' +import { extname, resolve } from 'path' import { SandboxBot } from './bot' import zhCN from './locales/zh-CN.yml' +import { createReadStream } from 'fs' +import { fileURLToPath } from 'url' declare module 'koishi' { interface Events { @@ -37,11 +40,19 @@ export interface Message { export const filter = false export const name = 'sandbox' -export const inject = ['console'] +export const inject = ['console', 'server'] -export interface Config {} +export interface Config { + fileServer: { + enabled: boolean + } +} -export const Config: Schema = Schema.object({}) +export const Config: Schema = Schema.object({ + fileServer: Schema.object({ + enabled: Schema.boolean().default(false).description('是否提供本地静态文件服务 (请勿在暴露在公网的设备上开启此选项)。') + }), +}) class SandboxService extends DataService> { static inject = ['database'] @@ -175,6 +186,14 @@ export function apply(ctx: Context, config: Config) { } }) + if (config.fileServer.enabled) { + ctx.server.get('/sandbox/:url(file:.+)', async (koa) => { + const { url } = koa.params + koa.type = extname(url) + koa.body = createReadStream(fileURLToPath(url)) + }) + } + ctx.i18n.define('zh-CN', zhCN) ctx.intersect(session => session.platform.startsWith('sandbox:')) diff --git a/plugins/sandbox/src/message.ts b/plugins/sandbox/src/message.ts index 7d8cff47..a865bf8a 100644 --- a/plugins/sandbox/src/message.ts +++ b/plugins/sandbox/src/message.ts @@ -1,5 +1,4 @@ import { Context, Dict, h, MessageEncoder, Random } from 'koishi' -import {} from '@koishijs/assets' import { SandboxBot } from './bot' export class SandboxMessenger extends MessageEncoder> { @@ -9,8 +8,8 @@ export class SandboxMessenger extends MessageEncode return [type, async (attrs) => { const src = attrs.src || attrs.url const type1 = type === 'image' ? 'img' : type - if (src.startsWith('file:') && this.bot.ctx.assets) { - return h(type1, { ...attrs, src: await this.bot.ctx.assets.upload(src, src) }) + if (src.startsWith('file:')) { + return h(type1, { ...attrs, src: `${this.bot.ctx.server.selfUrl}/sandbox/${src}` }) } return h(type1, { ...attrs, src }) }]