bigbluebutton-Github/bbb-export-annotations/workers/notifier.js

91 lines
2.9 KiB
JavaScript
Raw Normal View History

const Logger = require('../lib/utils/logger');
const config = require('../config');
const fs = require('fs');
const FormData = require('form-data');
2022-03-01 18:01:50 +08:00
const redis = require('redis');
const axios = require('axios').default;
const path = require('path');
const {workerData, parentPort} = require('worker_threads');
2022-07-28 02:54:16 +08:00
const [jobType, jobId, filename_with_extension] = workerData;
const logger = new Logger('presAnn Notifier Worker');
const dropbox = `${config.shared.presAnnDropboxDir}/${jobId}`;
const job = fs.readFileSync(path.join(dropbox, 'job'));
const exportJob = JSON.parse(job);
/** Notify Meeting Actor of file availability by
* sending a message through Redis PubSub */
2022-04-12 23:40:17 +08:00
async function notifyMeetingActor() {
const client = redis.createClient({
host: config.redis.host,
port: config.redis.port,
password: config.redis.password,
});
await client.connect();
client.on('error', (err) => logger.info('Redis Client Error', err));
2022-07-28 02:54:16 +08:00
const link = `${config.notifier.protocol}://${config.notifier.host}/bigbluebutton/presentation/${exportJob.parentMeetingId}/${exportJob.parentMeetingId}/${exportJob.presId}/pdf/${jobId}/${filename_with_extension}`;
const notification = {
envelope: {
name: config.notifier.msgName,
routing: {
sender: exportJob.module,
},
timestamp: (new Date()).getTime(),
},
core: {
header: {
name: config.notifier.msgName,
meetingId: exportJob.parentMeetingId,
userId: '',
},
body: {
fileURI: link,
presId: exportJob.presId,
},
},
};
logger.info(`Annotated PDF available at ${link}`);
await client.publish(config.redis.channels.publish,
JSON.stringify(notification));
client.disconnect();
2022-03-01 18:01:50 +08:00
}
/** Upload PDF to a BBB room */
async function upload() {
const callbackUrl = `http://${config.bbbWeb.host}:${config.bbbWeb.port}/bigbluebutton/presentation/${exportJob.presentationUploadToken}/upload`;
const formData = new FormData();
2022-07-28 02:54:16 +08:00
const file = `${exportJob.presLocation}/pdfs/${jobId}/${filename_with_extension}`;
formData.append('conference', exportJob.parentMeetingId);
formData.append('pod_id', config.notifier.pod_id);
formData.append('is_downloadable', config.notifier.is_downloadable);
formData.append('temporaryPresentationId', jobId);
formData.append('fileUpload', fs.createReadStream(file));
const res = await axios.post(callbackUrl, formData,
{headers: formData.getHeaders()});
logger.info(`Upload of job ${exportJob.jobId} returned ${res.data}`);
}
if (jobType == 'PresentationWithAnnotationDownloadJob') {
notifyMeetingActor();
} else if (jobType == 'PresentationWithAnnotationExportJob') {
upload();
} else {
logger.error(`Notifier received unknown job type ${jobType}`);
}
2022-07-09 20:15:17 +08:00
// Delete temporary files
fs.rm(dropbox, {recursive: true}, (err) => {
if (err) {
throw err;
}
});
2022-07-09 20:15:17 +08:00
parentPort.postMessage({message: workerData});