new name
This commit is contained in:
parent
5e9f49311e
commit
352d4e0a7a
22 changed files with 69 additions and 22 deletions
63
rusty-library/src/data/book.rs
Normal file
63
rusty-library/src/data/book.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
use std::{collections::HashMap, path::Path};
|
||||
|
||||
use calibre_db::data::{book::Book as DbBook, series::Series as DbSeries};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::app_state::AppState;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Book {
|
||||
pub id: u64,
|
||||
pub title: String,
|
||||
pub sort: String,
|
||||
pub path: String,
|
||||
pub author: String,
|
||||
pub series: Option<(String, f64)>,
|
||||
pub formats: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl Book {
|
||||
pub fn from_db_book(
|
||||
db_book: &DbBook,
|
||||
db_series: Option<(DbSeries, f64)>,
|
||||
author: &str,
|
||||
formats: HashMap<String, String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
id: db_book.id,
|
||||
title: db_book.title.clone(),
|
||||
sort: db_book.sort.clone(),
|
||||
path: db_book.path.clone(),
|
||||
author: author.to_string(),
|
||||
series: db_series.map(|x| (x.0.name, x.1)),
|
||||
formats,
|
||||
}
|
||||
}
|
||||
|
||||
fn formats(book: &DbBook, library_path: &Path) -> HashMap<String, String> {
|
||||
let book_path = library_path.join(&book.path);
|
||||
let mut formats = HashMap::new();
|
||||
|
||||
for entry in book_path.read_dir().unwrap().flatten() {
|
||||
if let Some(extension) = entry.path().extension() {
|
||||
let format = match extension.to_string_lossy().to_string().as_str() {
|
||||
"pdf" => Some("pdf".to_string()),
|
||||
"epub" => Some("epub".to_string()),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(format) = format {
|
||||
formats.insert(format, entry.file_name().to_string_lossy().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
formats
|
||||
}
|
||||
|
||||
pub fn full_book(book: &DbBook, state: &AppState) -> Option<Book> {
|
||||
let formats = Book::formats(book, &state.config.library_path);
|
||||
let author = state.calibre.book_author(book.id).ok()?;
|
||||
let series = state.calibre.book_series(book.id).ok()?;
|
||||
Some(Book::from_db_book(book, series, &author.name, formats))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue