From 7f30a513b471b4fa4ac169e7fc4baa282f275d15 Mon Sep 17 00:00:00 2001 From: "Stuart P. Bentley" Date: Wed, 6 Mar 2024 12:10:43 -0800 Subject: [PATCH] Fetch image immediately before presenting/sending Closes #1 --- herald/index.js | 21 ++++++++++++--------- sentinel/index.js | 7 +++++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/herald/index.js b/herald/index.js index 069556e..81713d3 100644 --- a/herald/index.js +++ b/herald/index.js @@ -53,15 +53,15 @@ function killViewerProcess() { return viewerProcess.then(lose, lose); } -function startViewerProcess(location, duration) { +async function startViewerProcess(imageStream, duration) { // defer to the termination of any existing viewer process if (viewerProcess) { - return killViewerProcess().then( - startViewerProcess.bind(null, location, duration)); + await killViewerProcess(); + return startViewerProcess(imageStream, duration); } // set up a new viewer process - viewerProcess = asDesktopUser`feh -F ${location}`; + viewerProcess = asDesktopUser({input: imageStream})`feh -F -`; // log any errors, except the one we're expecting viewerProcess.catch(logNonSigTermErrors); @@ -70,10 +70,13 @@ function startViewerProcess(location, duration) { if (duration) { viewerTimeout = setTimeout(killViewerProcess, duration); } +} - // ensure that we're returning a resolved Promise context, - // whether this was called from killViewerProcess.then or not - return Promise.resolve(); +async function fetchAndDisplay(location, duration) { + const res = await fetch(location); + if (!res.ok) throw new Error( + `HTTP error fetching image: ${response.status} ${response.statusText}`); + return startViewerProcess(res.body, duration); } app.post('/present/still', (req, res, next) => { @@ -81,9 +84,9 @@ app.post('/present/still', (req, res, next) => { // wake the display asDesktopUser`xset dpms force on`, // present the still - startViewerProcess(req.query.location, req.query.duration) + fetchAndDisplay(req.query.location, req.query.duration) - ].map(p=>p.catch(next))).then(()=>res.send()); + ].map(p => p.catch(next))).then(() => res.send()); }); app.listen(80); diff --git a/sentinel/index.js b/sentinel/index.js index 90c268c..f7f9433 100644 --- a/sentinel/index.js +++ b/sentinel/index.js @@ -61,8 +61,11 @@ const mailer = EMAIL_RECIPIENT && nodemailer.createTransport({ }, }); -function sendEmailNotification() { +async function sendEmailNotification() { const cid = uuid(); + const res = await fetch(SNAP_URL); + if (!res.ok) throw new Error( + `HTTP error fetching image: ${response.status} ${response.statusText}`); return mailer.sendMail({ from: {name: EMAIL_SENDER_NAME, address: EMAIL_SENDER_ADDRESS}, to: EMAIL_RECIPIENT, @@ -72,7 +75,7 @@ function sendEmailNotification() { html: ``, attachments: [{ filename: EMAIL_ATTACHMENT_FILENAME, - path: SNAP_URL, + content: res.body, cid }] }); }