forked from bigbluebutton/bigbluebutton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.js
More file actions
98 lines (85 loc) · 3.25 KB
/
Copy pathnotifier.js
File metadata and controls
98 lines (85 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import Logger from '../lib/utils/logger.js';
import fs from 'fs';
import FormData from 'form-data';
import redis from 'redis';
import axios from 'axios';
import path from 'path';
import {NewPresFileAvailableMsg} from '../lib/utils/message-builder.js';
import {workerData} from 'worker_threads';
const [jobType, jobId, serverSideFilename] = [workerData.jobType,
workerData.jobId,
workerData.serverSideFilename];
const logger = new Logger('presAnn Notifier Worker');
const config = JSON.parse(fs.readFileSync('./config/settings.json', 'utf8'));
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 */
async function notifyMeetingActor() {
const client = redis.createClient({
password: config.redis.password,
socket: {
host: config.redis.host,
port: config.redis.port,
},
});
await client.connect();
client.on('error', (err) => logger.info('Redis Client Error', err));
const link = path.join(
'presentation',
exportJob.parentMeetingId, exportJob.parentMeetingId,
exportJob.presId, 'pdf', jobId, serverSideFilename);
const notification = new NewPresFileAvailableMsg(exportJob, link);
logger.info(`Annotated PDF available at ${link}`);
await client.publish(config.redis.channels.publish, notification.build());
client.disconnect();
}
/** Upload PDF to a BBB room
* @param {String} filePath - Absolute path to the file, including the extension
*/
async function upload(filePath) {
const apiPath = '/bigbluebutton/presentation/';
const uploadToken = exportJob.presentationUploadToken;
const uploadAction = '/upload';
const callbackUrl = config.bbbWebAPI + apiPath + uploadToken + uploadAction;
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);
formData.append('fileUpload', fs.createReadStream(filePath));
try {
const res = await axios.post(callbackUrl, formData,
{headers: formData.getHeaders()});
logger.info(`Upload of job ${exportJob.jobId} returned ${res.data}`);
} catch (error) {
return logger.error(`Could not upload job ${exportJob.jobId}: ${error}`);
}
}
/**
* Runs the requested notification workflow and removes temporary files.
*/
async function main() {
if (jobType == 'PresentationWithAnnotationDownloadJob') {
await notifyMeetingActor();
} else if (jobType == 'PresentationWithAnnotationExportJob') {
const baseDirectory = exportJob.presLocation;
const subDirectory = 'pdfs';
const filePath = path.join(baseDirectory, subDirectory,
jobId, serverSideFilename);
await upload(filePath);
} else if (jobType == 'PadCaptureJob') {
const filePath = `${dropbox}/${serverSideFilename}`;
await upload(filePath);
} else {
logger.error(`Notifier received unknown job type ${jobType}`);
}
// Delete temporary files after notification/upload completes.
fs.rm(dropbox, {recursive: true}, (err) => {
if (err) {
throw err;
}
});
}
main();