Skip to content

Commit

Permalink
细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeRainStarSky committed Sep 23, 2024
1 parent 3339324 commit e445376
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
42 changes: 29 additions & 13 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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])
Expand Down
4 changes: 3 additions & 1 deletion plugins/adapter/ComWeChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 3 additions & 1 deletion plugins/adapter/GSUIDCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")}`
}
Expand Down
4 changes: 3 additions & 1 deletion plugins/adapter/OPQBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?:\/\//))
Expand Down
6 changes: 4 additions & 2 deletions plugins/adapter/OneBotv11.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit e445376

Please sign in to comment.