initial commit
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I4a6b498153eccd5407510dd541b7f4816a6a6964
This commit is contained in:
commit
6a73d11c4b
124 changed files with 34856 additions and 0 deletions
68
crates/pinakes-core/src/export.rs
Normal file
68
crates/pinakes-core/src/export.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
use std::path::Path;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::jobs::ExportFormat;
|
||||
use crate::storage::DynStorageBackend;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ExportResult {
|
||||
pub items_exported: usize,
|
||||
pub output_path: String,
|
||||
}
|
||||
|
||||
/// Export library data to the specified format.
|
||||
pub async fn export_library(
|
||||
storage: &DynStorageBackend,
|
||||
format: &ExportFormat,
|
||||
destination: &Path,
|
||||
) -> Result<ExportResult> {
|
||||
let pagination = crate::model::Pagination {
|
||||
offset: 0,
|
||||
limit: u64::MAX,
|
||||
sort: None,
|
||||
};
|
||||
let items = storage.list_media(&&pagination).await?;
|
||||
let count = items.len();
|
||||
|
||||
match format {
|
||||
ExportFormat::Json => {
|
||||
let json = serde_json::to_string_pretty(&items)
|
||||
.map_err(|e| crate::error::PinakesError::Config(format!("json serialize: {e}")))?;
|
||||
std::fs::write(destination, json)?;
|
||||
}
|
||||
ExportFormat::Csv => {
|
||||
let mut csv = String::new();
|
||||
csv.push_str("id,path,file_name,media_type,content_hash,file_size,title,artist,album,genre,year,duration_secs,description,created_at,updated_at\n");
|
||||
for item in &items {
|
||||
csv.push_str(&format!(
|
||||
"{},{},{},{:?},{},{},{},{},{},{},{},{},{},{},{}\n",
|
||||
item.id,
|
||||
item.path.display(),
|
||||
item.file_name,
|
||||
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(""),
|
||||
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(""),
|
||||
item.created_at,
|
||||
item.updated_at,
|
||||
));
|
||||
}
|
||||
std::fs::write(destination, csv)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ExportResult {
|
||||
items_exported: count,
|
||||
output_path: destination.to_string_lossy().to_string(),
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue