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

View File

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

View File

@ -4,7 +4,7 @@ import { NapCatCoreWorkingEnv } from "@/core";
import { SelfInfo } from "../entities";
import { NodeIKernelLoginService } from "../services";
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";
export interface InstanceContext {
@ -16,6 +16,7 @@ export interface InstanceContext {
readonly basicInfoWrapper: QQBasicInfoWrapper;
}
export interface NTApiContext {
FileApi: NTQQFileApi,
SystemApi: NTQQSystemApi,
CollectionApi: NTQQCollectionApi,
WebApi: NTQQWebApi,

View File

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

View File

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

View File

@ -1,12 +1,9 @@
import BaseAction from '../BaseAction';
import fs from 'fs/promises';
import { ob11Config } from '@/onebot11/config';
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 { NTQQFileApi, NTQQFriendApi, NTQQMsgApi, NTQQUserApi } from '@/core/apis';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { getGroup } from '@/core/data';
export interface GetFilePayload {
file: string; // 文件名或者fileUuid
@ -42,7 +39,11 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
return { id: element.elementId, element: element.fileElement };
}
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: {
high: string;
low: string;
@ -52,7 +53,7 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
if (UuidData) {
const peerUin = UuidData.high;
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;
//识别Peer
if (isGroup) {
@ -88,7 +89,7 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
file_size: fileSize,
file_name: fileName
};
if (enableLocalFile2Url) {
if (true/*enableLocalFile2Url*/) {
try {
res.base64 = await fs.readFile(downloadPath, 'base64');
} catch (e) {
@ -128,7 +129,7 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
file_size: NTSearchNameResult[0].fileSize.toString(),
file_name: NTSearchNameResult[0].fileName
};
if (enableLocalFile2Url) {
if (true/*enableLocalFile2Url*/) {
try {
res.base64 = await fs.readFile(downloadPath, 'base64');
} catch (e) {

View File

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

View File

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

View File

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