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

Regression: File upload via apps not working in some scenarios #18995

Merged
merged 10 commits into from
Sep 29, 2020
29 changes: 17 additions & 12 deletions app/apps/server/bridges/uploads.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,36 @@ export class AppUploadBridge {
});
}

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

if (!details.userId && !details.visitorToken) {
throw new Error('Missing user to perform the upload operation');
}

const fileStore = FileUpload.getStore('Uploads');
const insertSync = Meteor.wrapAsync(fileStore.insert.bind(fileStore));

details.type = determineFileType(buffer, details);

if (details.visitorToken) {
delete details.userId;
const uploadedFile = insertSync(details, buffer);

Meteor.call('sendFileLivechatMessage', details.rid, details.visitorToken, uploadedFile);
return this.orch.getConverters().get('uploads').convertToApp(uploadedFile);
}

return new Promise((resolve, reject) => Meteor.runAsUser(details.userId, () => {
const fileStore = FileUpload.getStore('Uploads');
const insertSync = details.userId
? (...args) => Meteor.runAsUser(details.userId, () => fileStore.insertSync(...args))
: Meteor.wrapAsync(fileStore.insert.bind(fileStore));

details.type = determineFileType(buffer, details);

return new Promise(Meteor.bindEnvironment((resolve, reject) => {
try {
const uploadedFile = insertSync(details, buffer);

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

resolve(this.orch.getConverters().get('uploads').convertToApp(uploadedFile));
} catch (err) {
reject(err);
Expand Down
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 (user && user.type !== 'app' && canAccessRoom(room, user, file) !== true) {
if (user?.type !== 'app' && canAccessRoom(room, user, file) !== true) {
return false;
}
const language = user ? user.language : 'en';
Expand Down
6 changes: 4 additions & 2 deletions app/file-upload/server/methods/sendFileMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import { Random } from 'meteor/random';
import _ from 'underscore';

import { Uploads } from '../../../models';
import { Rooms } from '../../../models/server/raw';
import { callbacks } from '../../../callbacks';
import { FileUpload } from '../lib/FileUpload';
import { canAccessRoom } from '../../../authorization/server/functions/canAccessRoom';

Meteor.methods({
async sendFileMessage(roomId, store, file, msgData = {}) {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'sendFileMessage' });
}

const room = Meteor.call('canAccessRoom', roomId, Meteor.userId());
const room = await Rooms.findOneById(roomId);
const user = Meteor.user();

if (user && user.type !== 'app' && !room) {
if (user?.type !== 'app' && !canAccessRoom(room, user) !== true) {
return false;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are not equivalent here 😛

> var user = null
undefined
> !!(user && user.type !== 'app')
false
> !!(user?.type !== 'app')
true
>


Expand Down