From 82c7bfcb64ea9bbf1186bb85482164bb1dca882f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Mon, 28 Feb 2022 22:43:23 +0100 Subject: [PATCH] run (all) extractors for manually added blobs and addresses --- src/extractors/mod.rs | 22 +++++++++++++- src/routes.rs | 67 ++++++++++++++++++++++++++----------------- 2 files changed, 62 insertions(+), 27 deletions(-) diff --git a/src/extractors/mod.rs b/src/extractors/mod.rs index 053b02d..bf0e6b9 100644 --- a/src/extractors/mod.rs +++ b/src/extractors/mod.rs @@ -44,4 +44,24 @@ pub trait Extractor { Ok(0) } } -} \ No newline at end of file +} + +pub fn extract_all( + address: &Address, + connection: &UpEndConnection, + job_container: Arc>, +) -> Result { + let mut entry_count = 0; + + #[cfg(feature = "extractors-web")] + { + entry_count += web::WebExtractor.insert_info(address, connection, job_container.clone())?; + } + + #[cfg(feature = "extractors-audio")] + { + entry_count += audio::ID3Extractor.insert_info(address, connection, job_container)?; + } + + Ok(entry_count) +} diff --git a/src/routes.rs b/src/routes.rs index efdac91..5262671 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -4,7 +4,7 @@ use crate::database::entry::{Entry, EntryValue, InvariantEntry}; use crate::database::hierarchies::{list_roots, resolve_path, UHierPath}; use crate::database::lang::Query; use crate::database::UpEndDatabase; -use crate::extractors::Extractor; +use crate::extractors::{self}; use crate::filesystem::add_file; use crate::previews::PreviewStore; use crate::util::exec::block_background; @@ -18,7 +18,7 @@ use actix_web::{delete, error, get, post, put, web, Either, Error, HttpResponse} use actix_web::{http, Responder}; use anyhow::{anyhow, Result}; use futures_util::TryStreamExt; -use log::{debug, info, trace}; +use log::{debug, info, trace, warn}; use serde::Deserialize; use serde_json::json; use std::convert::{TryFrom, TryInto}; @@ -354,33 +354,34 @@ pub async fn put_object( InEntry::Address { entity: in_address } => { let address = in_address.try_into().map_err(ErrorBadRequest)?; - let entries_to_add = match &address { - Address::Hash(_) | Address::Uuid(_) => vec![], - Address::Attribute(attribute) => vec![Entry { + let label_entry = match &address { + Address::Hash(_) | Address::Uuid(_) => None, + Address::Attribute(attribute) => Some(Entry { entity: address.clone(), attribute: LABEL_ATTR.to_string(), value: format!("ATTRIBUTE: {attribute}").into(), - }], - Address::Url(url) => { - #[cfg(feature = "extractors-web")] - { - let _address = address.clone(); - block_background(move || { - (crate::extractors::web::WebExtractor {}).insert_info( - &_address, - &state.upend.connection()?, - state.job_container.clone(), - ) - }); - } - vec![Entry { - entity: address.clone(), - attribute: LABEL_ATTR.to_string(), - value: url.clone().into(), - }] - } + }), + Address::Url(url) => Some(Entry { + entity: address.clone(), + attribute: LABEL_ATTR.to_string(), + value: url.clone().into(), + }), }; + let _address = address.clone(); + let _job_container = state.job_container.clone(); + block_background::<_, _, anyhow::Error>(move || { + let extract_result = + extractors::extract_all(&_address, &connection, _job_container); + if let Ok(entry_count) = extract_result { + debug!("Added {entry_count} extracted entries for {_address:?}"); + } else { + warn!("Failed to add extracted entries for {_address:?}!"); + } + Ok(()) + }); + + let connection = state.upend.connection().map_err(ErrorInternalServerError)?; Ok(web::block(move || { connection.transaction::<_, anyhow::Error, _>(|| { if connection.retrieve_object(&address)?.is_empty() { @@ -397,8 +398,8 @@ pub async fn put_object( })?; } - for entry in entries_to_add { - connection.insert_entry(entry)?; + if let Some(label_entry) = label_entry { + connection.insert_entry(label_entry)?; } Ok((None, Some(address))) @@ -466,6 +467,20 @@ pub async fn put_object( .await; } + let _address = address.clone(); + let _job_container = state.job_container.clone(); + let connection = state.upend.connection().map_err(ErrorInternalServerError)?; + block_background::<_, _, anyhow::Error>(move || { + let extract_result = + extractors::extract_all(&_address, &connection, _job_container); + if let Ok(entry_count) = extract_result { + debug!("Added {entry_count} extracted entries for {_address:?}"); + } else { + warn!("Failed to add extracted entries for {_address:?}!"); + } + Ok(()) + }); + Ok((None, Some(address))) } else { Err(anyhow!("Multipart contains no fields."))