Skip to content

Commit

Permalink
feat(sandbox): support local file server
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 30, 2024
1 parent f63322a commit a4fb464
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion plugins/sandbox/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
27 changes: 23 additions & 4 deletions plugins/sandbox/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<Config> = Schema.object({})
export const Config: Schema<Config> = Schema.object({
fileServer: Schema.object({
enabled: Schema.boolean().default(false).description('是否提供本地静态文件服务 (请勿在暴露在公网的设备上开启此选项)。')
}),
})

class SandboxService extends DataService<Dict<number>> {
static inject = ['database']
Expand Down Expand Up @@ -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:'))
Expand Down
5 changes: 2 additions & 3 deletions plugins/sandbox/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Context, Dict, h, MessageEncoder, Random } from 'koishi'
import {} from '@koishijs/assets'
import { SandboxBot } from './bot'

export class SandboxMessenger<C extends Context = Context> extends MessageEncoder<C, SandboxBot<C>> {
Expand All @@ -9,8 +8,8 @@ export class SandboxMessenger<C extends Context = Context> 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 })
}]
Expand Down

0 comments on commit a4fb464

Please sign in to comment.