🐞 fix: 修复 napcat 无法上传云盘的问题 for patch-1

This commit is contained in:
zhiyu1998 2024-11-13 17:50:00 +08:00
parent ca12caa75e
commit 079ef66f33
3 changed files with 62 additions and 20 deletions

View File

@ -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 { COMMON_USER_AGENT, REDIS_YUNZAI_ISOVERSEA, REDIS_YUNZAI_SONGINFO, REDIS_YUNZAI_CLOUDSONGLIST } from "../constants/constant.js";
import { downloadAudio, retryAxiosReq } from "../utils/common.js"; import { downloadAudio, retryAxiosReq } from "../utils/common.js";
import { redisExistKey, redisGetKey, redisSetKey } from "../utils/redis-util.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 { sendMusicCard, getGroupFileUrl } from "../utils/yunzai-util.js";
import config from "../model/config.js"; import config from "../model/config.js";
import FormData from 'form-data'; import FormData from 'form-data';
@ -409,17 +409,22 @@ export class songRequest extends plugin {
// 群文件上传云盘 // 群文件上传云盘
async getLatestDocument(e) { async getLatestDocument(e) {
const autoSelectNeteaseApi = await this.pickApi() const autoSelectNeteaseApi = await this.pickApi();
const cleanPath = await getGroupFileUrl(e) let { cleanPath, file_id } = await getGroupFileUrl(e);
// 拓展名 // Napcat 解决方案
const extension = cleanPath.match(/\.\w+$/); if (cleanPath.startsWith("https")) {
// 获取文件路径 const fileIdMatch = file_id.match(/\.(.*?)\.(\w+)$/);
const dirPath = cleanPath.substring(0, cleanPath.lastIndexOf('/')); const songName = fileIdMatch[1]; // 提取的歌曲名称
// 获取文件名 const fileFormat = fileIdMatch[2]; // 提取的文件格式
const fileName = cleanPath.split('/').pop().replace(/\.\w+$/, ''); cleanPath = await downloadAudio(cleanPath, this.getCurDownloadPath(e), songName, "manual", fileFormat);
// 进行文件名拆解 }
const parts = fileName.trim().match(/^([\s\S]+)\s*-\s*([\s\S]+)$/); logger.info(cleanPath);
const newFileName = dirPath + '/' + parts[2].replace(/^\s+|\s+$/g, '') + extension // 使用 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) { if (parts) {
const tags = { const tags = {

View File

@ -185,3 +185,20 @@ export async function getMediaFilesAndOthers(folderPath) {
handleError(err); 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 };
});
}

View File

@ -65,7 +65,27 @@ export async function getGroupFileUrl(e, count = 10) {
"group_id": e.group_id, "group_id": e.group_id,
"file_id": file_id "file_id": file_id
}); });
let path = decodeURIComponent(latestFileUrl.data.url) let cleanPath = decodeURIComponent(latestFileUrl.data.url)
const cleanPath = path.startsWith('file:///') ? path.replace('file:///', '') : path; // 适配 低版本 Napcat 例如3.6.4
return cleanPath 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 };
} }