Skip to content

Commit

Permalink
perf: 自动发送 50MB 以下文件到 tg
Browse files Browse the repository at this point in the history
close #27
  • Loading branch information
clansty committed Aug 1, 2022
1 parent cbae097 commit 72a30a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/helpers/forwardHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ const htmlEscape = (text: string) =>
.replace(/>/g, '>');

export default {
async downloadToCustomFile(url: string, allowWebp = false) {
async downloadToCustomFile(url: string, allowWebp = false, filename?: string) {
const { fileTypeFromBuffer } = await (Function('return import("file-type")')() as Promise<typeof import('file-type')>);
const file = await fetchFile(url);
if (filename) {
return new CustomFile(filename, file.length, '', file);
}
const type = await fileTypeFromBuffer(file);
if (allowWebp) {
return new CustomFile(`image.${type.ext}`, file.length, '', file);
Expand Down
18 changes: 12 additions & 6 deletions src/services/ForwardService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ export default class ForwardService {
}
case 'file': {
const extName = path.extname(elem.name);
if (exts.images.includes(extName.toLowerCase())) {
// 50M 以下文件下载转发
if (elem.size < 1024 * 1024 * 50 || exts.images.includes(extName.toLowerCase())) {
// 是图片
const url = await pair.qq.getFileUrl(elem.fid);
let url = await pair.qq.getFileUrl(elem.fid);
if (url.includes('?fname=')) {
url = url.split('?fname=')[0];
// Request path contains unescaped characters
}
this.log.info('正在发送媒体,长度', helper.hSize(elem.size));
try {
files.push(await helper.downloadToCustomFile(url, !(message || messageHeader)));
files.push(await helper.downloadToCustomFile(url, !(message || messageHeader), elem.name));
}
catch (e) {
this.log.error('下载媒体失败', e);
Expand Down Expand Up @@ -307,8 +313,8 @@ export default class ForwardService {
}
else if (message.video || message.videoNote || message.gif) {
const file = message.video || message.videoNote || message.gif;
if (file.size.gt(20 * 1024 * 1024)) {
chain.push('[视频大于 20MB]');
if (file.size.gt(50 * 1024 * 1024)) {
chain.push('[视频大于 50MB]');
}
else if (file.mimeType === 'video/webm') {
// 把 webm 转换成 gif
Expand Down Expand Up @@ -397,7 +403,7 @@ export default class ForwardService {
chain.push(`文件:${fileNameAttribute ? fileNameAttribute.fileName : ''}\n` +
`类型:${file.mimeType}\n` +
`大小:${file.size}`);
if (file.size.leq(20 * 1024 * 1024)) {
if (file.size.leq(50 * 1024 * 1024)) {
chain.push('\n文件正在上传中…');
if (pair.qq instanceof Group) {
pair.qq.fs.upload(await message.downloadMedia({}), '/',
Expand Down

0 comments on commit 72a30a0

Please sign in to comment.