Commit 55474879 authored by nanahira's avatar nanahira

some fixes

parent 67646c15
Pipeline #11793 canceled with stage
// import 'source-map-support/register';
import { DefineSchema, RegisterSchema, SchemaClass } from 'koishi-thirdeye';
import { SchemaProperty, RegisterSchema, SchemaClass } from 'koishi-thirdeye';
import { Quester } from 'koishi';
import { PicMiddleware } from './index';
@RegisterSchema()
export class PicsPluginConfig {
constructor(config: Partial<PicsPluginConfig>) {}
@DefineSchema({ description: '指令名', default: 'pic', hidden: true })
@SchemaProperty({ description: '指令名', default: 'pic', hidden: true })
commandName: string;
@DefineSchema({
@SchemaProperty({
description: 'Assets 服务可用时,使用 Assets 缓存图片。',
default: true,
})
useAssets: boolean;
@DefineSchema({ description: '使用 Base64 发送图片结果。', default: false })
@SchemaProperty({ description: '使用 Base64 发送图片结果。', default: false })
useBase64: boolean;
@DefineSchema({ type: Quester.createSchema(), default: {} })
@SchemaProperty({ type: Quester.createSchema(), default: {} })
httpConfig: Quester.Config;
}
......@@ -37,15 +37,15 @@ export interface PicSourceInfo {
export class PicSourceConfig implements PicSourceInfo {
constructor(config: Partial<PicSourceInfo>) {}
@DefineSchema({ type: 'string', default: [], description: '图源标签' })
@SchemaProperty({ type: 'string', default: [], description: '图源标签' })
tags: string[];
@DefineSchema({ default: 1, description: '图源权重' })
@SchemaProperty({ default: 1, description: '图源权重' })
weight: number;
@DefineSchema({ default: 1, description: '图源名称', required: true })
@SchemaProperty({ default: 1, description: '图源名称', required: true })
name: string;
@DefineSchema({ description: '图源描述' })
@SchemaProperty({ description: '图源描述' })
description?: string;
@DefineSchema({ description: '是否为默认图源' })
@SchemaProperty({ description: '是否为默认图源' })
isDefault?: boolean;
// 给目标对象注入上述对象。
......@@ -67,7 +67,9 @@ export interface PicMiddlewareInfo {
export class PicMiddlewareConfig {
constructor(config: PicMiddlewareInfo) {}
@SchemaProperty({ description: '中间件名称。' })
name: string;
@SchemaProperty({ description: '是否在首位插入中间件。', default: false })
prepend: boolean;
applyTo(target: PicMiddleware) {
......
......@@ -223,15 +223,26 @@ export default class PicsContainer
);
}
async download(url: string, extraConfig: AxiosRequestConfig = {}) {
async urlToBuffer(url: string, extraConfig: AxiosRequestConfig = {}) {
if (url.startsWith('base64://')) {
return url;
return Buffer.from(url.slice(9), 'base64');
}
const buf = await this._http.get(url, {
return this._http.get<Buffer>(url, {
responseType: 'arraybuffer',
...extraConfig,
});
return `base64://${buf.toString('base64')}`;
}
bufferToUrl(buffer: Buffer) {
return `base64://${buffer.toString('base64')}`;
}
async download(url: string, extraConfig: AxiosRequestConfig = {}) {
if (url.startsWith('base64://')) {
return url;
}
const buffer = await this.urlToBuffer(url, extraConfig);
return this.bufferToUrl(buffer);
}
async resolveUrl(url: string, middlwares = this.picMiddlewares) {
......
......@@ -5,9 +5,6 @@ import { PicMiddlewareConfig } from '../config';
@DefinePlugin({ schema: PicMiddlewareConfig })
export class PicDownloaderMiddleware extends PicMiddlewareBase {
override async use(url: string, next: PicNext) {
if (url.startsWith('base64://')) {
return next();
}
const downloadedUrl = await this.pics.download(url);
return next(downloadedUrl);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment