From 321e867d817962432372fec813d37d14dcd6d17b Mon Sep 17 00:00:00 2001 From: Clansty Date: Tue, 8 Mar 2022 23:22:15 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E7=9A=84=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/Telegram.ts | 16 ---- src/client/TelegramChat.ts | 20 +++++ tools/import/importer.ts | 180 +++++++++++++++++++------------------ 3 files changed, 113 insertions(+), 103 deletions(-) diff --git a/src/client/Telegram.ts b/src/client/Telegram.ts index cccbbcf..7fd2726 100644 --- a/src/client/Telegram.ts +++ b/src/client/Telegram.ts @@ -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; type ServiceMessageHandler = (message: Api.MessageService) => Promise; @@ -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); - } } diff --git a/src/client/TelegramChat.ts b/src/client/TelegramChat.ts index f70cb86..b82434f 100644 --- a/src/client/TelegramChat.ts +++ b/src/client/TelegramChat.ts @@ -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); + } } diff --git a/tools/import/importer.ts b/tools/import/importer.ts index e1b5ded..06f4295 100644 --- a/tools/import/importer.ts +++ b/tools/import/importer.ts @@ -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('导入成功!'); }, };