ok fuck all of that just compose the path when told to screenshot

This commit is contained in:
Lily Tsuru 2024-09-30 07:31:05 -04:00
parent 910ab614cd
commit 7e1f5eddb6
2 changed files with 31 additions and 38 deletions

View file

@ -65,34 +65,43 @@ async fn main() -> anyhow::Result<()> {
for (id, node) in config.vms.iter() { for (id, node) in config.vms.iter() {
tracing::info!("Adding node {id} : {:?}", node); tracing::info!("Adding node {id} : {:?}", node);
let mut clone = tx.subscribe(); let mut _rx = tx.subscribe();
let id_clone = id.clone();
let node_clone = node.clone();
let root = config.root_path.clone();
let _: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
while let Some(_) = _rx.recv().await? {
let span = tracing::span!(Level::INFO, "node screenshot", node = id_clone.as_str());
let zone = tzfile::Tz::named("GMT")?; let zone = tzfile::Tz::named("GMT")?;
let now = { chrono::Utc::now().with_timezone(&&zone) }; let now = { chrono::Utc::now().with_timezone(&&zone) };
let date_path = config.root_path.join(format!( let date_path = root.join(format!(
"{:02}-{:02}-{:02}", "{:02}-{:02}-{:02}",
now.year(), now.year(),
now.month(), now.month(),
now.day() now.day()
)); ));
if !date_path.exists() { let node_path = date_path.join(&id_clone);
std::fs::create_dir_all(&date_path)?;
if !node_path.exists() {
std::fs::create_dir_all(&node_path)?;
} }
let id_clone = id.clone(); let file_path = node_path.join(format!(
let node_clone = node.clone(); "{:02}-{:02}-{:02}.webp",
now.hour(),
let _: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move { now.minute(),
while let Some(_) = clone.recv().await? { now.second()
let span = tracing::span!(Level::INFO, "node screenshot", node = id_clone.as_str()); ));
match shotter::take_one_screenshot( match shotter::take_one_screenshot(
&node_clone.url, &node_clone.url,
&node_clone.origin, &node_clone.origin,
&id_clone, &id_clone,
date_path.clone(), file_path.clone(),
config.webp_quality, config.webp_quality,
) )
.instrument(span) .instrument(span)

View file

@ -4,7 +4,6 @@ use base64::{
engine::general_purpose::{GeneralPurpose, GeneralPurposeConfig}, engine::general_purpose::{GeneralPurpose, GeneralPurposeConfig},
Engine, Engine,
}; };
use chrono::Timelike;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tokio_tungstenite::tungstenite::client::IntoClientRequest; use tokio_tungstenite::tungstenite::client::IntoClientRequest;
@ -19,7 +18,7 @@ pub async fn take_one_screenshot(
url: &String, url: &String,
origin: &String, origin: &String,
node_name: &String, node_name: &String,
root_path: std::path::PathBuf, image_path: std::path::PathBuf,
webp_quality: f32, webp_quality: f32,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
// Don't particularly like this syntax but whatever // Don't particularly like this syntax but whatever
@ -84,7 +83,6 @@ pub async fn take_one_screenshot(
.in_current_span(), .in_current_span(),
); );
let node_name_clone = node_name.clone();
// main handler // main handler
let write_guac_handler_clone = write_tx.clone(); let write_guac_handler_clone = write_tx.clone();
@ -113,18 +111,7 @@ pub async fn take_one_screenshot(
// Layer 0 is the screen (it's also the only layer cvmts sends) // Layer 0 is the screen (it's also the only layer cvmts sends)
if msg[1].as_str() == "0" { if msg[1].as_str() == "0" {
if msg[3].as_str() == "0" && msg[4].as_str() == "0" { if msg[3].as_str() == "0" && msg[4].as_str() == "0" {
let zone = tzfile::Tz::named("GMT")?; let path = image_path.clone();
let now = { chrono::Utc::now().with_timezone(&&zone) };
let id_path = root_path.join(&node_name_clone);
let file_path = id_path.join(format!(
"{:02}-{:02}-{:02}.webp",
now.hour(),
now.minute(),
now.second()
));
let file_path_clone = file_path.clone();
let _: anyhow::Result<()> = let _: anyhow::Result<()> =
tokio::task::spawn_blocking(move || { tokio::task::spawn_blocking(move || {
@ -151,12 +138,9 @@ pub async fn take_one_screenshot(
let encoder = webp::Encoder::from_image(&image) let encoder = webp::Encoder::from_image(&image)
.expect("fuck you"); .expect("fuck you");
if !id_path.exists() {
std::fs::create_dir_all(&id_path)?;
}
let mut file = let mut file =
std::fs::File::create(file_path_clone)?; std::fs::File::create(&path)?;
{ {
let encoded = encoder let encoded = encoder
@ -171,7 +155,7 @@ pub async fn take_one_screenshot(
tracing::info!( tracing::info!(
"Screenshot written to \"{}\"", "Screenshot written to \"{}\"",
file_path.display() image_path.display()
); );
// no longer needed, everyone will close now! // no longer needed, everyone will close now!