mirror of https://github.com/Nofated095/Q2TG.git
perf: 优化导入的设计
This commit is contained in:
parent
8771bd9490
commit
321e867d81
|
@ -11,8 +11,6 @@ import os from 'os';
|
|||
import TelegramChat from './TelegramChat';
|
||||
import TelegramSession from '../models/TelegramSession';
|
||||
import { LogLevel } from 'telegram/extensions/Logger';
|
||||
import { CustomFile } from 'telegram/client/uploads';
|
||||
import { TelegramImportSession } from './TelegramImportSession';
|
||||
|
||||
type MessageHandler = (message: Api.Message) => Promise<boolean | void>;
|
||||
type ServiceMessageHandler = (message: Api.MessageService) => Promise<boolean | void>;
|
||||
|
@ -171,18 +169,4 @@ export default class Telegram {
|
|||
const newChat = updates.chats[0];
|
||||
return new TelegramChat(this, this.client, newChat, this.waitForMessageHelper);
|
||||
}
|
||||
|
||||
public async startImportSession(chat: TelegramChat, textFile: CustomFile, mediaCount: number) {
|
||||
const init = await this.client.invoke(
|
||||
new Api.messages.InitHistoryImport({
|
||||
peer: chat.entity,
|
||||
file: await this.client.uploadFile({
|
||||
file: textFile,
|
||||
workers: 1,
|
||||
}),
|
||||
mediaCount,
|
||||
}),
|
||||
);
|
||||
return new TelegramImportSession(chat, this.client, init.id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import { CustomFile } from 'telegram/client/uploads';
|
|||
import Telegram from './Telegram';
|
||||
import createPaginatedInlineSelector from '../utils/paginatedInlineSelector';
|
||||
import inlineDigitInput from '../utils/inlineDigitInput';
|
||||
import { TelegramImportSession } from './TelegramImportSession';
|
||||
|
||||
export default class TelegramChat {
|
||||
public readonly inputPeer: Api.TypeInputPeer;
|
||||
|
@ -204,4 +205,23 @@ export default class TelegramChat {
|
|||
})
|
||||
);
|
||||
}
|
||||
|
||||
public async startImportSession(textFile: CustomFile, mediaCount: number) {
|
||||
await this.client.invoke(
|
||||
new Api.messages.CheckHistoryImportPeer({
|
||||
peer: this.entity,
|
||||
})
|
||||
);
|
||||
const init = await this.client.invoke(
|
||||
new Api.messages.InitHistoryImport({
|
||||
peer: this.entity,
|
||||
file: await this.client.uploadFile({
|
||||
file: textFile,
|
||||
workers: 1,
|
||||
}),
|
||||
mediaCount,
|
||||
}),
|
||||
);
|
||||
return new TelegramImportSession(this, this.client, init.id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -135,101 +135,107 @@ export default {
|
|||
|
||||
console.log('正在准备导入…');
|
||||
|
||||
const newChat = await telegram.createChat(chatName);
|
||||
const txtBuffer = Buffer.from(output, 'utf-8');
|
||||
try {
|
||||
const newChat = await telegram.createChat(chatName);
|
||||
|
||||
const importSession = await telegram.startImportSession(
|
||||
newChat,
|
||||
new CustomFile('record.txt', txtBuffer.length, '', txtBuffer),
|
||||
files.size,
|
||||
);
|
||||
const importSession = await newChat.startImportSession(
|
||||
new CustomFile('record.txt', txtBuffer.length, '', txtBuffer),
|
||||
files.size,
|
||||
);
|
||||
|
||||
console.log('正在上传媒体…');
|
||||
console.log('正在上传媒体…');
|
||||
|
||||
const uploadMediaBar = new SingleBar({
|
||||
hideCursor: true,
|
||||
format: '{bar} {percentage}% | {value}/{total}',
|
||||
barsize: 120,
|
||||
}, Presets.shades_grey);
|
||||
uploadMediaBar.start(files.size, 0);
|
||||
const uploadMediaBar = new SingleBar({
|
||||
hideCursor: true,
|
||||
format: '{bar} {percentage}% | {value}/{total}',
|
||||
barsize: 120,
|
||||
}, Presets.shades_grey);
|
||||
uploadMediaBar.start(files.size, 0);
|
||||
|
||||
for (const md5 of files) {
|
||||
const fileName = md5 + '.file';
|
||||
const file = md5.startsWith('tgs') ? path.join('./assets/tgs', md5 + '.tgs') : path.join(outputPath, md5 + '.file');
|
||||
const type = md5.startsWith('tgs') ? {
|
||||
ext: 'tgs',
|
||||
mime: 'application/x-tgsticker',
|
||||
} : await fileTypeFromFile(file);
|
||||
for (const md5 of files) {
|
||||
const fileName = md5 + '.file';
|
||||
const file = md5.startsWith('tgs') ? path.join('./assets/tgs', md5 + '.tgs') : path.join(outputPath, md5 + '.file');
|
||||
const type = md5.startsWith('tgs') ? {
|
||||
ext: 'tgs',
|
||||
mime: 'application/x-tgsticker',
|
||||
} : await fileTypeFromFile(file);
|
||||
|
||||
let media: Api.TypeInputMedia;
|
||||
if (md5.startsWith('tgs') || type.ext === 'webp') {
|
||||
// 贴纸
|
||||
media = new Api.InputMediaUploadedDocument({
|
||||
file: await importSession.uploadFile(new CustomFile(
|
||||
`${fileName}.${type.ext}`,
|
||||
fs.statSync(file).size,
|
||||
file,
|
||||
)),
|
||||
mimeType: type.mime,
|
||||
attributes: [],
|
||||
});
|
||||
}
|
||||
else if (type.mime.startsWith('audio/')) {
|
||||
// 语音
|
||||
media = new Api.InputMediaUploadedDocument({
|
||||
file: await importSession.uploadFile(new CustomFile(
|
||||
`${fileName}.${type.ext}`,
|
||||
fs.statSync(file).size,
|
||||
file,
|
||||
)),
|
||||
mimeType: type.mime,
|
||||
attributes: [
|
||||
new Api.DocumentAttributeAudio({
|
||||
duration: 0,
|
||||
voice: true,
|
||||
}),
|
||||
],
|
||||
});
|
||||
}
|
||||
else if (type.ext === 'gif') {
|
||||
media = new Api.InputMediaUploadedDocument({
|
||||
file: await importSession.uploadFile(new CustomFile(
|
||||
`${fileName}.${type.ext}`,
|
||||
fs.statSync(file).size,
|
||||
file,
|
||||
)),
|
||||
mimeType: type.mime,
|
||||
attributes: [new Api.DocumentAttributeAnimated()],
|
||||
});
|
||||
}
|
||||
else if (type.mime.startsWith('image/')) {
|
||||
media = new Api.InputMediaUploadedPhoto({
|
||||
file: await importSession.uploadFile(new CustomFile(
|
||||
`${fileName}.${type.ext}`,
|
||||
fs.statSync(file).size,
|
||||
file,
|
||||
)),
|
||||
});
|
||||
}
|
||||
else {
|
||||
media = new Api.InputMediaUploadedDocument({
|
||||
file: await importSession.uploadFile(new CustomFile(
|
||||
`${fileName}.${type.ext}`,
|
||||
fs.statSync(file).size,
|
||||
file,
|
||||
)),
|
||||
mimeType: type.mime,
|
||||
attributes: [],
|
||||
});
|
||||
let media: Api.TypeInputMedia;
|
||||
if (md5.startsWith('tgs') || type.ext === 'webp') {
|
||||
// 贴纸
|
||||
media = new Api.InputMediaUploadedDocument({
|
||||
file: await importSession.uploadFile(new CustomFile(
|
||||
`${fileName}.${type.ext}`,
|
||||
fs.statSync(file).size,
|
||||
file,
|
||||
)),
|
||||
mimeType: type.mime,
|
||||
attributes: [],
|
||||
});
|
||||
}
|
||||
else if (type.mime.startsWith('audio/')) {
|
||||
// 语音
|
||||
media = new Api.InputMediaUploadedDocument({
|
||||
file: await importSession.uploadFile(new CustomFile(
|
||||
`${fileName}.${type.ext}`,
|
||||
fs.statSync(file).size,
|
||||
file,
|
||||
)),
|
||||
mimeType: type.mime,
|
||||
attributes: [
|
||||
new Api.DocumentAttributeAudio({
|
||||
duration: 0,
|
||||
voice: true,
|
||||
}),
|
||||
],
|
||||
});
|
||||
}
|
||||
else if (type.ext === 'gif') {
|
||||
media = new Api.InputMediaUploadedDocument({
|
||||
file: await importSession.uploadFile(new CustomFile(
|
||||
`${fileName}.${type.ext}`,
|
||||
fs.statSync(file).size,
|
||||
file,
|
||||
)),
|
||||
mimeType: type.mime,
|
||||
attributes: [new Api.DocumentAttributeAnimated()],
|
||||
});
|
||||
}
|
||||
else if (type.mime.startsWith('image/')) {
|
||||
media = new Api.InputMediaUploadedPhoto({
|
||||
file: await importSession.uploadFile(new CustomFile(
|
||||
`${fileName}.${type.ext}`,
|
||||
fs.statSync(file).size,
|
||||
file,
|
||||
)),
|
||||
});
|
||||
}
|
||||
else {
|
||||
media = new Api.InputMediaUploadedDocument({
|
||||
file: await importSession.uploadFile(new CustomFile(
|
||||
`${fileName}.${type.ext}`,
|
||||
fs.statSync(file).size,
|
||||
file,
|
||||
)),
|
||||
mimeType: type.mime,
|
||||
attributes: [],
|
||||
});
|
||||
}
|
||||
|
||||
await importSession.uploadMedia(fileName, media);
|
||||
uploadMediaBar.increment();
|
||||
}
|
||||
|
||||
await importSession.uploadMedia(fileName, media);
|
||||
uploadMediaBar.increment();
|
||||
await importSession.finish();
|
||||
|
||||
console.log('导入成功!');
|
||||
}
|
||||
catch (e) {
|
||||
console.error('错误', e);
|
||||
const dumpPath = path.join(outputPath, 'record');
|
||||
await fsP.writeFile(dumpPath, txtBuffer);
|
||||
console.log('临时文件位置', outputPath);
|
||||
}
|
||||
|
||||
await importSession.finish();
|
||||
await tmpDir.cleanup();
|
||||
|
||||
console.log('导入成功!');
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue