diff --git a/lib/bot.js b/lib/bot.js index 179e84fe..b9aadbf0 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -389,17 +389,29 @@ export default class Yunzai extends EventEmitter { } async Buffer(data, opts = {}) { - if (Buffer.isBuffer(data)) return data - data = this.String(data) - - if (data.startsWith("base64://")) - return Buffer.from(data.replace("base64://", ""), "base64") - else if (data.match(/^https?:\/\//)) - return opts.http ? data : Buffer.from(await (await fetch(data, opts)).arrayBuffer()) - else { - const file = data.replace(/^file:\/\//, "") - if (await this.fsStat(file)) - return opts.file ? `file://${path.resolve(file)}` : fs.readFile(file) + if (!Buffer.isBuffer(data)) { + data = this.String(data) + if (data.startsWith("base64://")) { + data = Buffer.from(data.replace("base64://", ""), "base64") + } else if (data.match(/^https?:\/\//)) { + if (opts.http) return data + data = Buffer.from(await (await fetch(data, opts)).arrayBuffer()) + } else { + const file = data.replace(/^file:\/\//, "") + if (await this.fsStat(file)) { + if (opts.file) return `file://${path.resolve(file)}` + const buffer = await fs.readFile(file) + if (typeof opts.size === "number" && buffer.length > opts.size) + return `file://${path.resolve(file)}` + return buffer + } + } + } + + if (typeof opts.size === "number" && data.length > opts.size) { + const file = path.join("temp", ulid()) + await fs.writeFile(file, data) + data = `file://${path.resolve(file)}` } return data } @@ -412,12 +424,16 @@ export default class Yunzai extends EventEmitter { file.buffer = data.file } else { file.url = data.file.replace(/^base64:\/\/.*/, "base64://...") - file.buffer = await this.Buffer(data.file, opts) + file.buffer = await this.Buffer(data.file, { + ...opts, size: undefined, + }) } if (Buffer.isBuffer(file.buffer)) { - file.type = await fileTypeFromBuffer(file.buffer) + file.type = await fileTypeFromBuffer(file.buffer) || {} file.md5 = md5(file.buffer) file.name ??= `${Date.now().toString(36)}.${file.md5.slice(0,8)}.${file.type.ext}` + if (typeof opts.size === "number" && file.buffer.length > opts.size) + file.buffer = await this.Buffer(data.file, opts) } } catch (err) { this.makeLog("error", ["文件类型检测错误", file, err]) diff --git a/plugins/adapter/ComWeChat.js b/plugins/adapter/ComWeChat.js index ffb336ed..4d30f791 100644 --- a/plugins/adapter/ComWeChat.js +++ b/plugins/adapter/ComWeChat.js @@ -33,7 +33,9 @@ Bot.adapter.push(new class ComWeChatAdapter { } async uploadFile(data, file) { - file = await Bot.fileType(file, { http: true }) + file = await Bot.fileType(file, { + http: true, size: 10485760, + }) const opts = { name: file.name } if (Buffer.isBuffer(file.buffer)) { diff --git a/plugins/adapter/GSUIDCore.js b/plugins/adapter/GSUIDCore.js index 92b03038..8e51db6e 100644 --- a/plugins/adapter/GSUIDCore.js +++ b/plugins/adapter/GSUIDCore.js @@ -62,7 +62,9 @@ Bot.adapter.push(new class GSUIDCoreAdapter { i = { type: "text", text: i } if (i.file) { - i.file = await Bot.Buffer(i.file, { http: true }) + i.file = await Bot.Buffer(i.file, { + http: true, size: 10485760, + }) if (Buffer.isBuffer(i.file)) i.file = `base64://${i.file.toString("base64")}` } diff --git a/plugins/adapter/OPQBot.js b/plugins/adapter/OPQBot.js index c7740d15..2242344d 100644 --- a/plugins/adapter/OPQBot.js +++ b/plugins/adapter/OPQBot.js @@ -38,7 +38,9 @@ Bot.adapter.push(new class OPQBotAdapter { async uploadFile(id, type, file) { const opts = { CommandId: this.CommandId[type] } - file = await Bot.Buffer(file, { http: true }) + file = await Bot.Buffer(file, { + http: true, size: 10485760, + }) if (Buffer.isBuffer(file)) opts.Base64Buf = file.toString("base64") else if (file.match(/^https?:\/\//)) diff --git a/plugins/adapter/OneBotv11.js b/plugins/adapter/OneBotv11.js index 248e6db9..5803775c 100644 --- a/plugins/adapter/OneBotv11.js +++ b/plugins/adapter/OneBotv11.js @@ -33,9 +33,11 @@ Bot.adapter.push(new class OneBotv11Adapter { } async makeFile(file, opts) { - file = await Bot.Buffer(file, { http: true, ...opts }) + file = await Bot.Buffer(file, { + http: true, size: 10485760, ...opts, + }) if (Buffer.isBuffer(file)) - file = `base64://${file.toString("base64")}` + return `base64://${file.toString("base64")}` return file }