diff --git a/apps/songRequest.js b/apps/songRequest.js index ed4a761..1ad2e3c 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 } from "../utils/file.js"; +import { checkAndRemoveFile, splitPaths } from "../utils/file.js"; import { sendMusicCard, getGroupFileUrl } from "../utils/yunzai-util.js"; import config from "../model/config.js"; import FormData from 'form-data'; @@ -355,7 +355,7 @@ export class songRequest extends plugin { } this.songCloudUpdate(e); return res; - + } else { throw new Error('上传失败,响应不正确'); } @@ -409,17 +409,22 @@ export class songRequest extends plugin { // 群文件上传云盘 async getLatestDocument(e) { - const autoSelectNeteaseApi = await this.pickApi() - const cleanPath = await getGroupFileUrl(e) - // 拓展名 - const extension = cleanPath.match(/\.\w+$/); - // 获取文件路径 - const dirPath = cleanPath.substring(0, cleanPath.lastIndexOf('/')); - // 获取文件名 - const fileName = cleanPath.split('/').pop().replace(/\.\w+$/, ''); - // 进行文件名拆解 - const parts = fileName.trim().match(/^([\s\S]+)\s*-\s*([\s\S]+)$/); - const newFileName = dirPath + '/' + parts[2].replace(/^\s+|\s+$/g, '') + extension + const autoSelectNeteaseApi = await this.pickApi(); + let { cleanPath, file_id } = await getGroupFileUrl(e); + // Napcat 解决方案 + if (cleanPath.startsWith("https")) { + const fileIdMatch = file_id.match(/\.(.*?)\.(\w+)$/); + const songName = fileIdMatch[1]; // 提取的歌曲名称 + const fileFormat = fileIdMatch[2]; // 提取的文件格式 + cleanPath = await downloadAudio(cleanPath, this.getCurDownloadPath(e), songName, "manual", fileFormat); + } + logger.info(cleanPath); + // 使用 splitPaths 提取信息 + const [{ dir: dirPath, fileName, extension, baseFileName }] = splitPaths(cleanPath); + // 文件名拆解为两部分 + const parts = baseFileName.trim().match(/^([\s\S]+)\s*-\s*([\s\S]+)$/); + // 生成新文件名 + const newFileName = `${dirPath}/${parts[2].trim()}${extension}`; // 进行元数据编辑 if (parts) { const tags = { @@ -680,4 +685,4 @@ export class songRequest extends plugin { await e.group.sendFile(path); } } -} \ No newline at end of file +} diff --git a/utils/file.js b/utils/file.js index f584de3..773da12 100644 --- a/utils/file.js +++ b/utils/file.js @@ -185,3 +185,20 @@ export async function getMediaFilesAndOthers(folderPath) { handleError(err); } } + +/** + * 将输入统一为数组形式,方便处理单个和多个路径 + * @param input + * @returns {{fileName: *, dir: *}[]} + */ +export function splitPaths(input) { + const paths = Array.isArray(input) ? input : [input]; + + return paths.map(filePath => { + const dir = path.dirname(filePath); + const fileName = path.basename(filePath); + const extension = path.extname(fileName); + const baseFileName = path.basename(fileName, extension); // 去除扩展名的文件名 + return { dir, fileName, extension, baseFileName }; + }); +} diff --git a/utils/yunzai-util.js b/utils/yunzai-util.js index e0df574..b059872 100644 --- a/utils/yunzai-util.js +++ b/utils/yunzai-util.js @@ -16,8 +16,8 @@ export function textArrayToMakeForward(e, textArray) { /** * 发送群组音乐卡片 * @param e - * @param platformType 音乐平台 - * @param musicId 音乐id + * @param platformType 音乐平台 + * @param musicId 音乐id */ export async function sendMusicCard(e, platformType, musicId) { @@ -65,7 +65,27 @@ export async function getGroupFileUrl(e, count = 10) { "group_id": e.group_id, "file_id": file_id }); - let path = decodeURIComponent(latestFileUrl.data.url) - const cleanPath = path.startsWith('file:///') ? path.replace('file:///', '') : path; - return cleanPath -} \ No newline at end of file + let cleanPath = decodeURIComponent(latestFileUrl.data.url) + // 适配 低版本 Napcat 例如:3.6.4 + if (cleanPath.startsWith("https")) { + // https://njc-download.ftn.qq.com/.... + const urlObj = new URL(cleanPath); + // 检查URL中是否包含 fname 参数 + if (urlObj.searchParams.has('fname')) { + // 获取 fname 参数的值 + const originalFname = urlObj.searchParams.get('fname'); + + // 提取 file_id(第一个"."后面的内容) + const fileId = file_id.split('.').slice(1).join('.'); // 分割并去掉第一个部分 + urlObj.searchParams.set('fname', `${originalFname}${fileId}`); + return { + cleanPath: urlObj.toString(), + file_id + }; + } + } else if (cleanPath.startsWith('file:///')) { + cleanPath.replace('file:///', '') + } + + return { cleanPath, file_id }; +}