From ab25e0bb9a2cfce30df430bbcd8768242955120c Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sun, 27 Mar 2022 22:38:56 +0100 Subject: [PATCH] Retrieve icons from the icon store --- quickpeep/src/bin/quickpeep.rs | 2 ++ quickpeep/src/web.rs | 1 + quickpeep/src/web/icon_retrieval.rs | 41 ++++++++++++++++++++++++++ quickpeep/src/web/searcher.rs | 2 +- quickpeep/templates/search.html.askama | 2 +- 5 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 quickpeep/src/web/icon_retrieval.rs diff --git a/quickpeep/src/bin/quickpeep.rs b/quickpeep/src/bin/quickpeep.rs index 617e9d9..98f3e25 100644 --- a/quickpeep/src/bin/quickpeep.rs +++ b/quickpeep/src/bin/quickpeep.rs @@ -5,6 +5,7 @@ use axum::routing::{get, get_service, post}; use axum::Router; use env_logger::Env; use quickpeep::config::WebConfig; +use quickpeep::web::icon_retrieval::retrieve_icon; use quickpeep::web::searcher::{search_root, search_search}; use quickpeep::web::seed_collector::{seed_collection_root, seed_collection_root_post}; use quickpeep::web::IndexAccess; @@ -68,6 +69,7 @@ async fn main() -> anyhow::Result<()> { .route("/seeds/", post(seed_collection_root_post)) .route("/", get(search_root)) .route("/search", get(search_search)) + .route("/icon.webp", get(retrieve_icon)) .layer(Extension(web_config)) .layer(Extension(pool)) .layer(Extension(index_access)) diff --git a/quickpeep/src/web.rs b/quickpeep/src/web.rs index 778ca7d..fc0e2c5 100644 --- a/quickpeep/src/web.rs +++ b/quickpeep/src/web.rs @@ -1,6 +1,7 @@ use quickpeep_index::backend::Backend; use std::sync::Arc; +pub mod icon_retrieval; pub mod searcher; pub mod seed_collector; diff --git a/quickpeep/src/web/icon_retrieval.rs b/quickpeep/src/web/icon_retrieval.rs new file mode 100644 index 0000000..b68d5a7 --- /dev/null +++ b/quickpeep/src/web/icon_retrieval.rs @@ -0,0 +1,41 @@ +use crate::webutil::internal_error; +use axum::body::{boxed, Bytes}; +use axum::extract::{Extension, Query}; +use axum::http::StatusCode; +use axum::response::{IntoResponse, Response}; +use quickpeep_index::auxiliary::icon_store::IconStore; +use serde::Deserialize; +use std::borrow::Borrow; +use std::sync::Arc; + +#[derive(Deserialize, Debug, Clone)] +pub struct IconRetrievalParameters { + /// The search query + b: String, +} + +pub async fn retrieve_icon( + icon_store: Extension>, + params: Query, +) -> impl IntoResponse { + retrieve_icon_inner(icon_store, params) + .await + .map_err(internal_error) +} + +pub async fn retrieve_icon_inner( + Extension(icon_store): Extension>, + Query(params): Query, +) -> anyhow::Result { + let favicon_url_hash = u64::from_str_radix(¶ms.b, 16)?; + let favicon_url_hash: [u8; 8] = favicon_url_hash.to_le_bytes(); + let txn = icon_store.env.ro_txn()?; + match icon_store.icons.get(&txn, &favicon_url_hash)? { + None => Ok((StatusCode::NOT_FOUND, "Not found").into_response()), + Some(icon_bytes) => Ok(Response::builder() + .header("content-type", "image/webp") + .body(boxed( + Bytes::copy_from_slice(icon_bytes.borrow()).into_response(), + ))?), + } +} diff --git a/quickpeep/src/web/searcher.rs b/quickpeep/src/web/searcher.rs index 3790c2d..1dca677 100644 --- a/quickpeep/src/web/searcher.rs +++ b/quickpeep/src/web/searcher.rs @@ -67,7 +67,7 @@ pub async fn search_search_inner( for search_doc in raw_results { results.push(SearchResult { - favicon_url: "".to_string(), + favicon_url: format!("{:16x}", search_doc.favicon_url_hash), url: search_doc.url, title: search_doc.title, excerpt: search_doc.excerpt, diff --git a/quickpeep/templates/search.html.askama b/quickpeep/templates/search.html.askama index d060ba8..4324c52 100644 --- a/quickpeep/templates/search.html.askama +++ b/quickpeep/templates/search.html.askama @@ -26,7 +26,7 @@