Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
fix: 修复发送文件链接不带文件格式时无法正确解析的问题 & 移除 fetch 请求库 使用原生支持
Browse files Browse the repository at this point in the history
  • Loading branch information
JdesEva committed Oct 27, 2023
1 parent efdb9e0 commit b0b86b6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"express": "^4.18.2",
"file-type": "^18.5.0",
"form-data": "^4.0.0",
"mime": "^3.0.0",
"multer": "^1.4.5-lts.1",
"node-fetch-commonjs": "^3.3.2",
"wechaty": "^1.20.2"
}
}
24 changes: 3 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/route/msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ module.exports = function registerPushHook({ app, bot }) {
}

const msgReceiver = isRoom ?
await bot.Room.find({ topic: to }) :
await bot.Contact.find(Utils.equalTrueType(to, 'object')? to : { name: to })
await bot.Room.find({ topic: to }) : await bot.Contact.find(Utils.equalTrueType(to, 'object') ? to : { name: to })

if (msgReceiver) {
const sendStatus = await Service.formatAndSendMsg({ bot, type, content, msgInstance: msgReceiver, res })
Expand Down
1 change: 0 additions & 1 deletion src/service/webhook.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const fetch = require('node-fetch-commonjs')
const FormData = require('form-data')
const chalk = require('chalk')
const {
Expand Down
21 changes: 17 additions & 4 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const { FileBox } = require('file-box') // hack: wechaty use Instance to Export File-box
const fetch = require('node-fetch-commonjs')
const MIME = require('mime')
const path = require('path')

const downloadFile = async fileUrl => {
try {
const response = await fetch(fileUrl);
if (response.ok) {
return await response.buffer();
const arrayBuffer = await response.arrayBuffer();
return {
type: response.headers.get('content-type'),
buffer: Buffer.from(arrayBuffer)
}
}
return null;
} catch (error) {
Expand All @@ -21,10 +26,18 @@ const equalTrueType = function (val, expectType) {
//http://www.baidu.com/image.png?a=1 => image.png
const getFileNameFromUrl = url => url.match(/.*\/([^/?]*)/)?.[1] || ''

// 格式化文件
// 有些资源文件链接是不会返回文件后缀的 例如 https://pangji-home.com/Fi5DimeGHBLQ3KcELn3DolvENjVU 其实是一张图片
const formatUrlToName = (url, mimeType) => {
const extension = MIME.getExtension(mimeType)
const baseFileName = getFileNameFromUrl(url)
return path.extname(url) ? baseFileName : `${baseFileName}.${extension}`
}

// bugfix: use `fileBox.fromUrl` api to get image is OK, but sometimes directly to get cloudflare img may return a 0 bites response.(when response is 301)
const getMediaFromUrl = async url => {
const buffer = await downloadFile(url)
return FileBox.fromBuffer(buffer, getFileNameFromUrl(url))
const { buffer, type } = await downloadFile(url)
return FileBox.fromBuffer(buffer, formatUrlToName(url, type))
}
const getBufferFile = formDataFile => {
return FileBox.fromBuffer(formDataFile.buffer, formDataFile.originalname)
Expand Down

0 comments on commit b0b86b6

Please sign in to comment.