feat: action check data
This commit is contained in:
1 parent
725376e821
commit
a3eb106785
8 files changed
+158
-46
No files matched your search
@@ -1,25 +1,46 @@
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
import { ActionName, BaseCheckResult } from '../types';
|
||||
import fs from 'fs';
|
||||
import { join as joinPath } from 'node:path';
|
||||
import { calculateFileMD5, getTempDir, httpDownload } from '@/common/utils/file';
|
||||
import { v4 as uuid4 } from 'uuid';
|
||||
|
||||
interface Payload {
|
||||
thread_count?: number;
|
||||
url?: string;
|
||||
base64?: string;
|
||||
name?: string;
|
||||
headers?: string | string[];
|
||||
}
|
||||
|
||||
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||
import Ajv from 'ajv';
|
||||
interface FileResponse {
|
||||
file: string;
|
||||
}
|
||||
const PayloadSchema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
thread_count: { type: 'number' },
|
||||
url: { type: 'string' },
|
||||
base64: { type: 'string' },
|
||||
name: { type: 'string' },
|
||||
headers: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
} as const satisfies JSONSchema;
|
||||
|
||||
type Payload = FromSchema<typeof PayloadSchema>;
|
||||
|
||||
export default class GoCQHTTPDownloadFile extends BaseAction<Payload, FileResponse> {
|
||||
actionName = ActionName.GoCQHTTP_DownloadFile;
|
||||
|
||||
validateDownload = new Ajv().compile(PayloadSchema);
|
||||
// 这里重写是为了兼容 headers可能出现 string string[]
|
||||
protected async check(payload: Payload): Promise<BaseCheckResult> {
|
||||
if (payload.headers) {
|
||||
// 如果存在headers 为数组则开始兼容string string[]
|
||||
payload.headers = payload?.headers && Array.isArray(payload.headers) ? payload.headers : [payload.headers as unknown as string];
|
||||
}
|
||||
if (!this.validateDownload(payload)) {
|
||||
return { valid: false, message: this.validateDownload.errors?.map(e => e.message).join(', ') as string };
|
||||
}
|
||||
return { valid: true };
|
||||
}
|
||||
protected async _handle(payload: Payload): Promise<FileResponse> {
|
||||
const isRandomName = !payload.name;
|
||||
const name = payload.name || uuid4();
|
||||
|
||||
@@ -3,12 +3,19 @@ import { OB11ForwardMessage, OB11Message, OB11MessageData } from '../../types';
|
||||
import { NTQQMsgApi } from '@/core/apis';
|
||||
import { dbUtil } from '@/core/utils/db';
|
||||
import { OB11Constructor } from '../../constructor';
|
||||
import { ActionName } from '../types';
|
||||
import { ActionName, BaseCheckResult } from '../types';
|
||||
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||
import Ajv from 'ajv';
|
||||
|
||||
interface Payload {
|
||||
message_id: string; // long msg id
|
||||
id?: string; // short msg id
|
||||
}
|
||||
const SchemaData = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
message_id: { type: 'string' },
|
||||
id: { type: 'string' }
|
||||
},
|
||||
} as const satisfies JSONSchema;
|
||||
|
||||
type Payload = FromSchema<typeof SchemaData>;
|
||||
|
||||
interface Response {
|
||||
messages: (OB11Message & { content: OB11MessageData })[];
|
||||
@@ -16,7 +23,7 @@ interface Response {
|
||||
|
||||
export class GoCQHTTGetForwardMsgAction extends BaseAction<Payload, any> {
|
||||
actionName = ActionName.GoCQHTTP_GetForwardMsg;
|
||||
|
||||
PayloadSchema = SchemaData;
|
||||
protected async _handle(payload: Payload): Promise<any> {
|
||||
const msgId = payload.message_id || payload.id;
|
||||
if (!msgId) {
|
||||
@@ -25,7 +32,7 @@ export class GoCQHTTGetForwardMsgAction extends BaseAction<Payload, any> {
|
||||
let rootMsg = await dbUtil.getMsgByLongId(msgId);
|
||||
if (!rootMsg) {
|
||||
rootMsg = await dbUtil.getMsgByShortId(parseInt(msgId));
|
||||
if (!rootMsg){
|
||||
if (!rootMsg) {
|
||||
throw Error('msg not found');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user