Refactor errors
Some checks failed
CI / Typos (push) Successful in 28s
CI / Clippy (push) Failing after 1m21s
CI / Build and test (all features) (push) Successful in 4m18s
CI / Build and test (push) Successful in 6m10s

This commit is contained in:
2026-03-12 23:04:59 -07:00
parent 15e56d895c
commit 95a547045d
17 changed files with 192 additions and 161 deletions

View File

@@ -1,11 +1,12 @@
use mime::Mime;
use pile_config::Label;
use pile_flac::{FlacBlock, FlacReader};
use pile_flac::{FlacBlock, FlacDecodeError, FlacReader};
use std::{
collections::HashMap,
io::BufReader,
sync::{Arc, OnceLock},
};
use tracing::trace;
use crate::{
extract::traits::{ListExtractor, ObjectExtractor},
@@ -31,16 +32,17 @@ impl FlacImagesExtractor {
let reader = FlacReader::new(BufReader::new(reader));
let mut count = 0usize;
for block in reader {
match block.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))? {
FlacBlock::AudioFrame(_) => break,
FlacBlock::Picture(_) => count += 1,
match block {
Ok(FlacBlock::AudioFrame(_)) => break,
Ok(FlacBlock::Picture(_)) => count += 1,
Err(FlacDecodeError::IoError(err)) => return Err(err),
Err(_) => return Ok(0),
_ => {}
}
}
Ok::<_, std::io::Error>(count)
})
.await
.map_err(std::io::Error::other)??;
.await??;
return Ok(count);
}
@@ -49,14 +51,20 @@ impl FlacImagesExtractor {
#[async_trait::async_trait]
impl ListExtractor for FlacImagesExtractor {
async fn get<'a>(&'a self, mut idx: usize) -> Result<Option<PileValue>, std::io::Error> {
trace!(
key = self.item.key().as_str(),
"Getting index {idx} from FlacImagesExtractor",
);
let key = self.item.key();
let reader = SyncReadBridge::new_current(self.item.read().await?);
let image = tokio::task::spawn_blocking(move || {
let reader = FlacReader::new(BufReader::new(reader));
let mut out: Option<(Mime, Vec<u8>)> = None;
'blocks: for block in reader {
match block.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))? {
FlacBlock::AudioFrame(_) => break,
FlacBlock::Picture(picture) => {
match block {
Ok(FlacBlock::AudioFrame(_)) => break,
Ok(FlacBlock::Picture(picture)) => {
if idx > 0 {
idx -= 1;
continue;
@@ -66,6 +74,16 @@ impl ListExtractor for FlacImagesExtractor {
break 'blocks;
}
Err(FlacDecodeError::IoError(err)) => return Err(err),
Err(error) => {
trace!(
message = "Could not parse FLAC images",
key = key.as_str(),
?error
);
return Ok(None);
}
_ => {}
}
}
@@ -93,23 +111,15 @@ impl ListExtractor for FlacImagesExtractor {
pub struct FlacExtractor {
item: Item,
output: OnceLock<HashMap<Label, PileValue>>,
images: Option<PileValue>,
images: PileValue,
}
impl FlacExtractor {
pub fn new(item: &Item) -> Self {
let is_flac = match item {
Item::File { path, .. } => path.to_str().unwrap_or_default().ends_with(".flac"),
Item::S3 { key, .. } => key.ends_with(".flac"),
};
let images =
is_flac.then(|| PileValue::ListExtractor(Arc::new(FlacImagesExtractor::new(item))));
Self {
item: item.clone(),
output: OnceLock::new(),
images,
images: PileValue::ListExtractor(Arc::new(FlacImagesExtractor::new(item))),
}
}
@@ -118,54 +128,55 @@ impl FlacExtractor {
return Ok(x);
}
let key = match &self.item {
Item::File { path, .. } => path.to_str().unwrap_or_default().to_owned(),
Item::S3 { key, .. } => key.to_string(),
};
if !key.ends_with(".flac") {
let _ = self.output.set(HashMap::new());
#[expect(clippy::unwrap_used)]
return Ok(self.output.get().unwrap());
}
trace!(
message = "Reading FLAC tags",
key = self.item.key().as_str()
);
let key = self.item.key();
let reader = SyncReadBridge::new_current(self.item.read().await?);
let raw_tags = tokio::task::spawn_blocking(move || {
let output = tokio::task::spawn_blocking(move || {
let reader = FlacReader::new(BufReader::new(reader));
let mut tags: Vec<(String, String)> = Vec::new();
let mut output: HashMap<Label, Vec<PileValue>> = HashMap::new();
for block in reader {
match block.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))? {
FlacBlock::VorbisComment(comment) => {
match block {
Ok(FlacBlock::AudioFrame(_)) => break,
Ok(FlacBlock::VorbisComment(comment)) => {
for (k, v) in comment.comment.comments {
tags.push((k.to_string().to_lowercase(), v.into()));
if let Some(label) = Label::new(k.to_string().to_lowercase()) {
output
.entry(label)
.or_default()
.push(PileValue::String(Arc::new(v)));
}
}
}
FlacBlock::AudioFrame(_) => break,
Err(FlacDecodeError::IoError(err)) => return Err(err),
Err(error) => {
trace!(
message = "Could not parse FLAC metadata",
key = key.as_str(),
?error
);
return Ok(HashMap::new());
}
_ => {}
}
}
Ok::<_, std::io::Error>(tags)
let output: HashMap<Label, PileValue> = output
.into_iter()
.map(|(k, v)| (k, PileValue::Array(Arc::new(v))))
.collect();
Ok::<HashMap<Label, PileValue>, std::io::Error>(output)
})
.await
.map_err(std::io::Error::other)??;
.await??;
let mut output: HashMap<Label, Vec<PileValue>> = HashMap::new();
for (k, v) in raw_tags {
if let Some(label) = Label::new(k) {
output
.entry(label)
.or_default()
.push(PileValue::String(Arc::new(v.into())));
}
}
let output: HashMap<Label, PileValue> = output
.into_iter()
.map(|(k, v)| (k, PileValue::Array(Arc::new(v))))
.collect();
let _ = self.output.set(output);
#[expect(clippy::unwrap_used)]
return Ok(self.output.get().unwrap());
return Ok(self.output.get_or_init(|| output));
}
}
@@ -180,20 +191,21 @@ impl ObjectExtractor for FlacExtractor {
return Ok(None);
}
if name.as_str() == "images"
&& let Some(ref images) = self.images
{
return Ok(Some(images.clone()));
if name.as_str() == "images" {
return Ok(Some(self.images.clone()));
}
Ok(self.get_inner().await?.get(name).cloned())
}
#[expect(clippy::unwrap_used)]
async fn fields(&self) -> Result<Vec<Label>, std::io::Error> {
let mut fields = self.get_inner().await?.keys().cloned().collect::<Vec<_>>();
if self.images.is_some() {
#[expect(clippy::unwrap_used)]
fields.push(Label::new("images").unwrap());
}
Ok(fields)
Ok(self
.get_inner()
.await?
.keys()
.cloned()
.chain([Label::new("images").unwrap()])
.collect::<Vec<_>>())
}
}