book feed

This commit is contained in:
Sebastian Hugentobler 2024-05-09 11:18:47 +02:00
parent 603c2fbe48
commit 12341d01a6
Signed by: shu
GPG key ID: BB32CF3CA052C2F0
15 changed files with 219 additions and 41 deletions

View file

@ -15,7 +15,7 @@ serde_json = "1.0.116"
serde_with = "3.8.1"
tera = "1.19.1"
thiserror = { workspace = true }
time = { version = "0.3.36", features = ["macros", "serde", "formatting"] }
time = { workspace = true }
tokio = { version = "1.37.0", features = ["rt-multi-thread", "macros"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

View file

@ -1,21 +1,39 @@
use std::{collections::HashMap, path::Path};
use std::{collections::HashMap, fmt::Display, path::Path};
use calibre_db::data::{
author::Author as DbAuthor, book::Book as DbBook, series::Series as DbSeries,
};
use serde::Serialize;
use time::OffsetDateTime;
use crate::app_state::AppState;
#[derive(Debug, Serialize)]
#[derive(Debug, Clone, Serialize, Eq, PartialEq, Hash)]
pub struct Format(pub String);
pub type Formats = HashMap<Format, String>;
impl Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0.as_ref() {
"pdf" => write!(f, "pdf"),
"epub" => write!(f, "epub"),
_ => write!(f, "unknown"),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct Book {
pub id: u64,
pub title: String,
pub sort: String,
pub path: String,
pub uuid: String,
pub last_modified: OffsetDateTime,
pub description: Option<String>,
pub author: DbAuthor,
pub series: Option<(DbSeries, f64)>,
pub formats: HashMap<String, String>,
pub formats: Formats,
}
impl Book {
@ -23,20 +41,23 @@ impl Book {
db_book: &DbBook,
db_series: Option<(DbSeries, f64)>,
author: DbAuthor,
formats: HashMap<String, String>,
formats: Formats,
) -> Self {
Self {
id: db_book.id,
title: db_book.title.clone(),
sort: db_book.sort.clone(),
path: db_book.path.clone(),
uuid: db_book.uuid.clone(),
description: db_book.description.clone(),
last_modified: db_book.last_modified,
author: author.clone(),
series: db_series.map(|x| (x.0, x.1)),
formats,
}
}
fn formats(book: &DbBook, library_path: &Path) -> HashMap<String, String> {
fn formats(book: &DbBook, library_path: &Path) -> Formats {
let book_path = library_path.join(&book.path);
let mut formats = HashMap::new();
@ -48,7 +69,10 @@ impl Book {
_ => None,
};
if let Some(format) = format {
formats.insert(format, entry.file_name().to_string_lossy().to_string());
formats.insert(
Format(format),
entry.file_name().to_string_lossy().to_string(),
);
}
}
}

View file

@ -7,7 +7,11 @@ use poem::{
IntoResponse,
};
use crate::{app_state::AppState, data::book::Book, handlers::error::HandlerError};
use crate::{
app_state::AppState,
data::book::{Book, Format},
handlers::error::HandlerError,
};
#[handler]
pub async fn handler(
@ -19,18 +23,14 @@ pub async fn handler(
.scalar_book(id)
.map_err(HandlerError::DataError)?;
let book = Book::full_book(&book, &state).ok_or(NotFoundError)?;
let format: &str = format.as_str();
let file_name = book.formats.get(format).ok_or(NotFoundError)?;
let format = Format(format);
let file_name = book.formats.get(&format).ok_or(NotFoundError)?;
let file_path = state.config.library_path.join(book.path).join(file_name);
let mut file = File::open(file_path).map_err(|_| NotFoundError)?;
let mut data = Vec::new();
file.read_to_end(&mut data).map_err(|_| NotFoundError)?;
let content_type = match format {
"pdf" => "application/pdf",
"epub" => "application/epub+zip",
_ => unreachable!(),
};
let content_type = format.0;
Ok(data
.with_content_type(content_type)

View file

@ -1,5 +1,6 @@
use std::sync::Arc;
use calibre_db::data::pagination::SortOrder;
use poem::{
handler,
web::{Data, WithContentType},
@ -9,6 +10,7 @@ use time::OffsetDateTime;
use crate::{
app_state::AppState,
data::book::Book,
handlers::error::HandlerError,
opds::{
author::Author, content::Content, entry::Entry, feed::Feed, link::Link,
@ -16,22 +18,72 @@ use crate::{
},
};
#[handler]
pub async fn handler(state: Data<&Arc<AppState>>) -> Result<WithContentType<String>, poem::Error> {
let now = OffsetDateTime::now_utc();
fn create_feed(
now: OffsetDateTime,
self_link: Link,
mut additional_links: Vec<Link>,
entries: Vec<Entry>,
) -> Feed {
let author = Author {
name: "Thallian".to_string(),
uri: "https://code.vanwa.ch/shu/rusty-library".to_string(),
email: None,
};
let home_link = Link {
href: "/opds".to_string(),
let mut links = vec![
Link {
href: "/opds".to_string(),
media_type: MediaType::Navigation,
rel: Relation::Start,
title: Some("Home".to_string()),
count: None,
},
self_link,
];
links.append(&mut additional_links);
Feed {
title: "rusty-library".to_string(),
id: "rusty:catalog".to_string(),
updated: now,
icon: "favicon.ico".to_string(),
author,
links,
entries,
}
}
#[handler]
pub async fn books(state: Data<&Arc<AppState>>) -> Result<WithContentType<String>, poem::Error> {
let books: Vec<Book> = state
.calibre
.books(u32::MAX.into(), None, &SortOrder::ASC)
.map(|x| {
x.iter()
.filter_map(|y| Book::full_book(y, &state))
.collect()
})
.map_err(HandlerError::DataError)?;
let entries: Vec<Entry> = books.into_iter().map(Entry::from).collect();
let now = OffsetDateTime::now_utc();
let self_link = Link {
href: "/opds/books".to_string(),
media_type: MediaType::Navigation,
rel: Relation::Start,
title: Some("Home".to_string()),
rel: Relation::Myself,
title: None,
count: None,
};
let feed = create_feed(now, self_link, vec![], entries);
let xml = feed.as_xml().map_err(HandlerError::OpdsError)?;
Ok(xml.with_content_type("application/atom+xml"))
}
#[handler]
pub async fn handler(state: Data<&Arc<AppState>>) -> Result<WithContentType<String>, poem::Error> {
let now = OffsetDateTime::now_utc();
let self_link = Link {
href: "/opds".to_string(),
media_type: MediaType::Navigation,
@ -47,6 +99,7 @@ pub async fn handler(state: Data<&Arc<AppState>>) -> Result<WithContentType<Stri
media_type: MediaType::Text,
content: "Index of all books".to_string(),
},
author: None,
links: vec![Link {
href: "/opds/books".to_string(),
media_type: MediaType::Navigation,
@ -56,15 +109,7 @@ pub async fn handler(state: Data<&Arc<AppState>>) -> Result<WithContentType<Stri
}],
};
let feed = Feed {
title: "rusty-library".to_string(),
id: "rusty:catalog".to_string(),
updated: now,
icon: "favicon.ico".to_string(),
author,
links: vec![home_link, self_link],
entries: vec![books_entry],
};
let feed = create_feed(now, self_link, vec![], vec![books_entry]);
let xml = feed.as_xml().map_err(HandlerError::OpdsError)?;
Ok(xml.with_content_type("application/atom+xml"))

View file

@ -43,6 +43,7 @@ pub async fn run(config: Config) -> Result<(), std::io::Error> {
let app = Route::new()
.at("/", get(handlers::recents::handler))
.at("/opds", get(handlers::opds::handler))
.at("/opds/books", get(handlers::opds::books))
.at("/books", get(handlers::books::handler_init))
.at("/books/:cursor/:sort_order", get(handlers::books::handler))
.at("/series", get(handlers::series::handler_init))

View file

@ -1,7 +1,11 @@
use serde::Serialize;
use time::OffsetDateTime;
use super::{content::Content, link::Link};
use crate::data::book::Book;
use super::{
author::Author, content::Content, link::Link, media_type::MediaType, relation::Relation,
};
#[derive(Debug, Serialize)]
#[serde(rename = "entry")]
@ -11,10 +15,46 @@ pub struct Entry {
#[serde(with = "time::serde::rfc3339")]
pub updated: OffsetDateTime,
pub content: Content,
pub author: Option<Author>,
#[serde(rename = "link")]
pub links: Vec<Link>,
}
impl From<Book> for Entry {
fn from(value: Book) -> Self {
let author = Author {
name: value.clone().author.name,
uri: format!("/opds/authors/{}", value.author.id),
email: None,
};
let mut links = vec![Link {
href: format!("/cover/{}", value.id),
media_type: MediaType::Jpeg,
rel: Relation::Image,
title: None,
count: None,
}];
let mut format_links: Vec<Link> = value
.formats
.iter()
.map(|(key, val)| Link::from((&value, (key, val.as_str()))))
.collect();
links.append(&mut format_links);
Self {
title: value.title.clone(),
id: format!("urn:uuid:{}", value.uuid),
updated: value.last_modified,
content: Content {
media_type: MediaType::Html,
content: value.description.clone().unwrap_or("".to_string()),
},
author: Some(author),
links,
}
}
}
#[cfg(test)]
mod tests {
use quick_xml::se::to_string;
@ -33,6 +73,7 @@ mod tests {
media_type: MediaType::Text,
content: "All authors".to_string(),
},
author: None,
links: vec![
Link {
href: "/opds".to_string(),

View file

@ -1,5 +1,7 @@
use serde::Serialize;
use crate::data::book::{Book, Format};
use super::{media_type::MediaType, relation::Relation};
#[derive(Debug, Serialize)]
@ -19,6 +21,20 @@ pub struct Link {
pub count: Option<u64>,
}
impl From<(&Book, (&Format, &str))> for Link {
fn from(value: (&Book, (&Format, &str))) -> Self {
let format = value.1 .0.clone();
let media_type: MediaType = format.into();
Self {
href: format!("/book/{}/{}", value.0.id, value.1 .0),
media_type,
rel: media_type.into(),
title: Some(value.1 .0 .0.clone()),
count: None,
}
}
}
#[cfg(test)]
mod tests {
use quick_xml::se::to_string;

View file

@ -1,6 +1,8 @@
use serde_with::SerializeDisplay;
#[derive(Debug, SerializeDisplay)]
use crate::data::book::Format;
#[derive(Debug, Copy, Clone, SerializeDisplay)]
pub enum MediaType {
Acquisition,
Epub,
@ -11,6 +13,16 @@ pub enum MediaType {
Text,
}
impl From<Format> for MediaType {
fn from(value: Format) -> Self {
match value.0.as_ref() {
"epub" => MediaType::Epub,
"pdf" => MediaType::Pdf,
_ => MediaType::Text,
}
}
}
impl std::fmt::Display for MediaType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {

View file

@ -1,5 +1,7 @@
use serde_with::SerializeDisplay;
use super::media_type::MediaType;
#[derive(Debug, SerializeDisplay)]
pub enum Relation {
Image,
@ -7,6 +9,21 @@ pub enum Relation {
Start,
Subsection,
Thumbnail,
Acquisition,
}
impl From<MediaType> for Relation {
fn from(value: MediaType) -> Self {
match value {
MediaType::Acquisition => Relation::Acquisition,
MediaType::Epub => Relation::Acquisition,
MediaType::Html => Relation::Acquisition,
MediaType::Jpeg => Relation::Image,
MediaType::Navigation => Relation::Myself,
MediaType::Pdf => Relation::Acquisition,
MediaType::Text => Relation::Acquisition,
}
}
}
impl std::fmt::Display for Relation {
@ -17,6 +34,7 @@ impl std::fmt::Display for Relation {
Relation::Start => write!(f, "start"),
Relation::Subsection => write!(f, "subsection"),
Relation::Thumbnail => write!(f, "http://opds-spec.org/image/thumbnail"),
Relation::Acquisition => write!(f, "http://opds-spec.org/acquisition"),
}
}
}