pinakes-core: fix isbn regex, csv quoting, document extraction, and enrichment accuracy
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I974959e74d2b5b5591437daa0f29291a6a6a6964
This commit is contained in:
parent
d77e5b9f2f
commit
d5be5026a7
5 changed files with 132 additions and 90 deletions
|
|
@ -2,6 +2,16 @@ use std::path::Path;
|
|||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Quote a string field for CSV output: wrap in double-quotes and escape
|
||||
/// internal double-quotes by doubling them (RFC 4180).
|
||||
fn csv_quote(s: &str) -> String {
|
||||
if s.contains([',', '"', '\n', '\r']) {
|
||||
format!("\"{}\"", s.replace('"', "\"\""))
|
||||
} else {
|
||||
s.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
use crate::{error::Result, jobs::ExportFormat, storage::DynStorageBackend};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
|
@ -11,6 +21,11 @@ pub struct ExportResult {
|
|||
}
|
||||
|
||||
/// Export library data to the specified format.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the storage query fails or if the output file cannot
|
||||
/// be written.
|
||||
pub async fn export_library(
|
||||
storage: &DynStorageBackend,
|
||||
format: &ExportFormat,
|
||||
|
|
@ -38,27 +53,30 @@ pub async fn export_library(
|
|||
album,genre,year,duration_secs,description,created_at,updated_at\n",
|
||||
);
|
||||
for item in &items {
|
||||
csv.push_str(&format!(
|
||||
"{},{},{},{:?},{},{},{},{},{},{},{},{},{},{},{}\n",
|
||||
use std::fmt::Write as _;
|
||||
writeln!(
|
||||
csv,
|
||||
"{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}",
|
||||
item.id,
|
||||
item.path.display(),
|
||||
item.file_name,
|
||||
item.media_type,
|
||||
csv_quote(&item.path.display().to_string()),
|
||||
csv_quote(&item.file_name),
|
||||
csv_quote(&format!("{:?}", item.media_type)),
|
||||
item.content_hash,
|
||||
item.file_size,
|
||||
item.title.as_deref().unwrap_or(""),
|
||||
item.artist.as_deref().unwrap_or(""),
|
||||
item.album.as_deref().unwrap_or(""),
|
||||
item.genre.as_deref().unwrap_or(""),
|
||||
csv_quote(item.title.as_deref().unwrap_or("")),
|
||||
csv_quote(item.artist.as_deref().unwrap_or("")),
|
||||
csv_quote(item.album.as_deref().unwrap_or("")),
|
||||
csv_quote(item.genre.as_deref().unwrap_or("")),
|
||||
item.year.map(|y| y.to_string()).unwrap_or_default(),
|
||||
item
|
||||
.duration_secs
|
||||
.map(|d| d.to_string())
|
||||
.unwrap_or_default(),
|
||||
item.description.as_deref().unwrap_or(""),
|
||||
csv_quote(item.description.as_deref().unwrap_or("")),
|
||||
item.created_at,
|
||||
item.updated_at,
|
||||
));
|
||||
)
|
||||
.unwrap_or_default();
|
||||
}
|
||||
std::fs::write(destination, csv)?;
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue