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

[NEW] Upload service #27543

Merged
merged 5 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/meteor/server/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { IVideoConfService } from './types/IVideoConfService';
import type { ISAUMonitorService } from './types/ISAUMonitorService';
import type { IDeviceManagementService } from './types/IDeviceManagementService';
import { FibersContextStore } from './lib/ContextStore';
import type { IUploadService } from './types/IUploadService';

// TODO think in a way to not have to pass the service name to proxify here as well
export const Authorization = proxifyWithWait<IAuthorization>('authorization');
Expand All @@ -40,7 +41,7 @@ export const LDAP = proxifyWithWait<ILDAPService>('ldap');
export const SAUMonitor = proxifyWithWait<ISAUMonitorService>('sau-monitor');
export const DeviceManagement = proxifyWithWait<IDeviceManagementService>('device-management');
export const VideoConf = proxifyWithWait<IVideoConfService>('video-conference');

export const Upload = proxifyWithWait<IUploadService>('upload');
// Calls without wait. Means that the service is optional and the result may be an error
// of service/method not available
export const EnterpriseSettings = proxify<IEnterpriseSettings>('ee-settings');
Expand Down
27 changes: 27 additions & 0 deletions apps/meteor/server/sdk/types/IUploadService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { IUploadDetails } from '@rocket.chat/apps-engine/definition/uploads/IUploadDetails';
import type { IMessage, IUpload } from '@rocket.chat/core-typings';

export interface IUploadFileParams {
userId: string;
buffer: Buffer;
details: Partial<IUploadDetails>;
}
export interface ISendFileMessageParams {
roomId: string;
userId: string;
file: IUpload;
message?: IMessage;
}

export interface ISendFileLivechatMessageParams {
roomId: string;
visitorToken: string;
file: IUpload;
message?: IMessage;
}

export interface IUploadService {
uploadFile(params: IUploadFileParams): Promise<IUpload>;
sendFileMessage(params: ISendFileMessageParams): Promise<IMessage | undefined>;
sendFileLivechatMessage(params: ISendFileLivechatMessageParams): Promise<IMessage | undefined>;
}
2 changes: 2 additions & 0 deletions apps/meteor/server/services/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { isRunningMs } from '../lib/isRunningMs';
import { PushService } from './push/service';
import { DeviceManagementService } from './device-management/service';
import { FederationService } from './federation/service';
import { UploadService } from './upload/service';

const { db } = MongoInternals.defaultRemoteCollectionDriver().mongo;

Expand All @@ -43,6 +44,7 @@ api.registerService(new PushService());
api.registerService(new DeviceManagementService());
api.registerService(new VideoConfService());
api.registerService(new FederationService());
api.registerService(new UploadService());

// if the process is running in micro services mode we don't need to register services that will run separately
if (!isRunningMs()) {
Expand Down
39 changes: 39 additions & 0 deletions apps/meteor/server/services/upload/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { IMessage, IUpload } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';

import { ServiceClassInternal } from '../../sdk/types/ServiceClass';
import type {
ISendFileLivechatMessageParams,
ISendFileMessageParams,
IUploadFileParams,
IUploadService,
} from '../../sdk/types/IUploadService';
import { FileUpload } from '../../../app/file-upload/server';

export class UploadService extends ServiceClassInternal implements IUploadService {
protected name = 'upload';

async uploadFile({ buffer, details }: IUploadFileParams): Promise<IUpload> {
const fileStore = FileUpload.getStore('Uploads');
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
return fileStore.insert(details, buffer);
}

async sendFileMessage({ roomId, file, userId, message }: ISendFileMessageParams): Promise<IMessage | undefined> {
return Meteor.runAsUser(userId, () => Meteor.call('sendFileMessage', roomId, null, file, message));
}

async sendFileLivechatMessage({ roomId, visitorToken, file, message }: ISendFileLivechatMessageParams): Promise<IMessage> {
return Meteor.call('sendFileLivechatMessage', roomId, visitorToken, file, message);
}
KevLehman marked this conversation as resolved.
Show resolved Hide resolved

async getFileBuffer({ file }: { userId: string; file: IUpload }): Promise<Buffer> {
return new Promise((resolve, reject) => {
FileUpload.getBuffer(file, (err: Error, buffer: Buffer) => {
if (err) {
return reject(err);
}
return resolve(buffer);
});
});
}
}