diff --git a/apps/songRequest.js b/apps/songRequest.js index c7aa6aa..2de52ba 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'; @@ -107,7 +107,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 ); // 计算列表数 @@ -415,9 +415,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]; diff --git a/utils/yunzai-util.js b/utils/yunzai-util.js index 3267c1e..e2d865c 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,