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

106 lines
2.8 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';
2016-11-12 03:02:46 +08:00
import RedisPubSub from '/imports/startup/server/redis';
2016-05-17 02:05:44 +08:00
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';
2016-05-13 01:43:59 +08:00
2016-11-12 03:02:46 +08:00
const requestWhiteboardHistory = (meetingId, slideId) => {
const REDIS_CONFIG = Meteor.settings.redis;
const CHANNEL = REDIS_CONFIG.channels.toBBBApps.whiteboard;
const EVENT_NAME = 'request_whiteboard_annotation_history_request';
2017-06-03 03:25:02 +08:00
const payload = {
2016-11-12 03:02:46 +08:00
meeting_id: meetingId,
requester_id: 'nodeJSapp',
whiteboard_id: slideId,
reply_to: `${meetingId}/nodeJSapp`,
};
return RedisPubSub.publish(CHANNEL, EVENT_NAME, payload);
};
2016-10-21 19:41:17 +08:00
const SUPPORTED_TYPES = [SVG, PNG];
2016-07-27 01:33:35 +08:00
2016-11-12 03:02:46 +08:00
export default function addSlide(meetingId, presentationId, slide) {
2016-10-21 19:41:17 +08:00
check(meetingId, String);
check(presentationId, String);
check(slide, Object);
2016-07-27 01:33:35 +08:00
2016-10-21 19:41:17 +08:00
const selector = {
meetingId,
presentationId,
'slide.id': slide.id,
};
2016-07-27 01:33:35 +08:00
2016-10-21 19:41:17 +08:00
const imageUri = slide.svg_uri || slide.png_uri;
2016-07-27 01:47:36 +08:00
2016-10-21 19:41:17 +08:00
const modifier = {
$set: {
2016-11-12 03:02:46 +08:00
meetingId,
presentationId,
2016-10-21 19:41:17 +08:00
slide: {
2016-11-12 03:02:46 +08:00
id: slide.id,
2016-10-21 19:41:17 +08:00
height_ratio: slide.height_ratio,
y_offset: slide.y_offset,
num: slide.num,
x_offset: slide.x_offset,
current: slide.current,
img_uri: imageUri,
txt_uri: slide.txt_uri,
width_ratio: slide.width_ratio,
swf_uri: slide.swf_uri,
thumb_uri: slide.thumb_uri,
width: slide.width,
height: slide.height,
},
},
};
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, slide.id);
2016-10-21 19:41:17 +08:00
if (insertedId) {
2016-11-12 03:02:46 +08:00
return Logger.info(`Added slide id=${slide.id} to presentation=${presentationId}`);
}
if (numChanged) {
return Logger.info(`Upserted slide id=${slide.id} to presentation=${presentationId}`);
2016-07-27 01:33:35 +08:00
}
2016-10-21 19:41:17 +08:00
};
2016-05-13 01:43:59 +08:00
2016-11-12 03:02:46 +08:00
return fetchImageSizes(imageUri)
.then(({ width, height }) => {
modifier.$set.slide.width = width;
modifier.$set.slide.height = height;
return Slides.upsert(selector, modifier, cb);
})
.catch(reason =>
Logger.error(`Error parsing image size. ${reason}. slide=${slide.id} uri=${imageUri}`));
2017-06-03 03:25:02 +08:00
}
2016-10-21 19:41:17 +08:00
2017-06-03 03:25:02 +08:00
const fetchImageSizes = imageUri =>
2016-10-21 19:41:17 +08:00
probe(imageUri)
2017-06-03 03:25:02 +08:00
.then((result) => {
2016-10-21 19:41:17 +08:00
if (!SUPPORTED_TYPES.includes(result.mime)) {
throw `Invalid image type, received ${result.mime} expecting ${SUPPORTED_TYPES.join()}`;
}
2016-11-12 03:02:46 +08:00
return {
width: result.width,
height: result.height,
};
2016-10-21 19:41:17 +08:00
})
2017-06-03 03:25:02 +08:00
.catch((reason) => {
2016-11-15 06:24:24 +08:00
Logger.error(`Error parsing image size. ${reason}. uri=${imageUri}`);
2016-10-21 19:41:17 +08:00
return reason;
});