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';
|
2017-10-12 10:00:28 +08:00
|
|
|
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';
|
2017-10-12 10:00:28 +08:00
|
|
|
import calculateSlideData from '/imports/api/slides/server/helpers';
|
2016-05-13 01:43:59 +08:00
|
|
|
|
2016-11-12 03:02:46 +08:00
|
|
|
const requestWhiteboardHistory = (meetingId, slideId) => {
|
2018-01-08 08:24:05 +08:00
|
|
|
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
|
|
|
|
2017-10-12 08:59: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()}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
width: result.width,
|
|
|
|
height: result.height,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch((reason) => {
|
|
|
|
Logger.error(`Error parsing image size. ${reason}. uri=${imageUri}`);
|
|
|
|
return reason;
|
|
|
|
});
|
|
|
|
|
2016-11-12 03:02:46 +08:00
|
|
|
export default function addSlide(meetingId, presentationId, slide) {
|
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,
|
|
|
|
});
|
2016-07-27 01:33:35 +08:00
|
|
|
|
2016-10-21 19:41:17 +08:00
|
|
|
const selector = {
|
|
|
|
meetingId,
|
|
|
|
presentationId,
|
2017-10-12 08:59:35 +08:00
|
|
|
id: slide.id,
|
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 },
|
|
|
|
{ presentationId },
|
|
|
|
flat(slide, { safe: true }),
|
|
|
|
),
|
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
|
|
|
|
2017-02-24 21:10:55 +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}`);
|
|
|
|
}
|
|
|
|
|
2017-10-12 08:59:35 +08:00
|
|
|
return Logger.info(`Upserted slide id=${slide.id} to presentation=${presentationId}`);
|
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 }) => {
|
2017-10-12 08:59:35 +08:00
|
|
|
// there is a rare case when for a very long not-active meeting
|
|
|
|
// the presentation files just disappear
|
|
|
|
// in that case just set the whole calculatedData to undefined
|
|
|
|
if (!width && !height) {
|
|
|
|
modifier.$set.calculatedData = undefined;
|
|
|
|
return Slides.upsert(selector, modifier, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pre-calculating the width, height, and vieBox coordinates / dimensions
|
|
|
|
// to unload the client-side
|
|
|
|
const slideData = {
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
xOffset: modifier.$set.xOffset,
|
|
|
|
yOffset: modifier.$set.yOffset,
|
|
|
|
widthRatio: modifier.$set.widthRatio,
|
|
|
|
heightRatio: modifier.$set.heightRatio,
|
|
|
|
};
|
|
|
|
modifier.$set.calculatedData = calculateSlideData(slideData);
|
|
|
|
modifier.$set.calculatedData.imageUri = imageUri;
|
|
|
|
modifier.$set.calculatedData.width = width;
|
|
|
|
modifier.$set.calculatedData.height = height;
|
2016-11-12 03:02:46 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|