feat: upload private file

This commit is contained in:
linyuchen 2024-04-25 23:27:14 +08:00
parent ac852cc382
commit 05f0985f7f

View File

@ -1,27 +1,32 @@
import BaseAction from "../BaseAction"; import BaseAction from "../BaseAction";
import {getGroup} from "../../../common/data"; import {getGroup, getUidByUin} from "../../../common/data";
import {ActionName} from "../types"; import {ActionName} from "../types";
import {SendMsgElementConstructor} from "../../../ntqqapi/constructor"; import {SendMsgElementConstructor} from "../../../ntqqapi/constructor";
import {ChatType, SendFileElement} from "../../../ntqqapi/types"; import {ChatType, SendFileElement} from "../../../ntqqapi/types";
import fs from "fs"; import fs from "fs";
import {NTQQMsgApi} from "../../../ntqqapi/api/msg"; import {NTQQMsgApi, Peer} from "../../../ntqqapi/api/msg";
import {uri2local} from "../../../common/utils"; import {uri2local} from "../../../common/utils";
interface Payload{ interface Payload{
group_id: number user_id: number
group_id?: number
file: string file: string
name: string name: string
folder: string folder: string
} }
export default class GoCQHTTPUploadGroupFile extends BaseAction<Payload, null> { class GoCQHTTPUploadFileBase extends BaseAction<Payload, null> {
actionName = ActionName.GoCQHTTP_UploadGroupFile actionName = ActionName.GoCQHTTP_UploadGroupFile
protected async _handle(payload: Payload): Promise<null> {
const group = await getGroup(payload.group_id.toString()); getPeer(payload: Payload): Peer {
if (!group){ if (payload.user_id){
throw new Error(`群组${payload.group_id}不存在`) return {chatType: ChatType.friend, peerUid: getUidByUin(payload.user_id.toString())}
} }
return {chatType: ChatType.group, peerUid: payload.group_id.toString()}
}
protected async _handle(payload: Payload): Promise<null> {
let file = payload.file; let file = payload.file;
if (fs.existsSync(file)){ if (fs.existsSync(file)){
file = `file://${file}` file = `file://${file}`
@ -31,7 +36,16 @@ export default class GoCQHTTPUploadGroupFile extends BaseAction<Payload, null> {
throw new Error(downloadResult.errMsg) throw new Error(downloadResult.errMsg)
} }
let sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name); let sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name);
await NTQQMsgApi.sendMsg({chatType: ChatType.group, peerUid: group.groupCode}, [sendFileEle]); await NTQQMsgApi.sendMsg(this.getPeer(payload), [sendFileEle]);
return null return null
} }
} }
export class GoCQHTTPUploadGroupFile extends GoCQHTTPUploadFileBase {
actionName = ActionName.GoCQHTTP_UploadGroupFile
}
export class GoCQHTTPUploadPrivateFile extends GoCQHTTPUploadFileBase {
actionName = ActionName.GoCQHTTP_UploadPrivateFile
}