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

Commit 1f580a4

Browse files
authored
发送文件时支持别名 (#184)
❤️ contributor: @Cassius0924 ## todo @danni-cool - <del>接口参数校验检查</del> - <del> 推消息v1 接口参数检查</del> - <del> 推消息 v2 接口参数检查</del> - [x] downloadurl 解析 ? query参数 $alias - [x] 核心逻辑修改 - [x] 用例测试
2 parents 5d191f1 + f0181ee commit 1f580a4

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,22 @@ curl --location 'http://localhost:3001/webhook/msg/v2?token=[YOUR_PERSONAL_TOKEN
174174
}'
175175
```
176176

177+
##### 发文件 url 同时支持修改成目标文件名
178+
179+
> 有些情况下,直接发送 url 文件名可能不是我们想要的,给 url 拼接 query 参数 `$alias` 可用于指定发送给目标的文件名(注意:别名不做文件转换)
180+
181+
```bash
182+
curl --location 'http://localhost:3001/webhook/msg/v2?token=[YOUR_PERSONAL_TOKEN]' \
183+
--header 'Content-Type: application/json' \
184+
--data '{
185+
"to": "testUser",
186+
"data": {
187+
"type": "fileUrl" ,
188+
"content": "https://download.samplelib.com/jpeg/sample-clouds-400x300.jpg?$alias=cloud.jpg"
189+
}
190+
}'
191+
```
192+
177193
##### 发给群消息
178194

179195
```bash

src/utils/index.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
const { FileBox } = require('file-box')
22
const MIME = require('mime')
33
const { logger } = require('./log')
4+
const { URL } = require('url')
45
/**
56
* 下载媒体文件转化为Buffer
67
* @param {string} fileUrl
7-
* @returns {Promise<{buffer?: Buffer, fileName?: string}>}
8+
* @returns {Promise<{buffer?: Buffer, fileName?: string, fileNameAlias?: string}>}
89
*/
910
const downloadFile = async (fileUrl) => {
1011
try {
1112
const response = await fetch(fileUrl)
1213

1314
if (response.ok) {
1415
const buffer = Buffer.from(await response.arrayBuffer())
15-
let fileName = getFileNameFromUrl(fileUrl)
16+
// 使用自定义文件名,解决URL无文件后缀名时,文件被微信解析成不正确的后缀问题
17+
let { fileName, query } = getFileInfoFromUrl(fileUrl)
1618

1719
// deal with unValid Url format like https://pangji-home.com/Fi5DimeGHBLQ3KcELn3DolvENjVU
1820
if (fileName === '') {
@@ -24,7 +26,8 @@ const downloadFile = async (fileUrl) => {
2426

2527
return {
2628
buffer,
27-
fileName
29+
fileName,
30+
fileNameAlias: query?.$alias
2831
}
2932
}
3033

@@ -36,25 +39,29 @@ const downloadFile = async (fileUrl) => {
3639
}
3740

3841
/**
42+
* @typedef {{fileName: string, query: null | Record<string, string>} } fileInfoObj
3943
* 从url中提取文件名
4044
* @param {string} url
41-
* @returns {string}
45+
* @returns {fileInfoObj}
4246
* @example 参数 url 示例
4347
* valid: "http://www.baidu.com/image.png?a=1 => image.png"
4448
* notValid: "https://pangji-home.com/Fi5DimeGHBLQ3KcELn3DolvENjVU => ''"
4549
*/
46-
const getFileNameFromUrl = (url) => {
47-
const matchRes = url.match(/.*\/([^/?]*)/)?.[1]
48-
49-
if (matchRes === undefined) return ''
50+
const getFileInfoFromUrl = (url) => {
51+
/** @type {fileInfoObj} */
52+
let matchRes = {
53+
fileName: url.match(/.*\/([^/?]*)/)?.[1] || '', // fileName has string.string is Valid filename
54+
query: null
55+
}
5056

51-
const checkMathDotPosition = matchRes.indexOf('.')
52-
// fileName has string.string is Valid filename
53-
if (checkMathDotPosition > -1) {
54-
return matchRes
55-
} else {
56-
return ''
57+
try {
58+
const urlObj = new URL(url)
59+
matchRes.query = Object.fromEntries(urlObj.searchParams)
60+
} catch (e) {
61+
// make ts happy
5762
}
63+
64+
return matchRes
5865
}
5966

6067
/**
@@ -63,9 +70,9 @@ const getFileNameFromUrl = (url) => {
6370
* @returns {Promise<import('file-box').FileBoxInterface>}
6471
*/
6572
const getMediaFromUrl = async (url) => {
66-
const { buffer, fileName } = await downloadFile(url)
73+
const { buffer, fileName, fileNameAlias } = await downloadFile(url)
6774
//@ts-expect-errors buffer 解析是吧的情况
68-
return FileBox.fromBuffer(buffer, fileName)
75+
return FileBox.fromBuffer(buffer, fileNameAlias || fileName)
6976
}
7077

7178
/**
@@ -187,7 +194,6 @@ module.exports = {
187194
...require('./nextTick.js'),
188195
...require('./paramsValid.js'),
189196
...require('./log.js'),
190-
getFileNameFromUrl,
191197
getMediaFromUrl,
192198
getBufferFile,
193199
generateToken,

0 commit comments

Comments
 (0)