Skip to content

Commit

Permalink
chore: move some file functions to upload service (RocketChat#29797)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosSpessatto authored Jan 24, 2024
1 parent bc1f26f commit 265faec
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
16 changes: 10 additions & 6 deletions apps/meteor/app/file-upload/server/methods/sendFileMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MessageAttachment, FileAttachmentProps, IUser, IUpload, AtLeast } from '@rocket.chat/core-typings';
import type { MessageAttachment, FileAttachmentProps, IUser, IUpload, AtLeast, FilesAndAttachments } from '@rocket.chat/core-typings';
import { Rooms, Uploads, Users } from '@rocket.chat/models';
import type { ServerMethods } from '@rocket.chat/ui-contexts';
import { Match, check } from 'meteor/check';
Expand All @@ -25,7 +25,7 @@ export const parseFileIntoMessageAttachments = async (
file: Partial<IUpload>,
roomId: string,
user: IUser,
): Promise<Record<string, any>> => {
): Promise<FilesAndAttachments> => {
validateFileRequiredFields(file);

await Uploads.updateFileComplete(file._id, user._id, omit(file, '_id'));
Expand All @@ -37,8 +37,10 @@ export const parseFileIntoMessageAttachments = async (
const files = [
{
_id: file._id,
name: file.name,
type: file.type,
name: file.name || '',
type: file.type || 'file',
size: file.size || 0,
format: file.identify?.format || '',
},
];

Expand Down Expand Up @@ -73,8 +75,10 @@ export const parseFileIntoMessageAttachments = async (
};
files.push({
_id: thumbnail._id,
name: file.name,
type: thumbnail.type,
name: file.name || '',
type: thumbnail.type || 'file',
size: thumbnail.size || 0,
format: thumbnail.identify?.format || '',
});
}
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@ export class RocketChatFileAdapter {
internalUser: IUser,
fileRecord: Partial<IUpload>,
): Promise<{ files: IMessage['files']; attachments: IMessage['attachments'] }> {
return new Promise<{ files: IMessage['files']; attachments: IMessage['attachments'] }>(async (resolve, reject) => {
const fileStore = FileUpload.getStore('Uploads');
const fileStore = FileUpload.getStore('Uploads');

const uploadedFile = await fileStore.insert(fileRecord, readableStream);
try {
const { files, attachments } = await parseFileIntoMessageAttachments(uploadedFile, internalRoomId, internalUser);
const uploadedFile = await fileStore.insert(fileRecord, readableStream);
const { files, attachments } = await parseFileIntoMessageAttachments(uploadedFile, internalRoomId, internalUser);

resolve({ files, attachments });
} catch (error) {
reject(error);
}
});
return { files, attachments };
}

public async getBufferFromFileRecord(fileRecord: IUpload): Promise<Buffer> {
Expand Down
14 changes: 11 additions & 3 deletions apps/meteor/server/services/upload/service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ServiceClassInternal } from '@rocket.chat/core-services';
import type { ISendFileLivechatMessageParams, ISendFileMessageParams, IUploadFileParams, IUploadService } from '@rocket.chat/core-services';
import type { IUpload } from '@rocket.chat/core-typings';
import type { IUpload, IUser, FilesAndAttachments } from '@rocket.chat/core-typings';

import { FileUpload } from '../../../app/file-upload/server';
import { sendFileMessage } from '../../../app/file-upload/server/methods/sendFileMessage';
import { parseFileIntoMessageAttachments, sendFileMessage } from '../../../app/file-upload/server/methods/sendFileMessage';
import { sendFileLivechatMessage } from '../../../app/livechat/server/methods/sendFileLivechatMessage';

export class UploadService extends ServiceClassInternal implements IUploadService {
Expand All @@ -22,12 +22,20 @@ export class UploadService extends ServiceClassInternal implements IUploadServic
return sendFileLivechatMessage({ roomId, visitorToken, file, msgData: message });
}

async getFileBuffer({ file }: { userId: string; file: IUpload }): Promise<Buffer> {
async getFileBuffer({ file }: { file: IUpload }): Promise<Buffer> {
const buffer = await FileUpload.getBuffer(file);

if (!(buffer instanceof Buffer)) {
throw new Error('Unknown error');
}
return buffer;
}

async extractMetadata(file: IUpload): Promise<{ height?: number; width?: number; format?: string }> {
return FileUpload.extractMetadata(file);
}

async parseFileIntoMessageAttachments(file: Partial<IUpload>, roomId: string, user: IUser): Promise<FilesAndAttachments> {
return parseFileIntoMessageAttachments(file, roomId, user);
}
}
6 changes: 3 additions & 3 deletions ee/packages/omnichannel-services/src/OmnichannelTranscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class OmnichannelTranscript extends ServiceClass implements IOmnichannelT
return quotes;
}

private async getMessagesData(userId: string, messages: IMessage[]): Promise<MessageData[]> {
private async getMessagesData(messages: IMessage[]): Promise<MessageData[]> {
const messagesData: MessageData[] = [];
for await (const message of messages) {
if (!message.attachments?.length) {
Expand Down Expand Up @@ -229,7 +229,7 @@ export class OmnichannelTranscript extends ServiceClass implements IOmnichannelT
continue;
}

const fileBuffer = await uploadService.getFileBuffer({ userId, file: uploadedFile });
const fileBuffer = await uploadService.getFileBuffer({ file: uploadedFile });
files.push({ name: file.name, buffer: fileBuffer, extension: uploadedFile.extension });
}

Expand Down Expand Up @@ -284,7 +284,7 @@ export class OmnichannelTranscript extends ServiceClass implements IOmnichannelT
const agent =
room.servedBy && (await Users.findOneAgentById(room.servedBy._id, { projection: { _id: 1, name: 1, username: 1, utcOffset: 1 } }));

const messagesData = await this.getMessagesData(details.userId, messages);
const messagesData = await this.getMessagesData(messages);

const [siteName, dateFormat, timeAndDateFormat, timezone, translations] = await Promise.all([
settingsService.get<string>('Site_Name'),
Expand Down
6 changes: 4 additions & 2 deletions packages/core-services/src/types/IUploadService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IUploadDetails } from '@rocket.chat/apps-engine/definition/uploads/IUploadDetails';
import type { IMessage, IUpload } from '@rocket.chat/core-typings';
import type { IMessage, IUpload, IUser, FilesAndAttachments } from '@rocket.chat/core-typings';

export interface IUploadFileParams {
userId: string;
Expand All @@ -24,5 +24,7 @@ export interface IUploadService {
uploadFile(params: IUploadFileParams): Promise<IUpload>;
sendFileMessage(params: ISendFileMessageParams): Promise<boolean | undefined>;
sendFileLivechatMessage(params: ISendFileLivechatMessageParams): Promise<boolean | undefined>;
getFileBuffer({ file }: { userId: string; file: IUpload }): Promise<Buffer>;
getFileBuffer({ file }: { file: IUpload }): Promise<Buffer>;
extractMetadata(file: IUpload): Promise<{ height?: number; width?: number; format?: string }>;
parseFileIntoMessageAttachments(file: Partial<IUpload>, roomId: string, user: IUser): Promise<FilesAndAttachments>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { FileProp } from './Files';
import type { FileAttachmentProps } from './Files/FileAttachmentProps';
import type { MessageAttachmentAction } from './MessageAttachmentAction';
import type { MessageAttachmentDefault } from './MessageAttachmentDefault';
import type { MessageQuoteAttachment } from './MessageQuoteAttachment';

export type MessageAttachment = MessageAttachmentAction | MessageAttachmentDefault | FileAttachmentProps | MessageQuoteAttachment;

export type FilesAndAttachments = {
files: FileProp[];
attachments: MessageAttachment[];
};

0 comments on commit 265faec

Please sign in to comment.