use crate::error::{BrowsingError, Result};
use base64::{Engine as _, engine::general_purpose};
use tracing::info;
pub struct ScreenshotManager;
impl ScreenshotManager {
pub fn new() -> Self {
Self
}
pub async fn take_screenshot(
&self,
page: &crate::actor::Page,
path: Option<&str>,
full_page: bool,
format: Option<&str>,
quality: Option<u32>,
) -> Result<Vec<u8>> {
let data_b64 = page
.screenshot_with_options(format, quality, full_page, None)
.await?;
let screenshot_data = general_purpose::STANDARD
.decode(&data_b64)
.map_err(|e| BrowsingError::Browser(format!("Failed to decode screenshot: {}", e)))?;
if let Some(file_path) = path {
tokio::fs::write(file_path, &screenshot_data)
.await
.map_err(|e| BrowsingError::Browser(format!("Failed to save screenshot: {}", e)))?;
info!("Screenshot saved to: {}", file_path);
}
Ok(screenshot_data)
}
pub async fn take_screenshot_base64(
&self,
page: &crate::actor::Page,
full_page: bool,
) -> Result<String> {
let data_b64 = page
.screenshot_with_options(None, None, full_page, None)
.await?;
Ok(data_b64)
}
}
impl Default for ScreenshotManager {
fn default() -> Self {
Self::new()
}
}