Skip to content

Commit

Permalink
fixed a problem when uploading file smaller than 10MB
Browse files Browse the repository at this point in the history
  • Loading branch information
TheodoreKrypton committed Mar 30, 2024
1 parent 6b2afc8 commit 0435c6b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/api/client/message-api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<number> {
Expand Down
59 changes: 31 additions & 28 deletions src/api/client/message-api/file-uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,18 @@ export abstract class FileUploader<T extends GeneralFileMessage> {
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`,
Expand Down Expand Up @@ -116,33 +122,30 @@ export abstract class FileUploader<T extends GeneralFileMessage> {
try {
this.fileName = fileName ?? this.defaultFileName;

if (isBig(this.fileSize)) {
const createWorker = async (workerId: number): Promise<void> => {
try {
while (!this.done()) {
await this.uploadNextPart(workerId);
if (callback) {
callback(this.uploaded, this.fileSize);
}
const createWorker = async (workerId: number): Promise<void> => {
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<Promise<void>> = [];
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<Promise<void>> = [];
for (let i = 0; i < workers; i++) {
promises.push(createWorker(i));
}

await Promise.all(promises);
} finally {
this.close();
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/impl/gramjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
);
Expand Down
2 changes: 1 addition & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit 0435c6b

Please sign in to comment.