-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} | ||
} |