bigbluebutton-Github/bigbluebutton-html5/imports/api/slides/server/modifiers/addSlide.js

136 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-10-21 19:41:17 +08:00
import probe from 'probe-image-size';
2016-11-12 03:02:46 +08:00
import { Meteor } from 'meteor/meteor';
2016-10-21 19:41:17 +08:00
import { check } from 'meteor/check';
2017-10-12 08:59:35 +08:00
import flat from 'flat';
import RedisPubSub from '/imports/startup/server/redis';
import { Slides } from '/imports/api/slides';
2016-10-21 19:41:17 +08:00
import Logger from '/imports/startup/server/logger';
import { SVG, PNG } from '/imports/utils/mimeTypes';
import calculateSlideData from '/imports/api/slides/server/helpers';
import addSlidePositions from './addSlidePositions';
2016-05-13 01:43:59 +08:00
const loadSlidesFromHttpAlways = Meteor.settings.private.app.loadSlidesFromHttpAlways || false;
2016-11-12 03:02:46 +08:00
const requestWhiteboardHistory = (meetingId, slideId) => {
const REDIS_CONFIG = Meteor.settings.private.redis;
2017-10-12 08:59:35 +08:00
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
const EVENT_NAME = 'GetWhiteboardAnnotationsReqMsg';
const USER_ID = 'nodeJSapp';
2016-11-12 03:02:46 +08:00
2017-06-03 03:25:02 +08:00
const payload = {
2017-10-12 08:59:35 +08:00
whiteboardId: slideId,
2016-11-12 03:02:46 +08:00
};
2017-10-12 08:59:35 +08:00
return RedisPubSub.publishUserMessage(CHANNEL, EVENT_NAME, meetingId, USER_ID, payload);
2016-11-12 03:02:46 +08:00
};
2016-10-21 19:41:17 +08:00
const SUPPORTED_TYPES = [SVG, PNG];
2016-07-27 01:33:35 +08:00
const fetchImageSizes = imageUri => probe(imageUri)
.then((result) => {
if (!SUPPORTED_TYPES.includes(result.mime)) {
throw new Meteor.Error('invalid-image-type', `received ${result.mime} expecting ${SUPPORTED_TYPES.join()}`);
}
2017-10-12 08:59:35 +08:00
return {
width: result.width,
height: result.height,
};
})
.catch((reason) => {
Logger.error(`Error parsing image size. ${reason}. uri=${imageUri}`);
return reason;
});
2017-10-12 08:59:35 +08:00
export default function addSlide(meetingId, podId, presentationId, slide) {
check(podId, String);
2016-10-21 19:41:17 +08:00
check(presentationId, String);
2017-10-12 08:59:35 +08:00
check(slide, {
id: String,
num: Number,
thumbUri: String,
swfUri: String,
txtUri: String,
svgUri: String,
current: Boolean,
xOffset: Number,
yOffset: Number,
widthRatio: Number,
heightRatio: Number,
content: String,
2017-10-12 08:59:35 +08:00
});
2016-07-27 01:33:35 +08:00
const {
id: slideId,
xOffset,
yOffset,
widthRatio,
heightRatio,
...restSlide
} = slide;
2016-10-21 19:41:17 +08:00
const selector = {
meetingId,
podId,
2016-10-21 19:41:17 +08:00
presentationId,
id: slideId,
2016-10-21 19:41:17 +08:00
};
2016-07-27 01:33:35 +08:00
2017-10-12 08:59:35 +08:00
const imageUri = slide.svgUri || slide.pngUri;
2016-07-27 01:47:36 +08:00
2016-10-21 19:41:17 +08:00
const modifier = {
2017-10-12 08:59:35 +08:00
$set: Object.assign(
{ meetingId },
{ podId },
2017-10-12 08:59:35 +08:00
{ presentationId },
{ id: slideId },
{ imageUri },
flat(restSlide),
{ safe: true },
2017-10-12 08:59:35 +08:00
),
2016-10-21 19:41:17 +08:00
};
2016-07-27 01:47:36 +08:00
2016-10-21 19:41:17 +08:00
const cb = (err, numChanged) => {
if (err) {
return Logger.error(`Adding slide to collection: ${err}`);
}
const { insertedId } = numChanged;
2016-07-27 01:33:35 +08:00
requestWhiteboardHistory(meetingId, slideId);
2016-10-21 19:41:17 +08:00
if (insertedId) {
return Logger.info(`Added slide id=${slideId} pod=${podId} presentation=${presentationId}`);
2016-11-12 03:02:46 +08:00
}
return Logger.info(`Upserted slide id=${slideId} pod=${podId} presentation=${presentationId}`);
2016-10-21 19:41:17 +08:00
};
2016-05-13 01:43:59 +08:00
const imageSizeUri = (loadSlidesFromHttpAlways ? imageUri.replace(/^https/i, 'http') : imageUri);
return fetchImageSizes(imageSizeUri)
2016-11-12 03:02:46 +08:00
.then(({ width, height }) => {
// there is a rare case when for a very long not-active meeting the presentation
// files just disappear and width/height can't be retrieved
if (width && height) {
// pre-calculating the width, height, and vieBox coordinates / dimensions
// to unload the client-side
const slideData = {
width,
height,
xOffset,
yOffset,
widthRatio,
heightRatio,
};
const slidePosition = calculateSlideData(slideData);
addSlidePositions(meetingId, podId, presentationId, slideId, slidePosition);
2017-10-12 08:59:35 +08:00
}
2016-11-12 03:02:46 +08:00
return Slides.upsert(selector, modifier, cb);
})
.catch(reason => Logger.error(`Error parsing image size. ${reason}. slide=${slideId} uri=${imageUri}`));
2017-06-03 03:25:02 +08:00
}