优化fanqie文件上传方式

This commit is contained in:
Jerry 2025-05-13 13:43:39 +08:00
parent 301113669e
commit 481ca177b7
3 changed files with 31 additions and 6 deletions

View File

@ -2,7 +2,10 @@ import fs from 'node:fs';
import path from 'path'; import path from 'path';
import chokidar from 'chokidar'; import chokidar from 'chokidar';
import ConfigControl from '../lib/config/configControl.js'; import ConfigControl from '../lib/config/configControl.js';
import configControl from '../lib/config/configControl.js';
import Fanqie from '../models/apps/fanqie/fanqie.js'; import Fanqie from '../models/apps/fanqie/fanqie.js';
import FromData from 'form-data';
import axios from 'axios';
let redis = global.redis; let redis = global.redis;
@ -191,16 +194,19 @@ export class xzq extends plugin {
} }
} }
const uploadOk = await this.upload(e, filePath); const uploadReturn = await this.upload(e, filePath);
this.clearCache(false, true, String(e.message_id)); await this.clearCache(false, true, String(e.message_id));
if (!uploadOk) return e.reply('上传失败', true); if (!uploadOk) return e.reply('上传失败', true);
e.reply(`${book_info.book_name}》上传成功,用时 ${(Date.now() - startTime) / 1000}s`, true); e.reply(
`${book_info.book_name}》上传成功,用时 ${(Date.now() - startTime) / 1000}s\n下载链接为${uploadReturn.url},将在10分钟后删除请及时下载`
);
return true; return true;
} }
async upload(e, filePath) { async upload(e, filePath) {
try { try {
/*
let res; let res;
if (e.isGroup) { if (e.isGroup) {
res = e.bot.sendApi('upload_group_file', { res = e.bot.sendApi('upload_group_file', {
@ -217,10 +223,28 @@ export class xzq extends plugin {
}); });
} }
return !!res; return !!res;
*/
const form = new FromData();
const fileStream = fs.createReadStream(filePath);
form.append('file', fileStream);
form.append('token', configControl.get('coreConfig')?.token);
const uploadUrl = `${configControl.get('coreConfig')?.coreUrl}/upload?dir=fanqie&expire=600`;
const response = await axios.post(uploadUrl, form, {
headers: {
...form.getHeaders(),
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
});
if (response?.success) {
const url = response.data?.url;
const message = response.data?.message;
return { url, message };
}
} catch (err) { } catch (err) {
logger.error(`文件上传错误:${logger.red(err.stack)}`); logger.error(`文件上传错误:${logger.red(err.stack)}`);
if (e?.reply) e.reply(`文件上传失败:${err.message}`, true); e.reply(`文件上传失败:${err.message}`, true);
return false; return null;
} }
} }
} }

View File

@ -16,7 +16,7 @@ export default class ReportBots extends plugin {
{ {
name: 'crystelf定时同步', name: 'crystelf定时同步',
corn: '*/30 * * * *', corn: '*/30 * * * *',
fnc: 'autoReport', fnc: () => this.autoReport(),
}, },
], ],
}); });

View File

@ -18,6 +18,7 @@
"dependencies": { "dependencies": {
"axios": "^1.8.4", "axios": "^1.8.4",
"chalk": "^5.4.1", "chalk": "^5.4.1",
"form-data": "^4.0.2",
"openai": "^4.89.0", "openai": "^4.89.0",
"ws": "^8.18.1" "ws": "^8.18.1"
}, },