Skip to content

Commit

Permalink
[NEW] Upload service (#27543)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored Dec 20, 2022
1 parent bef27c8 commit c71714a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
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');
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);
}

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);
});
});
}
}

0 comments on commit c71714a

Please sign in to comment.