From 0435c6b67edf66246cd6efcd572677335c28b856 Mon Sep 17 00:00:00 2001 From: Wheat Carrier Date: Sat, 30 Mar 2024 22:04:33 +0800 Subject: [PATCH] fixed a problem when uploading file smaller than 10MB --- src/api/client/message-api/api.ts | 3 +- src/api/client/message-api/file-uploader.ts | 59 +++++++++++---------- src/api/impl/gramjs.ts | 2 +- src/api/types.ts | 2 +- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/api/client/message-api/api.ts b/src/api/client/message-api/api.ts index 81cbdcb..006d13c 100644 --- a/src/api/client/message-api/api.ts +++ b/src/api/client/message-api/api.ts @@ -5,7 +5,6 @@ import { IBot, TDLibApi } from 'src/api/interface'; import { config } from 'src/config'; import { TechnicalError } from 'src/errors/base'; import { db } from 'src/server/manager/db'; -import { Logger } from 'src/utils/logger'; import { getUploader } from './file-uploader'; import { MessageBroker } from './message-broker'; @@ -105,7 +104,7 @@ export class MessageApi extends MessageBroker { } private static report(uploaded: number, totalSize: number) { - Logger.info(`${(uploaded / totalSize) * 100}% uploaded`); + // Logger.info(`${(uploaded / totalSize) * 100}% uploaded`); } protected async sendFile(fileMsg: GeneralFileMessage): Promise { diff --git a/src/api/client/message-api/file-uploader.ts b/src/api/client/message-api/file-uploader.ts index b8cba5d..e99a0f8 100644 --- a/src/api/client/message-api/file-uploader.ts +++ b/src/api/client/message-api/file-uploader.ts @@ -75,12 +75,18 @@ export abstract class FileUploader { let retry = 3; while (retry) { try { - const rsp = await this.client.saveBigFilePart({ - fileId: this.fileId, - filePart: this.partCnt - 1, // 0-indexed - fileTotalParts: this.parts, - bytes: chunk, - }); + const rsp = isBig(this.fileSize) + ? await this.client.saveBigFilePart({ + fileId: this.fileId, + filePart: this.partCnt - 1, // 0-indexed + fileTotalParts: this.parts, + bytes: chunk, + }) + : await this.client.saveFilePart({ + fileId: this.fileId, + filePart: this.partCnt - 1, // 0-indexed + bytes: chunk, + }); if (!rsp.success) { throw new TechnicalError( `File chunk ${this.partCnt} of ${this.fileName} failed to upload`, @@ -116,33 +122,30 @@ export abstract class FileUploader { try { this.fileName = fileName ?? this.defaultFileName; - if (isBig(this.fileSize)) { - const createWorker = async (workerId: number): Promise => { - try { - while (!this.done()) { - await this.uploadNextPart(workerId); - if (callback) { - callback(this.uploaded, this.fileSize); - } + const createWorker = async (workerId: number): Promise => { + try { + while (!this.done()) { + await this.uploadNextPart(workerId); + if (callback) { + Logger.info( + `[worker ${workerId}] ${ + this.uploaded / this.fileSize + }% uploaded`, + ); + callback(this.uploaded, this.fileSize); } - } catch (err) { - this._errors[workerId] = err; } - }; - - const promises: Array> = []; - for (let i = 0; i < workers; i++) { - promises.push(createWorker(i)); + } catch (err) { + this._errors[workerId] = err; } + }; - await Promise.all(promises); - } else { - const bytes = await this.read(this.fileSize); - await this.client.saveFilePart({ - fileId: this.fileId, - bytes, - }); + const promises: Array> = []; + for (let i = 0; i < workers; i++) { + promises.push(createWorker(i)); } + + await Promise.all(promises); } finally { this.close(); } diff --git a/src/api/impl/gramjs.ts b/src/api/impl/gramjs.ts index 403e7be..e73e0b0 100644 --- a/src/api/impl/gramjs.ts +++ b/src/api/impl/gramjs.ts @@ -157,7 +157,7 @@ export class GramJSApi implements ITDLibClient { const rsp = await this.client.invoke( new Api.upload.SaveFilePart({ fileId: req.fileId, - filePart: 0, + filePart: req.filePart, bytes: req.bytes, }), ); diff --git a/src/api/types.ts b/src/api/types.ts index 6026da7..02dc01b 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -43,10 +43,10 @@ export type PinMessageReq = Chat & Message; export type SaveFilePartReq = { fileId: BigInteger; bytes: Buffer; + filePart: number; }; export type SaveBigFilePartReq = SaveFilePartReq & { - filePart: number; fileTotalParts: number; };