little-hesinde/rusty-library/src/handlers/opds.rs

117 lines
3.1 KiB
Rust
Raw Normal View History

2024-05-08 16:11:39 +00:00
use std::sync::Arc;
2024-05-09 09:18:47 +00:00
use calibre_db::data::pagination::SortOrder;
2024-05-08 16:11:39 +00:00
use poem::{
handler,
2024-05-09 06:39:46 +00:00
web::{Data, WithContentType},
2024-05-08 16:11:39 +00:00
IntoResponse,
};
2024-05-09 06:39:46 +00:00
use time::OffsetDateTime;
2024-05-08 16:11:39 +00:00
use crate::{
app_state::AppState,
2024-05-09 09:18:47 +00:00
data::book::Book,
2024-05-09 06:39:46 +00:00
handlers::error::HandlerError,
2024-05-08 16:11:39 +00:00
opds::{
author::Author, content::Content, entry::Entry, feed::Feed, link::Link,
media_type::MediaType, relation::Relation,
},
};
2024-05-09 09:18:47 +00:00
fn create_feed(
now: OffsetDateTime,
self_link: Link,
mut additional_links: Vec<Link>,
entries: Vec<Entry>,
) -> Feed {
2024-05-08 16:11:39 +00:00
let author = Author {
name: "Thallian".to_string(),
uri: "https://code.vanwa.ch/shu/rusty-library".to_string(),
email: None,
};
2024-05-09 09:18:47 +00:00
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(),
2024-05-08 16:11:39 +00:00
media_type: MediaType::Navigation,
2024-05-09 09:18:47 +00:00
rel: Relation::Myself,
title: None,
2024-05-08 16:11:39 +00:00
count: None,
};
2024-05-09 09:18:47 +00:00
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();
2024-05-08 16:11:39 +00:00
let self_link = Link {
href: "/opds".to_string(),
media_type: MediaType::Navigation,
rel: Relation::Myself,
title: None,
count: None,
};
let books_entry = Entry {
title: "Books".to_string(),
id: "rusty:books".to_string(),
2024-05-09 06:39:46 +00:00
updated: now,
2024-05-08 16:11:39 +00:00
content: Content {
media_type: MediaType::Text,
content: "Index of all books".to_string(),
},
2024-05-09 09:18:47 +00:00
author: None,
2024-05-08 16:11:39 +00:00
links: vec![Link {
href: "/opds/books".to_string(),
media_type: MediaType::Navigation,
rel: Relation::Subsection,
title: None,
count: None,
}],
};
2024-05-09 09:18:47 +00:00
let feed = create_feed(now, self_link, vec![], vec![books_entry]);
2024-05-09 06:39:46 +00:00
let xml = feed.as_xml().map_err(HandlerError::OpdsError)?;
2024-05-08 16:11:39 +00:00
Ok(xml.with_content_type("application/atom+xml"))
}