Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pdf-transcript): Don't error out when trying to process an image bigger than NATS max payload #32318

Merged
merged 8 commits into from
May 9, 2024
7 changes: 7 additions & 0 deletions .changeset/silly-clocks-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rocket.chat/omnichannel-services': patch
'@rocket.chat/core-services': patch
'@rocket.chat/meteor': patch
---

Fixed error handling for files bigger than NATS max allowed payload. This should prevent PDFs from erroring out when generating from rooms that contain heavy images.
4 changes: 2 additions & 2 deletions apps/meteor/server/services/translation/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ServiceClassInternal } from '@rocket.chat/core-services';
import type { ITranslationService } from '@rocket.chat/core-services';
import type { IUser } from '@rocket.chat/core-typings';
import type { IUser, AtLeast } from '@rocket.chat/core-typings';
import { Settings } from '@rocket.chat/models';
import mem from 'mem';

Expand All @@ -22,7 +22,7 @@ export class TranslationService extends ServiceClassInternal implements ITransla
}

// Use translate when you want to translate to the user's language, or server's as a fallback
async translate(text: string, user: IUser): Promise<string> {
async translate(text: string, user: AtLeast<IUser, 'language'>): Promise<string> {
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
const language = user.language || (await this.getServerLanguageCached());

return this.translateText(text, language);
Expand Down
26 changes: 20 additions & 6 deletions ee/packages/omnichannel-services/src/OmnichannelTranscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,20 @@ export class OmnichannelTranscript extends ServiceClass implements IOmnichannelT
continue;
}

const fileBuffer = await uploadService.getFileBuffer({ file: uploadedFile });
files.push({ name: file.name, buffer: fileBuffer, extension: uploadedFile.extension });
try {
const fileBuffer = await uploadService.getFileBuffer({ file: uploadedFile });
files.push({ name: file.name, buffer: fileBuffer, extension: uploadedFile.extension });
} catch (e: unknown) {
this.log.error(`Failed to get file ${file._id}`, e);
// Push empty buffer so parser processes this as "unsupported file"
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
files.push({ name: file.name, buffer: null });

if ((e as Error).message === 'MAX_PAYLOAD_EXCEEDED') {
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
this.log.error(
`File is too big to be processed by NATS. See NATS config for allowing bigger messages to be sent between services`,
);
}
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
}
}

// When you send a file message, the things you type in the modal are not "msg", they're in "description" of the attachment
Expand Down Expand Up @@ -275,7 +287,9 @@ export class OmnichannelTranscript extends ServiceClass implements IOmnichannelT
}
this.currentJobNumber++;
try {
const room = await LivechatRooms.findOneById(details.rid);
const room = await LivechatRooms.findOneById<Pick<IOmnichannelRoom, '_id' | 'v' | 'closedAt' | 'servedBy'>>(details.rid, {
projection: { _id: 1, v: 1, closedAt: 1, servedBy: 1 },
});
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
if (!room) {
throw new Error('room-not-found');
}
Expand Down Expand Up @@ -346,11 +360,11 @@ export class OmnichannelTranscript extends ServiceClass implements IOmnichannelT

private async pdfFailed({ details, e }: { details: WorkDetailsWithSource; e: Error }): Promise<void> {
this.log.error(`Transcript for room ${details.rid} by user ${details.userId} - Failed: ${e.message}`);
const room = await LivechatRooms.findOneById(details.rid);
const room = await LivechatRooms.findOneById<Pick<IOmnichannelRoom, '_id'>>(details.rid, { projection: { _id: 1 } });
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
if (!room) {
return;
}
const user = await Users.findOneById(details.userId);
const user = await Users.findOneById<Pick<IUser, 'language' | '_id'>>(details.userId, { projection: { _id: 1, language: 1 } });
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
if (!user) {
return;
}
Expand All @@ -369,7 +383,7 @@ export class OmnichannelTranscript extends ServiceClass implements IOmnichannelT

private async pdfComplete({ details, file }: { details: WorkDetailsWithSource; file: IUpload }): Promise<void> {
this.log.info(`Transcript for room ${details.rid} by user ${details.userId} - Complete`);
const user = await Users.findOneById(details.userId);
const user = await Users.findOneById<Pick<IUser, 'language' | '_id'>>(details.userId, { projection: { _id: 1, language: 1 } });
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
if (!user) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core-services/src/types/ITranslationService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IUser } from '@rocket.chat/core-typings';
import type { AtLeast, IUser } from '@rocket.chat/core-typings';

export interface ITranslationService {
translateText(text: string, targetLanguage: string): Promise<string>;
translate(text: string, user: IUser): Promise<string>;
translate(text: string, user: AtLeast<IUser, 'language'>): Promise<string>;
translateToServerLanguage(text: string): Promise<string>;
}
Loading