service/media: add some tracing

This commit is contained in:
Lambda 2024-05-27 19:34:17 +00:00 committed by Charles Hall
parent 0a92c72566
commit b6fc9b0feb
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF

View file

@ -5,6 +5,7 @@ use tokio::{
fs::File, fs::File,
io::{AsyncReadExt, AsyncWriteExt, BufReader}, io::{AsyncReadExt, AsyncWriteExt, BufReader},
}; };
use tracing::{debug, warn};
use crate::{services, Result}; use crate::{services, Result};
@ -120,20 +121,31 @@ impl Service {
/// Generates a thumbnail from the given image file contents. Returns /// Generates a thumbnail from the given image file contents. Returns
/// `Ok(None)` if the input image should be used as-is. /// `Ok(None)` if the input image should be used as-is.
#[tracing::instrument(skip(file), fields(input_size = file.len()))] #[tracing::instrument(
skip(file),
fields(input_size = file.len(), original_width, original_height),
)]
fn generate_thumbnail( fn generate_thumbnail(
file: &[u8], file: &[u8],
width: u32, width: u32,
height: u32, height: u32,
crop: bool, crop: bool,
) -> Result<Option<Vec<u8>>> { ) -> Result<Option<Vec<u8>>> {
let Ok(image) = image::load_from_memory(file) else { let image = match image::load_from_memory(file) {
Ok(image) => image,
Err(error) => {
warn!(%error, "Failed to parse source image");
return Ok(None); return Ok(None);
}
}; };
let original_width = image.width(); let original_width = image.width();
let original_height = image.height(); let original_height = image.height();
tracing::Span::current().record("original_width", original_width);
tracing::Span::current().record("original_height", original_height);
if width > original_width || height > original_height { if width > original_width || height > original_height {
debug!("Requested thumbnail is larger than source image");
return Ok(None); return Ok(None);
} }
@ -179,6 +191,7 @@ impl Service {
image.thumbnail_exact(exact_width, exact_height) image.thumbnail_exact(exact_width, exact_height)
}; };
debug!("Serializing thumbnail as PNG");
let mut thumbnail_bytes = Vec::new(); let mut thumbnail_bytes = Vec::new();
thumbnail.write_to( thumbnail.write_to(
&mut Cursor::new(&mut thumbnail_bytes), &mut Cursor::new(&mut thumbnail_bytes),
@ -216,7 +229,7 @@ impl Service {
if let Ok((content_disposition, content_type, key)) = if let Ok((content_disposition, content_type, key)) =
self.db.search_file_metadata(mxc.clone(), width, height) self.db.search_file_metadata(mxc.clone(), width, height)
{ {
// Using saved thumbnail debug!("Using saved thumbnail");
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
let mut file = Vec::new(); let mut file = Vec::new();
File::open(path).await?.read_to_end(&mut file).await?; File::open(path).await?.read_to_end(&mut file).await?;
@ -228,19 +241,18 @@ impl Service {
})); }));
} }
// thumbnail not found, generate
let Ok((content_disposition, content_type, key)) = let Ok((content_disposition, content_type, key)) =
self.db.search_file_metadata(mxc.clone(), 0, 0) self.db.search_file_metadata(mxc.clone(), 0, 0)
else { else {
debug!("Original image not found, can't generate thumbnail");
return Ok(None); return Ok(None);
}; };
// Generate a thumbnail
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
let mut file = Vec::new(); let mut file = Vec::new();
File::open(path).await?.read_to_end(&mut file).await?; File::open(path).await?.read_to_end(&mut file).await?;
debug!("Generating thumbnail");
let thumbnail_result = { let thumbnail_result = {
let file = file.clone(); let file = file.clone();
let outer_span = tracing::span::Span::current(); let outer_span = tracing::span::Span::current();
@ -255,6 +267,7 @@ impl Service {
}; };
let Some(thumbnail_bytes) = thumbnail_result? else { let Some(thumbnail_bytes) = thumbnail_result? else {
debug!("Returning source image as-is");
return Ok(Some(FileMeta { return Ok(Some(FileMeta {
content_disposition, content_disposition,
content_type, content_type,
@ -262,6 +275,8 @@ impl Service {
})); }));
}; };
debug!("Saving created thumbnail");
// Save thumbnail in database so we don't have to generate it // Save thumbnail in database so we don't have to generate it
// again next time // again next time
let thumbnail_key = self.db.create_file_metadata( let thumbnail_key = self.db.create_file_metadata(