refactor: 初步fileApi就绪

This commit is contained in:
手瓜一十雪 2024-08-09 17:12:57 +08:00
parent dfb31b78d9
commit b016268fdb
9 changed files with 84 additions and 80 deletions

View File

@ -75,69 +75,71 @@ export class NTQQFileApi {
async downloadMediaByUuid() { async downloadMediaByUuid() {
//napCatCore.session.getRichMediaService().downloadFileForFileUuid(); //napCatCore.session.getRichMediaService().downloadFileForFileUuid();
} }
// async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, timeout = 1000 * 60 * 2, force: boolean = false) { async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, timeout = 1000 * 60 * 2, force: boolean = false) {
// //logDebug('receive downloadMedia task', msgId, chatType, peerUid, elementId, thumbPath, sourcePath, timeout, force); //logDebug('receive downloadMedia task', msgId, chatType, peerUid, elementId, thumbPath, sourcePath, timeout, force);
// // 用于下载收到的消息中的图片等 // 用于下载收到的消息中的图片等
// if (sourcePath && fs.existsSync(sourcePath)) { if (sourcePath && fs.existsSync(sourcePath)) {
// if (force) { if (force) {
// try { try {
// await fsPromises.unlink(sourcePath); await fsPromises.unlink(sourcePath);
// } catch (e) { } catch (e) {
// // //
// } }
// } else { } else {
// return sourcePath; return sourcePath;
// } }
// } }
// const data = await this.core.eventWrapper.CallNormalEvent< const data = await this.core.eventWrapper.CallNormalEvent<
// ( (
// params: { params: {
// fileModelId: string, fileModelId: string,
// downloadSourceType: number, downloadSourceType: number,
// triggerType: number, triggerType: number,
// msgId: string, msgId: string,
// chatType: ChatType, chatType: ChatType,
// peerUid: string, peerUid: string,
// elementId: string, elementId: string,
// thumbSize: number, thumbSize: number,
// downloadType: number, downloadType: number,
// filePath: string filePath: string
// }) => Promise<unknown>, }) => Promise<unknown>,
// (fileTransNotifyInfo: OnRichMediaDownloadCompleteParams) => void (fileTransNotifyInfo: OnRichMediaDownloadCompleteParams) => void
// >( >(
// 'NodeIKernelMsgService/downloadRichMedia', 'NodeIKernelMsgService/downloadRichMedia',
// 'NodeIKernelMsgListener/onRichMediaDownloadComplete', 'NodeIKernelMsgListener/onRichMediaDownloadComplete',
// 1, 1,
// timeout, timeout,
// (arg: OnRichMediaDownloadCompleteParams) => { (arg: OnRichMediaDownloadCompleteParams) => {
// if (arg.msgId === msgId) { if (arg.msgId === msgId) {
// return true; return true;
// } }
// return false; return false;
// }, },
// { {
// fileModelId: '0', fileModelId: '0',
// downloadSourceType: 0, downloadSourceType: 0,
// triggerType: 1, triggerType: 1,
// msgId: msgId, msgId: msgId,
// chatType: chatType, chatType: chatType,
// peerUid: peerUid, peerUid: peerUid,
// elementId: elementId, elementId: elementId,
// thumbSize: 0, thumbSize: 0,
// downloadType: 1, downloadType: 1,
// filePath: thumbPath filePath: thumbPath
// } }
// ); );
// let filePath = data[1].filePath; let filePath = data[1].filePath;
// if (filePath.startsWith('\\')) { if (filePath.startsWith('\\')) {
// // log('filePath start with \\'); // log('filePath start with \\');
// const downloadPath = sessionConfig.defaultFileDownloadPath; // Mlikiowa V2.0.0 Refactor Todo
// //logDebug('downloadPath', downloadPath); //const downloadPath = sessionConfig.defaultFileDownloadPath;
// filePath = path.join(downloadPath, filePath); //logDebug('downloadPath', downloadPath);
// // 下载路径是下载文件夹的相对路径 //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// } filePath = path.join("", filePath);
// return filePath; // 下载路径是下载文件夹的相对路径
// } }
return filePath;
}
async getImageSize(filePath: string): Promise<ISizeCalculationResult | undefined> { async getImageSize(filePath: string): Promise<ISizeCalculationResult | undefined> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -8,7 +8,7 @@ import { MsgListener, ProfileListener } from "./listeners";
import { sleep } from "@/common/utils/helper"; import { sleep } from "@/common/utils/helper";
import { SelfInfo, LineDevice, SelfStatusInfo } from "./entities"; import { SelfInfo, LineDevice, SelfStatusInfo } from "./entities";
import { LegacyNTEventWrapper } from "@/common/framework/event-legacy"; import { LegacyNTEventWrapper } from "@/common/framework/event-legacy";
import { NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from "./apis"; import { NTQQFileApi, NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from "./apis";
import os from "node:os"; import os from "node:os";
import { NTQQCollectionApi } from "./apis/collection"; import { NTQQCollectionApi } from "./apis/collection";
export enum NapCatCoreWorkingEnv { export enum NapCatCoreWorkingEnv {
@ -43,8 +43,9 @@ export class NapCatCore {
this.eventWrapper = new LegacyNTEventWrapper(context.wrapper, context.session); this.eventWrapper = new LegacyNTEventWrapper(context.wrapper, context.session);
this.initNapCatCoreListeners().then().catch(console.error); this.initNapCatCoreListeners().then().catch(console.error);
this.ApiContext = { this.ApiContext = {
SystemApi:new NTQQSystemApi(this.context, this), FileApi: new NTQQFileApi(this.context, this),
CollectionApi:new NTQQCollectionApi(this.context, this), SystemApi: new NTQQSystemApi(this.context, this),
CollectionApi: new NTQQCollectionApi(this.context, this),
WebApi: new NTQQWebApi(this.context, this), WebApi: new NTQQWebApi(this.context, this),
FriendApi: new NTQQFriendApi(this.context, this), FriendApi: new NTQQFriendApi(this.context, this),
MsgApi: new NTQQMsgApi(this.context, this), MsgApi: new NTQQMsgApi(this.context, this),

View File

@ -4,7 +4,7 @@ import { NapCatCoreWorkingEnv } from "@/core";
import { SelfInfo } from "../entities"; import { SelfInfo } from "../entities";
import { NodeIKernelLoginService } from "../services"; import { NodeIKernelLoginService } from "../services";
import { WrapperNodeApi, NodeIQQNTWrapperSession } from "@/core"; import { WrapperNodeApi, NodeIQQNTWrapperSession } from "@/core";
import { NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from "../apis"; import { NTQQFileApi, NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from "../apis";
import { NTQQCollectionApi } from "../apis/collection"; import { NTQQCollectionApi } from "../apis/collection";
export interface InstanceContext { export interface InstanceContext {
@ -16,6 +16,7 @@ export interface InstanceContext {
readonly basicInfoWrapper: QQBasicInfoWrapper; readonly basicInfoWrapper: QQBasicInfoWrapper;
} }
export interface NTApiContext { export interface NTApiContext {
FileApi: NTQQFileApi,
SystemApi: NTQQSystemApi, SystemApi: NTQQSystemApi,
CollectionApi: NTQQCollectionApi, CollectionApi: NTQQCollectionApi,
WebApi: NTQQWebApi, WebApi: NTQQWebApi,

View File

@ -17,7 +17,6 @@ export class CreateCollection extends BaseAction<Payload, any> {
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
return await this.CoreContext.getApiContext().CollectionApi.createCollection( return await this.CoreContext.getApiContext().CollectionApi.createCollection(
this.CoreContext.selfInfo.uin, this.CoreContext.selfInfo.uin,
this.CoreContext.selfInfo.uid, this.CoreContext.selfInfo.uid,
this.CoreContext.selfInfo.nick, this.CoreContext.selfInfo.nick,

View File

@ -18,6 +18,7 @@ export class DelGroupFileFolder extends BaseAction<Payload, any> {
actionName = ActionName.DelGroupFileFolder; actionName = ActionName.DelGroupFileFolder;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi;
return (await NTQQGroupApi.DelGroupFileFolder(payload.group_id.toString(), payload.folder_id)).groupFileCommonResult; return (await NTQQGroupApi.DelGroupFileFolder(payload.group_id.toString(), payload.folder_id)).groupFileCommonResult;
} }
} }

View File

@ -1,12 +1,9 @@
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import fs from 'fs/promises'; import fs from 'fs/promises';
import { ob11Config } from '@/onebot11/config';
import { UUIDConverter } from '@/common/utils/helper'; import { UUIDConverter } from '@/common/utils/helper';
import { ActionName, BaseCheckResult } from '../types'; import { ActionName } from '../types';
import { ChatType, ElementType, FileElement, Peer, RawMessage, VideoElement } from '@/core/entities'; import { ChatType, ElementType, FileElement, Peer, RawMessage, VideoElement } from '@/core/entities';
import { NTQQFileApi, NTQQFriendApi, NTQQMsgApi, NTQQUserApi } from '@/core/apis';
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { getGroup } from '@/core/data';
export interface GetFilePayload { export interface GetFilePayload {
file: string; // 文件名或者fileUuid file: string; // 文件名或者fileUuid
@ -42,7 +39,11 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
return { id: element.elementId, element: element.fileElement }; return { id: element.elementId, element: element.fileElement };
} }
protected async _handle(payload: GetFilePayload): Promise<GetFileResponse> { protected async _handle(payload: GetFilePayload): Promise<GetFileResponse> {
const { enableLocalFile2Url } = ob11Config; const NTQQFriendApi = this.CoreContext.getApiContext().FriendApi;
const NTQQUserApi = this.CoreContext.getApiContext().UserApi;
const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi;
const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi;
const NTQQFileApi = this.CoreContext.getApiContext().FileApi;
let UuidData: { let UuidData: {
high: string; high: string;
low: string; low: string;
@ -52,7 +53,7 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
if (UuidData) { if (UuidData) {
const peerUin = UuidData.high; const peerUin = UuidData.high;
const msgId = UuidData.low; const msgId = UuidData.low;
const isGroup = await getGroup(peerUin); const isGroup: boolean = !!(await NTQQGroupApi.getGroups(false)).find(e => e.groupCode == peerUin);
let peer: Peer | undefined; let peer: Peer | undefined;
//识别Peer //识别Peer
if (isGroup) { if (isGroup) {
@ -88,7 +89,7 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
file_size: fileSize, file_size: fileSize,
file_name: fileName file_name: fileName
}; };
if (enableLocalFile2Url) { if (true/*enableLocalFile2Url*/) {
try { try {
res.base64 = await fs.readFile(downloadPath, 'base64'); res.base64 = await fs.readFile(downloadPath, 'base64');
} catch (e) { } catch (e) {
@ -128,7 +129,7 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
file_size: NTSearchNameResult[0].fileSize.toString(), file_size: NTSearchNameResult[0].fileSize.toString(),
file_name: NTSearchNameResult[0].fileName file_name: NTSearchNameResult[0].fileName
}; };
if (enableLocalFile2Url) { if (true/*enableLocalFile2Url*/) {
try { try {
res.base64 = await fs.readFile(downloadPath, 'base64'); res.base64 = await fs.readFile(downloadPath, 'base64');
} catch (e) { } catch (e) {

View File

@ -17,6 +17,7 @@ export class GetGroupFileCount extends BaseAction<Payload, { count: number }> {
actionName = ActionName.GetGroupFileCount; actionName = ActionName.GetGroupFileCount;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi;
const ret = await NTQQGroupApi.GetGroupFileCount([payload.group_id?.toString()]); const ret = await NTQQGroupApi.GetGroupFileCount([payload.group_id?.toString()]);
return { count: ret.groupFileCounts[0] }; return { count: ret.groupFileCounts[0] };
} }

View File

@ -1,8 +1,6 @@
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { NTQQGroupApi, NTQQMsgApi, NTQQUserApi } from '@/core/apis';
const SchemaData = { const SchemaData = {
type: 'object', type: 'object',
properties: { properties: {
@ -19,6 +17,7 @@ export class GetGroupFileList extends BaseAction<Payload, { FileList: Array<any>
actionName = ActionName.GetGroupFileList; actionName = ActionName.GetGroupFileList;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi;
const ret = await NTQQMsgApi.getGroupFileList(payload.group_id.toString(), { const ret = await NTQQMsgApi.getGroupFileList(payload.group_id.toString(), {
sortType: 1, sortType: 1,
fileCount: payload.file_count, fileCount: payload.file_count,

View File

@ -1,8 +1,6 @@
import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import BaseAction from '../BaseAction'; import BaseAction from '../BaseAction';
import { ActionName } from '../types'; import { ActionName } from '../types';
import { NTQQGroupApi, NTQQMsgApi, NTQQUserApi } from '@/core/apis';
const SchemaData = { const SchemaData = {
type: 'object', type: 'object',
properties: { properties: {
@ -18,6 +16,7 @@ export class SetGroupFileFolder extends BaseAction<Payload, any> {
actionName = ActionName.SetGroupFileFolder; actionName = ActionName.SetGroupFileFolder;
PayloadSchema = SchemaData; PayloadSchema = SchemaData;
protected async _handle(payload: Payload) { protected async _handle(payload: Payload) {
const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi;
return (await NTQQGroupApi.CreatGroupFileFolder(payload.group_id.toString(), payload.folder_name)).resultWithGroupItem; return (await NTQQGroupApi.CreatGroupFileFolder(payload.group_id.toString(), payload.folder_name)).resultWithGroupItem;
} }
} }