mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
fix: self nickname
fix: @member msg report fix: send file:// on Windows ver: 3.0.2
This commit is contained in:
parent
6a8d67a8ae
commit
1936671cb3
@ -4,7 +4,7 @@
|
|||||||
"name": "LLOneBot",
|
"name": "LLOneBot",
|
||||||
"slug": "LLOneBot",
|
"slug": "LLOneBot",
|
||||||
"description": "LiteLoaderQQNT的OneBotApi",
|
"description": "LiteLoaderQQNT的OneBotApi",
|
||||||
"version": "3.0.1",
|
"version": "3.0.2",
|
||||||
"thumbnail": "./icon.png",
|
"thumbnail": "./icon.png",
|
||||||
"authors": [{
|
"authors": [{
|
||||||
"name": "linyuchen",
|
"name": "linyuchen",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { NTQQApi } from '../ntqqapi/ntcall';
|
import { NTQQApi } from '../ntqqapi/ntcall';
|
||||||
import { Friend, Group, RawMessage, SelfInfo } from "../ntqqapi/types";
|
import { Friend, Group, GroupMember, RawMessage, SelfInfo } from "../ntqqapi/types";
|
||||||
|
|
||||||
export let groups: Group[] = []
|
export let groups: Group[] = []
|
||||||
export let friends: Friend[] = []
|
export let friends: Friend[] = []
|
||||||
@ -23,16 +23,23 @@ export async function getGroup(qq: string): Promise<Group | undefined> {
|
|||||||
return group
|
return group
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getGroupMember(groupQQ: string, memberQQ: string) {
|
export async function getGroupMember(groupQQ: string, memberQQ: string=null, memberUid: string=null) {
|
||||||
const group = await getGroup(groupQQ)
|
const group = await getGroup(groupQQ)
|
||||||
if (group) {
|
if (group) {
|
||||||
let member = group.members?.find(member => member.uin === memberQQ)
|
let filterFunc: (member: GroupMember) => boolean
|
||||||
|
if (memberQQ){
|
||||||
|
filterFunc = member => member.uin === memberQQ
|
||||||
|
}
|
||||||
|
else if (memberUid){
|
||||||
|
filterFunc = member => member.uid === memberUid
|
||||||
|
}
|
||||||
|
let member = group.members?.find(filterFunc)
|
||||||
if (!member){
|
if (!member){
|
||||||
const _members = await NTQQApi.getGroupMembers(groupQQ)
|
const _members = await NTQQApi.getGroupMembers(groupQQ)
|
||||||
if (_members.length){
|
if (_members.length){
|
||||||
group.members = _members
|
group.members = _members
|
||||||
}
|
}
|
||||||
member = group.members?.find(member => member.uin === memberQQ)
|
member = group.members?.find(filterFunc)
|
||||||
}
|
}
|
||||||
return member
|
return member
|
||||||
}
|
}
|
||||||
|
@ -83,6 +83,7 @@ function onLoad() {
|
|||||||
log("report self message error: ", e.toString())
|
log("report self message error: ", e.toString())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
NTQQApi.getGroups(true).then()
|
||||||
startExpress(getConfigUtil().getConfig().port)
|
startExpress(getConfigUtil().getConfig().port)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,6 +100,7 @@ function onLoad() {
|
|||||||
if (selfInfo.uin) {
|
if (selfInfo.uin) {
|
||||||
try {
|
try {
|
||||||
const userInfo = (await NTQQApi.getUserInfo(selfInfo.uid))
|
const userInfo = (await NTQQApi.getUserInfo(selfInfo.uid))
|
||||||
|
log("self info", userInfo);
|
||||||
if (userInfo) {
|
if (userInfo) {
|
||||||
selfInfo.nick = userInfo.nick
|
selfInfo.nick = userInfo.nick
|
||||||
} else {
|
} else {
|
||||||
@ -108,30 +110,11 @@ function onLoad() {
|
|||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log("get self nickname failed", e.toString())
|
log("get self nickname failed", e.toString())
|
||||||
|
return setTimeout(() => {
|
||||||
|
getSelfInfo().then()
|
||||||
|
}, 100)
|
||||||
}
|
}
|
||||||
start();
|
start();
|
||||||
// try {
|
|
||||||
// friends.push(...(await NTQQApi.getFriends(true)))
|
|
||||||
// log("get friends", friends)
|
|
||||||
// let _groups: Group[] = []
|
|
||||||
// for(let i=0; i++; i<3){
|
|
||||||
// try{
|
|
||||||
// _groups = await NTQQApi.getGroups(true)
|
|
||||||
// log("get groups sucess", _groups)
|
|
||||||
// break
|
|
||||||
// } catch(e) {
|
|
||||||
// log("get groups failed", e)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// for (let g of _groups) {
|
|
||||||
// g.members = (await NTQQApi.getGroupMembers(g.groupCode))
|
|
||||||
// log("group members", g.members)
|
|
||||||
// groups.push(g)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// } catch (e) {
|
|
||||||
// log("!!!初始化失败", e.stack.toString())
|
|
||||||
// }
|
|
||||||
} else {
|
} else {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
getSelfInfo().then()
|
getSelfInfo().then()
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
import {BrowserWindow} from 'electron';
|
import { BrowserWindow } from 'electron';
|
||||||
import {getConfigUtil, log} from "../common/utils";
|
import { getConfigUtil, log } from "../common/utils";
|
||||||
import {NTQQApi, NTQQApiClass, sendMessagePool} from "./ntcall";
|
import { NTQQApi, NTQQApiClass, sendMessagePool } from "./ntcall";
|
||||||
import { Group, User } from "./types";
|
import { Group, User } from "./types";
|
||||||
import { RawMessage } from "./types";
|
import { RawMessage } from "./types";
|
||||||
import {friends, groups, msgHistory} from "../common/data";
|
import { friends, groups, msgHistory } from "../common/data";
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
export let hookApiCallbacks: Record<string, (apiReturn: any)=>void>={}
|
export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {}
|
||||||
|
|
||||||
export enum ReceiveCmd {
|
export enum ReceiveCmd {
|
||||||
UPDATE_MSG = "nodeIKernelMsgListener/onMsgInfoListUpdate",
|
UPDATE_MSG = "nodeIKernelMsgListener/onMsgInfoListUpdate",
|
||||||
NEW_MSG = "nodeIKernelMsgListener/onRecvMsg",
|
NEW_MSG = "nodeIKernelMsgListener/onRecvMsg",
|
||||||
SELF_SEND_MSG = "nodeIKernelMsgListener/onAddSendMsg",
|
SELF_SEND_MSG = "nodeIKernelMsgListener/onAddSendMsg",
|
||||||
USER_INFO = "nodeIKernelProfileListener/onProfileDetailInfoChanged",
|
USER_INFO = "nodeIKernelProfileListener/onProfileSimpleChanged",
|
||||||
GROUPS = "nodeIKernelGroupListener/onGroupListUpdate",
|
GROUPS = "nodeIKernelGroupListener/onGroupListUpdate",
|
||||||
GROUPS_UNIX = "onGroupListUpdate",
|
GROUPS_UNIX = "onGroupListUpdate",
|
||||||
FRIENDS = "onBuddyListChange"
|
FRIENDS = "onBuddyListChange"
|
||||||
}
|
}
|
||||||
|
|
||||||
interface NTQQApiReturnData<PayloadType=unknown> extends Array<any> {
|
interface NTQQApiReturnData<PayloadType = unknown> extends Array<any> {
|
||||||
0: {
|
0: {
|
||||||
"type": "request",
|
"type": "request",
|
||||||
"eventName": NTQQApiClass,
|
"eventName": NTQQApiClass,
|
||||||
@ -51,7 +51,7 @@ export function hookNTQQApiReceive(window: BrowserWindow) {
|
|||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
hook.hookFunc(receiveData.payload);
|
hook.hookFunc(receiveData.payload);
|
||||||
}catch (e) {
|
} catch (e) {
|
||||||
log("hook error", e, receiveData.payload)
|
log("hook error", e, receiveData.payload)
|
||||||
}
|
}
|
||||||
}).then()
|
}).then()
|
||||||
@ -59,10 +59,10 @@ export function hookNTQQApiReceive(window: BrowserWindow) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (args[0]?.callbackId){
|
if (args[0]?.callbackId) {
|
||||||
// log("hookApiCallback", hookApiCallbacks, args)
|
// log("hookApiCallback", hookApiCallbacks, args)
|
||||||
const callbackId = args[0].callbackId;
|
const callbackId = args[0].callbackId;
|
||||||
if (hookApiCallbacks[callbackId]){
|
if (hookApiCallbacks[callbackId]) {
|
||||||
// log("callback found")
|
// log("callback found")
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
hookApiCallbacks[callbackId](args[1]);
|
hookApiCallbacks[callbackId](args[1]);
|
||||||
@ -85,36 +85,43 @@ export function registerReceiveHook<PayloadType>(method: ReceiveCmd, hookFunc: (
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeReceiveHook(id: string){
|
export function removeReceiveHook(id: string) {
|
||||||
const index = receiveHooks.findIndex(h=>h.id === id)
|
const index = receiveHooks.findIndex(h => h.id === id)
|
||||||
receiveHooks.splice(index, 1);
|
receiveHooks.splice(index, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateGroups(_groups: Group[]){
|
async function updateGroups(_groups: Group[]) {
|
||||||
for(let group of _groups){
|
for (let group of _groups) {
|
||||||
let existGroup = groups.find(g=>g.groupCode == group.groupCode)
|
let existGroup = groups.find(g => g.groupCode == group.groupCode)
|
||||||
if (!existGroup){
|
if (!existGroup) {
|
||||||
// log("update group")
|
NTQQApi.getGroupMembers(group.groupCode).then(members => {
|
||||||
let _membeers = await NTQQApi.getGroupMembers(group.groupCode)
|
if (members) {
|
||||||
if (_membeers){
|
group.members = members
|
||||||
group.members = _membeers
|
}
|
||||||
}
|
})
|
||||||
|
groups.push(group)
|
||||||
log("update group members", group.members)
|
log("update group members", group.members)
|
||||||
}
|
} else {
|
||||||
else{
|
Object.assign(existGroup, group)
|
||||||
group.members = [...existGroup.members]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
groups.length = 0;
|
|
||||||
groups.push(..._groups)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
registerReceiveHook<{groupList: Group[]}>(ReceiveCmd.GROUPS, (payload)=>updateGroups(payload.groupList).then())
|
registerReceiveHook<{ groupList: Group[] }>(ReceiveCmd.GROUPS, (payload) => updateGroups(payload.groupList).then())
|
||||||
registerReceiveHook<{groupList: Group[]}>(ReceiveCmd.GROUPS_UNIX, (payload)=>updateGroups(payload.groupList).then())
|
registerReceiveHook<{ groupList: Group[] }>(ReceiveCmd.GROUPS_UNIX, (payload) => updateGroups(payload.groupList).then())
|
||||||
registerReceiveHook<{data:{categoryId: number, categroyName: string, categroyMbCount: number, buddyList: User[]}[]}>(ReceiveCmd.FRIENDS, payload=>{
|
registerReceiveHook<{
|
||||||
friends.length = 0
|
data: { categoryId: number, categroyName: string, categroyMbCount: number, buddyList: User[] }[]
|
||||||
|
}>(ReceiveCmd.FRIENDS, payload => {
|
||||||
for (const fData of payload.data) {
|
for (const fData of payload.data) {
|
||||||
friends.push(...fData.buddyList)
|
const _friends = fData.buddyList;
|
||||||
|
for (let friend of _friends) {
|
||||||
|
let existFriend = friends.find(f => f.uin == friend.uin)
|
||||||
|
if (!existFriend) {
|
||||||
|
friends.push(friend)
|
||||||
|
} else {
|
||||||
|
Object.assign(existFriend, friend)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -131,24 +138,27 @@ registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.UPDATE_MSG, (payl
|
|||||||
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
|
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
|
||||||
for (const message of payload.msgList) {
|
for (const message of payload.msgList) {
|
||||||
log("收到新消息,push到历史记录", message)
|
log("收到新消息,push到历史记录", message)
|
||||||
if (!msgHistory[message.msgId]){
|
if (!msgHistory[message.msgId]) {
|
||||||
msgHistory[message.msgId] = message
|
msgHistory[message.msgId] = message
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
Object.assign(msgHistory[message.msgId], message)
|
Object.assign(msgHistory[message.msgId], message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const msgIds = Object.keys(msgHistory);
|
||||||
|
if (msgIds.length > 3000) {
|
||||||
|
delete msgHistory[msgIds.sort()[0]]
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
registerReceiveHook<{msgRecord: RawMessage}>(ReceiveCmd.SELF_SEND_MSG, ({msgRecord})=>{
|
registerReceiveHook<{ msgRecord: RawMessage }>(ReceiveCmd.SELF_SEND_MSG, ({msgRecord}) => {
|
||||||
const message = msgRecord;
|
const message = msgRecord;
|
||||||
const peerUid = message.peerUid;
|
const peerUid = message.peerUid;
|
||||||
// log("收到自己发送成功的消息", Object.keys(sendMessagePool), message);
|
// log("收到自己发送成功的消息", Object.keys(sendMessagePool), message);
|
||||||
const sendCallback = sendMessagePool[peerUid];
|
const sendCallback = sendMessagePool[peerUid];
|
||||||
if (sendCallback){
|
if (sendCallback) {
|
||||||
try{
|
try {
|
||||||
sendCallback(message);
|
sendCallback(message);
|
||||||
}catch(e){
|
} catch (e) {
|
||||||
log("receive self msg error", e.stack)
|
log("receive self msg error", e.stack)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -142,10 +142,9 @@ export class NTQQApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async getUserInfo(uid: string) {
|
static async getUserInfo(uid: string) {
|
||||||
const result = await callNTQQApi<{ info: User }>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.USER_INFO,
|
const result = await callNTQQApi<{ profiles: Map<string, User> }>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.USER_INFO,
|
||||||
[{ force: true, uids: [uid] }, undefined], ReceiveCmd.USER_INFO)
|
[{ force: true, uids: [uid] }, undefined], ReceiveCmd.USER_INFO)
|
||||||
return result.info
|
return result.profiles.get(uid)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getFriends(forced = false) {
|
static async getFriends(forced = false) {
|
||||||
|
@ -10,6 +10,7 @@ import { v4 as uuid4 } from 'uuid';
|
|||||||
import { log } from "../../common/utils";
|
import { log } from "../../common/utils";
|
||||||
import BaseAction from "./BaseAction";
|
import BaseAction from "./BaseAction";
|
||||||
import { ActionName } from "./types";
|
import { ActionName } from "./types";
|
||||||
|
import * as fs from "fs";
|
||||||
|
|
||||||
export interface ReturnDataType {
|
export interface ReturnDataType {
|
||||||
message_id: string
|
message_id: string
|
||||||
@ -23,6 +24,7 @@ class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
|||||||
chatType: ChatType.friend,
|
chatType: ChatType.friend,
|
||||||
peerUid: ""
|
peerUid: ""
|
||||||
}
|
}
|
||||||
|
let deleteAfterSentFiles: string[] = []
|
||||||
let group: Group | undefined = undefined;
|
let group: Group | undefined = undefined;
|
||||||
if (payload?.group_id) {
|
if (payload?.group_id) {
|
||||||
group = await getGroup(payload.group_id.toString())
|
group = await getGroup(payload.group_id.toString())
|
||||||
@ -94,21 +96,21 @@ class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case OB11MessageDataType.image: {
|
case OB11MessageDataType.image:
|
||||||
const file = sendMsg.data?.file
|
|
||||||
if (file) {
|
|
||||||
const picPath = await (await uri2local(uuid4(), file)).path
|
|
||||||
if (picPath) {
|
|
||||||
sendElements.push(await SendMsgElementConstructor.pic(picPath))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} break;
|
|
||||||
case OB11MessageDataType.voice: {
|
case OB11MessageDataType.voice: {
|
||||||
const file = sendMsg.data?.file
|
const file = sendMsg.data?.file
|
||||||
if (file) {
|
if (file) {
|
||||||
const voicePath = await (await uri2local(uuid4(), file)).path
|
const {path, isLocal} = (await uri2local(uuid4(), file))
|
||||||
if (voicePath) {
|
if (path) {
|
||||||
sendElements.push(await SendMsgElementConstructor.ptt(voicePath))
|
if (!isLocal){ // 只删除http和base64转过来的文件
|
||||||
|
deleteAfterSentFiles.push(path)
|
||||||
|
}
|
||||||
|
if (sendMsg.type === OB11MessageDataType.image){
|
||||||
|
sendElements.push(await SendMsgElementConstructor.pic(path))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sendElements.push(await SendMsgElementConstructor.ptt(path))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,6 +119,7 @@ class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
|||||||
// log("send msg:", peer, sendElements)
|
// log("send msg:", peer, sendElements)
|
||||||
try {
|
try {
|
||||||
const returnMsg = await NTQQApi.sendMsg(peer, sendElements)
|
const returnMsg = await NTQQApi.sendMsg(peer, sendElements)
|
||||||
|
deleteAfterSentFiles.map(f=>fs.unlink(f, ()=>{}))
|
||||||
return { message_id: returnMsg.msgId }
|
return { message_id: returnMsg.msgId }
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw(e.toString())
|
throw(e.toString())
|
||||||
|
@ -54,10 +54,18 @@ export class OB11Constructor {
|
|||||||
message_data["data"]["mention"] = "all"
|
message_data["data"]["mention"] = "all"
|
||||||
message_data["data"]["qq"] = "all"
|
message_data["data"]["qq"] = "all"
|
||||||
} else {
|
} else {
|
||||||
|
let atUid = element.textElement.atNtUid
|
||||||
let atQQ = element.textElement.atUid
|
let atQQ = element.textElement.atUid
|
||||||
// let atMember = await getGroupMember(msg.peerUin, uid)
|
if (!atQQ || atQQ === "0"){
|
||||||
message_data["data"]["mention"] = atQQ
|
const atMember = await getGroupMember(msg.peerUin, null, atUid)
|
||||||
message_data["data"]["qq"] = atQQ
|
if (atMember){
|
||||||
|
atQQ = atMember.uin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (atQQ){
|
||||||
|
message_data["data"]["mention"] = atQQ
|
||||||
|
message_data["data"]["qq"] = atQQ
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (element.textElement) {
|
} else if (element.textElement) {
|
||||||
message_data["type"] = "text"
|
message_data["type"] = "text"
|
||||||
@ -103,7 +111,6 @@ export class OB11Constructor {
|
|||||||
let filePath: string = message_data.data.file;
|
let filePath: string = message_data.data.file;
|
||||||
message_data.data.file = "file://" + filePath
|
message_data.data.file = "file://" + filePath
|
||||||
if (enableBase64) {
|
if (enableBase64) {
|
||||||
// filePath = filePath.replace("\\Ori\\", "\\Thumb\\")
|
|
||||||
let {err, data} = await file2base64(filePath);
|
let {err, data} = await file2base64(filePath);
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log("文件转base64失败", err)
|
console.log("文件转base64失败", err)
|
||||||
@ -116,16 +123,6 @@ export class OB11Constructor {
|
|||||||
resMsg.message.push(message_data);
|
resMsg.message.push(message_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// if (msgHistory.length > 10000) {
|
|
||||||
// msgHistory.splice(0, 100)
|
|
||||||
// }
|
|
||||||
// msgHistory.push(message)
|
|
||||||
// if (!reportSelfMessage && onebot_message_data["user_id"] == self_qq) {
|
|
||||||
// console.log("开启了不上传自己发送的消息,进行拦截 ", onebot_message_data);
|
|
||||||
// } else {
|
|
||||||
// console.log("发送上传消息给ipc main", onebot_message_data);
|
|
||||||
// window.llonebot.postData(onebot_message_data);
|
|
||||||
// }
|
|
||||||
return resMsg;
|
return resMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,12 @@ const fs = require("fs").promises;
|
|||||||
export async function uri2local(fileName: string, uri: string){
|
export async function uri2local(fileName: string, uri: string){
|
||||||
let filePath = path.join(CONFIG_DIR, fileName)
|
let filePath = path.join(CONFIG_DIR, fileName)
|
||||||
let url = new URL(uri);
|
let url = new URL(uri);
|
||||||
|
let res = {
|
||||||
|
success: false,
|
||||||
|
errMsg: "",
|
||||||
|
path: "",
|
||||||
|
isLocal: false
|
||||||
|
}
|
||||||
if (url.protocol == "base64:") {
|
if (url.protocol == "base64:") {
|
||||||
// base64转成文件
|
// base64转成文件
|
||||||
let base64Data = uri.split("base64://")[1]
|
let base64Data = uri.split("base64://")[1]
|
||||||
@ -13,51 +19,43 @@ export async function uri2local(fileName: string, uri: string){
|
|||||||
const buffer = Buffer.from(base64Data, 'base64');
|
const buffer = Buffer.from(base64Data, 'base64');
|
||||||
await fs.writeFile(filePath, buffer);
|
await fs.writeFile(filePath, buffer);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return {
|
res.errMsg = `base64文件下载失败,` + e.toString()
|
||||||
success: false,
|
return res
|
||||||
errMsg: `base64文件下载失败,` + e.toString(),
|
|
||||||
path: ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (url.protocol == "http:" || url.protocol == "https:") {
|
} else if (url.protocol == "http:" || url.protocol == "https:") {
|
||||||
// 下载文件
|
// 下载文件
|
||||||
let res = await fetch(url)
|
let fetchRes = await fetch(url)
|
||||||
if (!res.ok) {
|
if (!fetchRes.ok) {
|
||||||
return {
|
res.errMsg = `${url}下载失败,` + fetchRes.statusText
|
||||||
success: false,
|
return res
|
||||||
errMsg: `${url}下载失败,` + res.statusText,
|
|
||||||
path: ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
let blob = await res.blob();
|
let blob = await fetchRes.blob();
|
||||||
let buffer = await blob.arrayBuffer();
|
let buffer = await blob.arrayBuffer();
|
||||||
try {
|
try {
|
||||||
await fs.writeFile(filePath, Buffer.from(buffer));
|
await fs.writeFile(filePath, Buffer.from(buffer));
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
return {
|
res.errMsg = `${url}下载失败,` + e.toString()
|
||||||
success: false,
|
return res
|
||||||
errMsg: `${url}下载失败,` + e.toString(),
|
|
||||||
path: ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (url.protocol === "file:"){
|
} else if (url.protocol === "file:"){
|
||||||
await fs.copyFile(url.pathname, filePath);
|
// await fs.copyFile(url.pathname, filePath);
|
||||||
// filePath = (await NTQQApi.uploadFile(url.pathname)).path;
|
if (process.platform === "win32"){
|
||||||
|
filePath = url.pathname.slice(1)
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
filePath = url.pathname
|
||||||
|
}
|
||||||
|
res.isLocal = true
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
return {
|
res.errMsg = `不支持的file协议,` + url.protocol
|
||||||
success: false,
|
return res
|
||||||
errMsg: `不支持的file协议,` + url.protocol,
|
|
||||||
path: ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (isGIF(filePath)) {
|
if (isGIF(filePath) && !res.isLocal) {
|
||||||
await fs.rename(filePath, filePath + ".gif");
|
await fs.rename(filePath, filePath + ".gif");
|
||||||
filePath += ".gif";
|
filePath += ".gif";
|
||||||
}
|
}
|
||||||
return {
|
res.success = true
|
||||||
success: true,
|
res.path = filePath
|
||||||
errMsg: "",
|
return res
|
||||||
path: filePath
|
|
||||||
};
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user