code housekeeping
This commit is contained in:
parent
e1b6578b2a
commit
d3c3ff8e4e
10 changed files with 108 additions and 100 deletions
|
@ -6,7 +6,7 @@ use tracing::{info_span, instrument, warn};
|
|||
|
||||
use crate::config;
|
||||
|
||||
use super::{query, Content, Dir, ImageSize, Query, VPath, VPathBuf};
|
||||
use super::{Content, Dir, ImageSize, Query, VPath, VPathBuf, query};
|
||||
|
||||
pub struct ImageSizeCache<T> {
|
||||
inner: T,
|
||||
|
@ -27,18 +27,18 @@ where
|
|||
T: Dir,
|
||||
{
|
||||
fn compute_image_size(&self, path: &VPath) -> anyhow::Result<Option<ImageSize>> {
|
||||
if path.extension().is_some_and(config::is_image_file) {
|
||||
if let Some(content) = query::<Content>(&self.inner, path) {
|
||||
if path.extension() == Some("svg") {
|
||||
return Ok(svg_size(&content.string()?));
|
||||
} else {
|
||||
let _span = info_span!("raster_image_size").entered();
|
||||
let reader = image::ImageReader::new(Cursor::new(content.bytes()))
|
||||
.with_guessed_format()
|
||||
.context("cannot guess image format")?;
|
||||
let (width, height) = reader.into_dimensions()?;
|
||||
return Ok(Some(ImageSize { width, height }));
|
||||
}
|
||||
if path.extension().is_some_and(config::is_image_file)
|
||||
&& let Some(content) = query::<Content>(&self.inner, path)
|
||||
{
|
||||
if path.extension() == Some("svg") {
|
||||
return Ok(svg_size(&content.string()?));
|
||||
} else {
|
||||
let _span = info_span!("raster_image_size").entered();
|
||||
let reader = image::ImageReader::new(Cursor::new(content.bytes()))
|
||||
.with_guessed_format()
|
||||
.context("cannot guess image format")?;
|
||||
let (width, height) = reader.into_dimensions()?;
|
||||
return Ok(Some(ImageSize { width, height }));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,11 +105,11 @@ fn svg_size(svg: &str) -> Option<ImageSize> {
|
|||
let mut height: Option<u32> = None;
|
||||
let mut view_box: Option<[u32; 4]> = None;
|
||||
while let Some(Ok(token)) = tokenizer.next() {
|
||||
if let xmlparser::Token::ElementStart { local, .. } = &token {
|
||||
if local == "svg" {
|
||||
in_svg = true;
|
||||
continue;
|
||||
}
|
||||
if let xmlparser::Token::ElementStart { local, .. } = &token
|
||||
&& local == "svg"
|
||||
{
|
||||
in_svg = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if in_svg {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue