2022-02-27 00:52:05 +08:00
|
|
|
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');
|
2022-02-27 00:52:05 +08:00
|
|
|
const axios = require('axios').default;
|
2022-04-29 19:50:42 +08:00
|
|
|
const path = require('path');
|
2022-02-27 00:52:05 +08:00
|
|
|
|
2022-08-10 21:03:26 +08:00
|
|
|
const {workerData} = require('worker_threads');
|
2022-11-11 21:57:23 +08:00
|
|
|
const [jobType, jobId, filename] = [workerData.jobType, workerData.jobId, workerData.filename];
|
2022-02-27 00:52:05 +08:00
|
|
|
|
|
|
|
const logger = new Logger('presAnn Notifier Worker');
|
|
|
|
|
2022-07-27 22:49:25 +08:00
|
|
|
const dropbox = `${config.shared.presAnnDropboxDir}/${jobId}`;
|
|
|
|
const job = fs.readFileSync(path.join(dropbox, 'job'));
|
|
|
|
const exportJob = JSON.parse(job);
|
2022-02-27 00:52:05 +08:00
|
|
|
|
2022-07-27 22:49:25 +08:00
|
|
|
/** Notify Meeting Actor of file availability by
|
|
|
|
* sending a message through Redis PubSub */
|
2022-04-12 23:40:17 +08:00
|
|
|
async function notifyMeetingActor() {
|
2022-07-27 22:49:25 +08:00
|
|
|
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-08-10 20:30:39 +08:00
|
|
|
|
2022-10-22 17:19:39 +08:00
|
|
|
const link = config.bbbWebPublicAPI + path.join('presentation',
|
2022-08-10 20:30:39 +08:00
|
|
|
exportJob.parentMeetingId, exportJob.parentMeetingId,
|
2022-09-29 01:56:29 +08:00
|
|
|
exportJob.presId, 'pdf', jobId, filename);
|
2022-08-10 20:30:39 +08:00
|
|
|
|
2022-07-27 22:49:25 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-29 01:56:29 +08:00
|
|
|
/** Upload PDF to a BBB room
|
|
|
|
* @param {String} filePath - Absolute path to the file, including the extension
|
|
|
|
*/
|
|
|
|
async function upload(filePath) {
|
2022-08-10 20:30:39 +08:00
|
|
|
const callbackUrl = `${config.bbbWebAPI}/bigbluebutton/presentation/${exportJob.presentationUploadToken}/upload`;
|
2022-07-27 22:49:25 +08:00
|
|
|
const formData = new FormData();
|
|
|
|
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);
|
2022-09-29 01:56:29 +08:00
|
|
|
formData.append('fileUpload', fs.createReadStream(filePath));
|
2022-07-27 22:49:25 +08:00
|
|
|
|
2022-09-29 01:56:29 +08:00
|
|
|
try {
|
|
|
|
const res = await axios.post(callbackUrl, formData,
|
|
|
|
{headers: formData.getHeaders()});
|
|
|
|
logger.info(`Upload of job ${exportJob.jobId} returned ${res.data}`);
|
|
|
|
} catch (error) {
|
2022-10-19 19:36:25 +08:00
|
|
|
return logger.error(`Could not upload job ${exportJob.jobId}: ${error}`);
|
2022-09-29 01:56:29 +08:00
|
|
|
}
|
2022-02-27 00:52:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (jobType == 'PresentationWithAnnotationDownloadJob') {
|
2022-07-27 22:49:25 +08:00
|
|
|
notifyMeetingActor();
|
2022-02-27 00:52:05 +08:00
|
|
|
} else if (jobType == 'PresentationWithAnnotationExportJob') {
|
2022-09-29 01:56:29 +08:00
|
|
|
const filePath = `${exportJob.presLocation}/pdfs/${jobId}/${filename}`;
|
|
|
|
upload(filePath);
|
|
|
|
} else if (jobType == 'PadCaptureJob') {
|
|
|
|
const filePath = `${dropbox}/${filename}`;
|
|
|
|
upload(filePath);
|
2022-02-27 00:52:05 +08:00
|
|
|
} else {
|
2022-07-27 22:49:25 +08:00
|
|
|
logger.error(`Notifier received unknown job type ${jobType}`);
|
2022-02-27 00:52:05 +08:00
|
|
|
}
|
|
|
|
|
2022-07-09 20:15:17 +08:00
|
|
|
// Delete temporary files
|
2022-07-27 22:49:25 +08:00
|
|
|
fs.rm(dropbox, {recursive: true}, (err) => {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|