2022-02-10 01:14:52 +08:00
|
|
|
const Logger = require('../lib/utils/logger');
|
2022-09-29 01:56:29 +08:00
|
|
|
const axios = require('axios').default;
|
2022-02-10 01:14:52 +08:00
|
|
|
const config = require('../config');
|
2022-09-29 01:56:29 +08:00
|
|
|
const cp = require('child_process');
|
2022-02-10 01:14:52 +08:00
|
|
|
const fs = require('fs');
|
2022-09-29 01:56:29 +08:00
|
|
|
const path = require('path');
|
2022-02-10 01:14:52 +08:00
|
|
|
const redis = require('redis');
|
2022-09-29 01:56:29 +08:00
|
|
|
const sanitize = require('sanitize-filename');
|
|
|
|
const stream = require('stream');
|
2022-11-12 00:51:30 +08:00
|
|
|
const WorkerStarter = require('../lib/utils/worker-starter');
|
2022-12-19 02:43:14 +08:00
|
|
|
const {PresAnnStatusMsg} = require('../lib/utils/message-builder');
|
2022-11-12 00:51:30 +08:00
|
|
|
const {workerData} = require('worker_threads');
|
2022-09-29 01:56:29 +08:00
|
|
|
const {promisify} = require('util');
|
2022-02-10 01:14:52 +08:00
|
|
|
|
2022-11-12 00:51:30 +08:00
|
|
|
const jobId = workerData.jobId;
|
2022-02-10 01:14:52 +08:00
|
|
|
const logger = new Logger('presAnn Collector');
|
2022-09-29 22:32:49 +08:00
|
|
|
logger.info(`Collecting job ${jobId}`);
|
2022-02-10 01:14:52 +08:00
|
|
|
|
2022-06-28 19:37:29 +08:00
|
|
|
const dropbox = path.join(config.shared.presAnnDropboxDir, jobId);
|
2022-02-10 01:14:52 +08:00
|
|
|
|
2022-02-13 04:03:07 +08:00
|
|
|
// Takes the Job from the dropbox
|
2022-07-27 22:49:25 +08:00
|
|
|
const job = fs.readFileSync(path.join(dropbox, 'job'));
|
|
|
|
const exportJob = JSON.parse(job);
|
2022-11-11 21:57:23 +08:00
|
|
|
const jobType = exportJob.jobType;
|
2022-02-10 19:48:54 +08:00
|
|
|
|
2022-09-29 01:56:29 +08:00
|
|
|
async function collectAnnotationsFromRedis() {
|
2022-07-27 22:49:25 +08:00
|
|
|
const client = redis.createClient({
|
|
|
|
host: config.redis.host,
|
|
|
|
port: config.redis.port,
|
|
|
|
password: config.redis.password,
|
|
|
|
});
|
2022-02-10 19:48:54 +08:00
|
|
|
|
2022-07-27 22:49:25 +08:00
|
|
|
client.on('error', (err) => logger.info('Redis Client Error', err));
|
2022-06-02 05:22:08 +08:00
|
|
|
|
2022-07-27 22:49:25 +08:00
|
|
|
await client.connect();
|
2022-06-28 19:37:29 +08:00
|
|
|
|
2022-09-29 22:32:49 +08:00
|
|
|
const presAnn = await client.hGetAll(jobId);
|
2022-07-27 22:49:25 +08:00
|
|
|
|
|
|
|
// Remove annotations from Redis
|
|
|
|
await client.del(jobId);
|
|
|
|
|
|
|
|
const annotations = JSON.stringify(presAnn);
|
2022-02-10 01:14:52 +08:00
|
|
|
|
2022-07-27 22:49:25 +08:00
|
|
|
const whiteboard = JSON.parse(annotations);
|
|
|
|
const pages = JSON.parse(whiteboard.pages);
|
|
|
|
|
|
|
|
fs.writeFile(path.join(dropbox, 'whiteboard'), annotations, function(err) {
|
|
|
|
if (err) {
|
|
|
|
return logger.error(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Collect the presentation page files (PDF / PNG / JPEG)
|
|
|
|
// from the presentation directory
|
|
|
|
const presFile = path.join(exportJob.presLocation, exportJob.presId);
|
|
|
|
const pdfFile = `${presFile}.pdf`;
|
|
|
|
|
2022-08-17 02:27:40 +08:00
|
|
|
// Message to display conversion progress toast
|
2022-12-19 02:43:14 +08:00
|
|
|
const statusUpdate = new PresAnnStatusMsg(exportJob);
|
2022-08-17 02:27:40 +08:00
|
|
|
|
2022-07-27 22:49:25 +08:00
|
|
|
if (fs.existsSync(pdfFile)) {
|
|
|
|
for (const p of pages) {
|
|
|
|
const pageNumber = p.page;
|
|
|
|
const outputFile = path.join(dropbox, `slide${pageNumber}`);
|
|
|
|
|
|
|
|
// CairoSVG doesn't handle transparent SVG and PNG embeds properly,
|
|
|
|
// e.g., in rasterized text. So textboxes may get a black background
|
|
|
|
// when downloading/exporting repeatedly. To avoid that, we take slides
|
|
|
|
// from the uploaded file, but later probe the dimensions from the SVG
|
|
|
|
// so it matches what was shown in the browser.
|
|
|
|
|
|
|
|
const extract_png_from_pdf = [
|
|
|
|
'-png',
|
|
|
|
'-f', pageNumber,
|
|
|
|
'-l', pageNumber,
|
|
|
|
'-scale-to', config.collector.pngWidthRasterizedSlides,
|
|
|
|
'-singlefile',
|
|
|
|
'-cropbox',
|
|
|
|
pdfFile, outputFile,
|
2022-07-28 02:54:16 +08:00
|
|
|
];
|
2022-07-27 22:49:25 +08:00
|
|
|
|
2022-08-17 02:27:40 +08:00
|
|
|
try {
|
|
|
|
cp.spawnSync(config.shared.pdftocairo, extract_png_from_pdf, {shell: false});
|
|
|
|
} catch (error) {
|
2022-12-19 02:43:14 +08:00
|
|
|
logger.error(`PDFtoCairo failed extracting slide ${pageNumber} in job ${jobId}: ${error.message}`);
|
|
|
|
statusUpdate.setError();
|
2022-08-17 02:27:40 +08:00
|
|
|
}
|
|
|
|
|
2022-12-19 02:43:14 +08:00
|
|
|
await client.publish(config.redis.channels.publish, statusUpdate.build(pageNumber));
|
2022-07-27 22:49:25 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-05-05 02:37:32 +08:00
|
|
|
const imageName = 'slide1';
|
|
|
|
|
2022-12-19 02:43:14 +08:00
|
|
|
if (fs.existsSync(`${presFile}.png`)) {
|
2023-05-05 02:37:32 +08:00
|
|
|
fs.copyFileSync(`${presFile}.png`, path.join(dropbox, `${imageName}.png`));
|
2022-12-19 02:43:14 +08:00
|
|
|
} else if (fs.existsSync(`${presFile}.jpeg`)) {
|
2023-05-05 02:37:32 +08:00
|
|
|
fs.copyFileSync(`${presFile}.jpeg`, path.join(dropbox, `${imageName}.jpeg`));
|
|
|
|
} else if (fs.existsSync(`${presFile}.jpg`)) {
|
|
|
|
// JPG file available: copy changing extension to JPEG
|
|
|
|
fs.copyFileSync(`${presFile}.jpg`, path.join(dropbox, `${imageName}.jpeg`));
|
2022-12-19 02:43:14 +08:00
|
|
|
} else {
|
|
|
|
await client.publish(config.redis.channels.publish, statusUpdate.build());
|
|
|
|
client.disconnect();
|
2023-05-05 02:37:32 +08:00
|
|
|
return logger.error(`No PDF, PNG, JPG or JPEG file available for job ${jobId}`);
|
2022-12-19 02:43:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
await client.publish(config.redis.channels.publish, statusUpdate.build());
|
2022-07-27 22:49:25 +08:00
|
|
|
}
|
|
|
|
|
2022-08-17 02:27:40 +08:00
|
|
|
client.disconnect();
|
2022-11-12 00:51:30 +08:00
|
|
|
|
2022-12-19 02:43:14 +08:00
|
|
|
const process = new WorkerStarter({jobId});
|
2022-11-12 00:51:30 +08:00
|
|
|
process.process();
|
2022-09-29 22:32:49 +08:00
|
|
|
}
|
2022-09-29 01:56:29 +08:00
|
|
|
|
|
|
|
async function sleep(ms) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Export shared notes via bbb-pads in the desired format
|
|
|
|
* @param {Integer} retries - Number of retries to get the shared notes
|
|
|
|
*/
|
2022-11-11 06:34:19 +08:00
|
|
|
async function collectSharedNotes(retries = 3) {
|
2022-09-29 01:56:29 +08:00
|
|
|
/** One of the following formats is supported:
|
|
|
|
etherpad / html / pdf / txt / doc / odf */
|
|
|
|
|
|
|
|
const padId = exportJob.presId;
|
|
|
|
const notesFormat = 'pdf';
|
|
|
|
|
2022-10-20 01:09:11 +08:00
|
|
|
const filename = `${sanitize(exportJob.filename.replace(/\s/g, '_'))}.${notesFormat}`;
|
2022-09-29 01:56:29 +08:00
|
|
|
const notes_endpoint = `${config.bbbPadsAPI}/p/${padId}/export/${notesFormat}`;
|
|
|
|
const filePath = path.join(dropbox, filename);
|
|
|
|
|
|
|
|
const finishedDownload = promisify(stream.finished);
|
|
|
|
const writer = fs.createWriteStream(filePath);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await axios({
|
|
|
|
method: 'GET',
|
|
|
|
url: notes_endpoint,
|
|
|
|
responseType: 'stream',
|
|
|
|
});
|
|
|
|
response.data.pipe(writer);
|
|
|
|
await finishedDownload(writer);
|
|
|
|
} catch (err) {
|
2022-11-04 00:41:51 +08:00
|
|
|
if (retries > 0 && err?.response?.status == 429) {
|
|
|
|
// Wait for the bbb-pads API to be available due to rate limiting
|
|
|
|
const backoff = err.response.headers['retry-after'] * 1000;
|
|
|
|
logger.info(`Retrying ${jobId} in ${backoff}ms...`);
|
|
|
|
await sleep(backoff);
|
2022-09-29 01:56:29 +08:00
|
|
|
return collectSharedNotes(retries - 1);
|
|
|
|
} else {
|
|
|
|
logger.error(`Could not download notes in job ${jobId}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 00:51:30 +08:00
|
|
|
const notifier = new WorkerStarter({jobType, jobId, filename});
|
|
|
|
notifier.notify();
|
2022-09-29 22:32:49 +08:00
|
|
|
}
|
2022-09-29 01:56:29 +08:00
|
|
|
|
2022-11-11 21:57:23 +08:00
|
|
|
switch (jobType) {
|
2022-09-29 01:56:29 +08:00
|
|
|
case 'PresentationWithAnnotationExportJob': return collectAnnotationsFromRedis();
|
|
|
|
case 'PresentationWithAnnotationDownloadJob': return collectAnnotationsFromRedis();
|
2022-11-11 06:34:19 +08:00
|
|
|
case 'PadCaptureJob': return collectSharedNotes();
|
2022-11-11 21:57:23 +08:00
|
|
|
default: return logger.error(`Unknown job type ${jobType}`);
|
2022-09-29 01:56:29 +08:00
|
|
|
}
|