Skip to content

Commit

Permalink
[NEW][Apps] Add a new upload API (#18955)
Browse files Browse the repository at this point in the history
Co-authored-by: Douglas Gubert <[email protected]>
  • Loading branch information
Shiqi Mei and d-gubert authored Sep 22, 2020
1 parent 995b4e3 commit b77070b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 39 deletions.
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

0 comments on commit b77070b

Please sign in to comment.