From cacd10e32b5007c939e58d9306cb955f2c37895c Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Wed, 13 Nov 2024 22:24:55 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=8E=88=20pref=EF=BC=9A=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E4=BA=91=E7=9B=98=E5=A4=B1=E8=B4=A5=E5=90=8E=E5=86=8D?= =?UTF-8?q?=E6=AC=A1=E4=B8=8A=E4=BC=A0=E6=80=A7=E8=83=BD=EF=BC=9B=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=B8=80=E4=B8=AA=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E7=9A=84=E5=87=BD=E6=95=B0`checkFileExists`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/songRequest.js | 15 ++++++++++++--- utils/file.js | 28 +++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/apps/songRequest.js b/apps/songRequest.js index 1ad2e3c..9603296 100644 --- a/apps/songRequest.js +++ b/apps/songRequest.js @@ -8,7 +8,7 @@ import { NETEASE_API_CN, NETEASE_SONG_DOWNLOAD, NETEASE_TEMP_API } from "../cons import { COMMON_USER_AGENT, REDIS_YUNZAI_ISOVERSEA, REDIS_YUNZAI_SONGINFO, REDIS_YUNZAI_CLOUDSONGLIST } from "../constants/constant.js"; import { downloadAudio, retryAxiosReq } from "../utils/common.js"; import { redisExistKey, redisGetKey, redisSetKey } from "../utils/redis-util.js"; -import { checkAndRemoveFile, splitPaths } from "../utils/file.js"; +import { checkAndRemoveFile, checkFileExists, splitPaths } from "../utils/file.js"; import { sendMusicCard, getGroupFileUrl } from "../utils/yunzai-util.js"; import config from "../model/config.js"; import FormData from 'form-data'; @@ -416,9 +416,18 @@ export class songRequest extends plugin { const fileIdMatch = file_id.match(/\.(.*?)\.(\w+)$/); const songName = fileIdMatch[1]; // 提取的歌曲名称 const fileFormat = fileIdMatch[2]; // 提取的文件格式 - cleanPath = await downloadAudio(cleanPath, this.getCurDownloadPath(e), songName, "manual", fileFormat); + // 检测文件是否存在 已提升性能 + if (await checkFileExists(cleanPath)) { + // 如果文件已存在 + logger.mark(`[R插件][云盘] 上传路径审计:已存在下载文件`); + cleanPath = `${this.getCurDownloadPath(e)}/${songName}.${fileFormat}`; + } else { + // 如果文件不存在 + logger.mark(`[R插件][云盘] 上传路径审计:不存在下载文件,将进行下载...`); + cleanPath = await downloadAudio(cleanPath, this.getCurDownloadPath(e), songName, "manual", fileFormat); + } } - logger.info(cleanPath); + logger.info(`[R插件][云盘] 上传路径审计: ${ cleanPath }`); // 使用 splitPaths 提取信息 const [{ dir: dirPath, fileName, extension, baseFileName }] = splitPaths(cleanPath); // 文件名拆解为两部分 diff --git a/utils/file.js b/utils/file.js index 773da12..060403e 100644 --- a/utils/file.js +++ b/utils/file.js @@ -12,12 +12,29 @@ const mimeTypes = { // 添加其他文件类型和MIME类型的映射 }; -// 通用错误处理函数 +/** + * 通用错误处理函数 + * @param err + */ function handleError(err) { logger.error(`错误: ${ err.message }\n堆栈: ${ err.stack }`); throw err; } +/** + * 异步的方式检查文件是否存在 + * @param filePath + * @returns {Promise} + */ +export async function checkFileExists(filePath) { + try { + await fs.access(filePath); + return true; // 文件存在 + } catch (error) { + return false; // 文件不存在 + } +} + /** * 检查文件是否存在并且删除 * @param {string} file - 文件路径 @@ -188,8 +205,13 @@ export async function getMediaFilesAndOthers(folderPath) { /** * 将输入统一为数组形式,方便处理单个和多个路径 - * @param input - * @returns {{fileName: *, dir: *}[]} + * @param {string|string[]} input - 一个或多个文件路径 + * + * fileName:文件的完整名称,包括文件名和扩展名 + * extension:文件的扩展名 + * dir:文件所在的目录路径 + * baseFileName:不包含扩展名的文件名 + * @returns {{fileName: string, extension: string, dir: string, baseFileName: string}[]} - 一个包含文件信息的对象数组 */ export function splitPaths(input) { const paths = Array.isArray(input) ? input : [input]; From 1123e089b21ba72a6d06fb071c171483e98c0fb5 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Wed, 13 Nov 2024 22:29:46 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=AA=9B=20chore:=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=20await?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/songRequest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/songRequest.js b/apps/songRequest.js index 9603296..6d330fb 100644 --- a/apps/songRequest.js +++ b/apps/songRequest.js @@ -108,7 +108,7 @@ export class songRequest extends plugin { // 获取云盘歌单列表 const cloudSongList = await this.getCloudSong() // 搜索云盘歌单并进行搜索 - const matchedSongs = await cloudSongList.filter(({ songName, singerName }) => + const matchedSongs = cloudSongList.filter(({ songName, singerName }) => songName.includes(songKeyWord) || singerName.includes(songKeyWord) || songName == songKeyWord || singerName == songKeyWord ); // 计算列表数 From 7cedd641ef508ff0cb836b7fd2c69c715678f711 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Wed, 13 Nov 2024 23:02:58 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E8=8E=B7=E5=8F=96=E7=BE=A4=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=9C=80=E6=96=B0=E7=9A=84=E5=9B=BE=E7=89=87=E7=9A=84=E5=B7=A5?= =?UTF-8?q?=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/yunzai-util.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/utils/yunzai-util.js b/utils/yunzai-util.js index 3267c1e..3932b46 100644 --- a/utils/yunzai-util.js +++ b/utils/yunzai-util.js @@ -19,7 +19,6 @@ export function textArrayToMakeForward(e, textArray) { * @param platformType 音乐平台 * @param musicId 音乐id */ - export async function sendMusicCard(e, platformType, musicId) { await e.bot.sendApi('send_group_msg', { group_id: e.group.group_id, @@ -35,13 +34,34 @@ export async function sendMusicCard(e, platformType, musicId) { }); } +/** + * 获取群文件最新的图片 + * @param e + * @param count + * @returns {Promise<*|string>} + */ +export async function getLatestImage(e, count = 10) { + // 获取最新的聊天记录,阈值为5 + const latestChat = await e.bot.sendApi("get_group_msg_history", { + "group_id": e.group_id, + "count": count + }); + const messages = latestChat.data.messages; + // 找到最新的图片 + for (let i = messages.length - 1; i >= 0; i--) { + const message = messages?.[i]?.message; + if (message?.[0]?.type === "image") { + return message?.[0].data?.url; + } + } + return ""; +} /** * 获取群文件Url地址 * @param e * @param count 获取群聊条数 */ - export async function getGroupFileUrl(e, count = 10) { const latestChat = await e.bot.sendApi("get_group_msg_history", { "group_id": e.group_id, From d5a4fac04009a9fd91c92b8ffc1a27164ed307a8 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Wed, 13 Nov 2024 23:03:41 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E8=8E=B7=E5=8F=96=E7=BE=A4=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=9C=80=E6=96=B0=E7=9A=84=E5=9B=BE=E7=89=87=E7=9A=84=E5=B7=A5?= =?UTF-8?q?=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/yunzai-util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/yunzai-util.js b/utils/yunzai-util.js index 3932b46..e2d865c 100644 --- a/utils/yunzai-util.js +++ b/utils/yunzai-util.js @@ -37,7 +37,7 @@ export async function sendMusicCard(e, platformType, musicId) { /** * 获取群文件最新的图片 * @param e - * @param count + * @param count 获取群聊条数 * @returns {Promise<*|string>} */ export async function getLatestImage(e, count = 10) { From c47b8859475e02bcbf7d4e4c490047e7cd2e20c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E5=88=80=E9=B1=BC?= <9903082+qdyovo@user.noreply.gitee.com> Date: Thu, 14 Nov 2024 11:43:16 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=90=9E=20fix:=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=A4=9A=E4=BD=99=E7=9A=84=E5=BC=95=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/songRequest.js | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/songRequest.js b/apps/songRequest.js index 6d330fb..2de52ba 100644 --- a/apps/songRequest.js +++ b/apps/songRequest.js @@ -13,7 +13,6 @@ import { sendMusicCard, getGroupFileUrl } from "../utils/yunzai-util.js"; import config from "../model/config.js"; import FormData from 'form-data'; import NodeID3 from 'node-id3'; -import { isError } from "node:util"; let FileSuffix = 'flac'