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][Apps] Add a new upload API #18955

Merged
merged 10 commits into from
Sep 22, 2020
19 changes: 19 additions & 0 deletions app/apps/lib/misc/determineFileType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fileType from 'file-type';

import { mime as MIME } from '../../../utils/lib/mimeTypes';

export function determineFileType(buffer, details) {
const mime = MIME.lookup(details.name);

if (mime) {
return Array.isArray(mime) ? mime[0] : mime;
}

const detectedType = fileType(buffer);

if (detectedType) {
return detectedType.mime;
}

return 'application/octet-stream';
}
20 changes: 20 additions & 0 deletions app/apps/server/bridges/uploads.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Meteor } from 'meteor/meteor';

import { FileUpload } from '../../../file-upload/server';
import { determineFileType } from '../../lib/misc/determineFileType';

export class AppUploadBridge {
constructor(orch) {
Expand Down Expand Up @@ -26,4 +29,21 @@ export class AppUploadBridge {
});
});
}

createUpload(details, buffer, appId) {
this.orch.debugLog(`The App ${ appId } is creating an upload "${ details.name }"`);

details.type = determineFileType(buffer, details);

const fileStore = FileUpload.getStore('Uploads');
const uploadedFile = fileStore.insertSync(details, buffer);

if (details.userId) {
Meteor.runAsUser(details.userId, () => {
Meteor.call('sendFileMessage', details.rid, null, uploadedFile);
});
}

return this.orch.getConverters().get('uploads').convertToApp(uploadedFile);
}
}
2 changes: 1 addition & 1 deletion app/file-upload/server/lib/FileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const FileUpload = {
const room = Rooms.findOneById(file.rid);
const directMessageAllowed = settings.get('FileUpload_Enabled_Direct');
const fileUploadAllowed = settings.get('FileUpload_Enabled');
if (canAccessRoom(room, user, file) !== true) {
if (user.type !== 'app' && canAccessRoom(room, user, file) !== true) {
return false;
}
const language = user ? user.language : 'en';
Expand Down
58 changes: 21 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"@nivo/heatmap": "^0.61.0",
"@nivo/line": "^0.61.1",
"@nivo/pie": "^0.61.1",
"@rocket.chat/apps-engine": "1.18.0-beta.3837",
"@rocket.chat/apps-engine": "1.18.0-beta.3848",
"@rocket.chat/css-in-js": "^0.14.1",
"@rocket.chat/fuselage": "^0.14.1",
"@rocket.chat/fuselage-hooks": "^0.14.1",
Expand Down