Retrieve icons from the icon store

This commit is contained in:
Olivier 'reivilibre' 2022-03-27 22:38:56 +01:00
parent 25db9fdb24
commit ab25e0bb9a
5 changed files with 46 additions and 2 deletions

View File

@ -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))

View File

@ -1,6 +1,7 @@
use quickpeep_index::backend::Backend;
use std::sync::Arc;
pub mod icon_retrieval;
pub mod searcher;
pub mod seed_collector;

View File

@ -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<Arc<IconStore>>,
params: Query<IconRetrievalParameters>,
) -> impl IntoResponse {
retrieve_icon_inner(icon_store, params)
.await
.map_err(internal_error)
}
pub async fn retrieve_icon_inner(
Extension(icon_store): Extension<Arc<IconStore>>,
Query(params): Query<IconRetrievalParameters>,
) -> anyhow::Result<Response> {
let favicon_url_hash = u64::from_str_radix(&params.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(),
))?),
}
}

View File

@ -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,

View File

@ -26,7 +26,7 @@
<ul class="search_results">
{%- for result in results %}
<li>
<img src="{{ result.favicon_url }}">
<img src="/icon.webp?b={{ result.favicon_url }}">
<div class="result_title"><a href="{{ result.url }}" rel="nofollow noreferrer">{{ result.title }}</a></div>
<div class="result_excerpt">
{{- result.excerpt|safe -}}