refactor(backend): don't use Option to compile out preview support

This commit is contained in:
Tomáš Mládek 2024-07-29 13:00:41 +02:00
parent d29df7ef9a
commit 22f9b6b447
3 changed files with 24 additions and 21 deletions

View file

@ -427,11 +427,10 @@ async fn main() -> Result<()> {
let browser_enabled = !args.no_desktop && webui_enabled && !args.no_browser;
let preview_path = upend.path.join("previews");
#[cfg(feature = "previews")]
let preview_store = Some(Arc::new(crate::previews::PreviewStore::new(
let preview_store = Arc::new(previews::PreviewStore::new(
preview_path.clone(),
store.clone(),
)));
));
if args.clean {
info!("Cleaning temporary directories...");
@ -443,9 +442,6 @@ async fn main() -> Result<()> {
}
}
#[cfg(not(feature = "previews"))]
let preview_store = None;
let mut bind: SocketAddr = args.bind.parse().expect("Incorrect bind format.");
let secret = args.secret.unwrap_or_else(|| {

View file

@ -37,7 +37,6 @@ pub struct PreviewStore {
max_limit: Semaphore,
}
#[cfg(feature = "previews")]
impl PreviewStore {
pub fn new<P: AsRef<Path>>(path: P, store: Arc<Box<dyn UpStore + Send + Sync>>) -> Self {
PreviewStore {
@ -47,7 +46,10 @@ impl PreviewStore {
max_limit: Semaphore::new(num_cpus::get()),
}
}
}
#[cfg(feature = "previews")]
impl PreviewStore {
async fn get_path(
&self,
hash: &UpMultihash,

View file

@ -56,7 +56,7 @@ pub struct State {
pub store: Arc<Box<dyn UpStore + Sync + Send>>,
pub config: UpEndConfig,
pub job_container: jobs::JobContainer,
pub preview_store: Option<Arc<PreviewStore>>,
pub preview_store: Arc<PreviewStore>,
pub public: Arc<Mutex<bool>>,
}
@ -345,14 +345,14 @@ pub async fn get_thumbnail(
check_auth(&req, &state)?;
#[cfg(feature = "previews")]
if let Some(preview_store) = &state.preview_store {
{
let hash = hash.into_inner();
let address = Address::decode(&b58_decode(&hash).map_err(ErrorInternalServerError)?)
.map_err(ErrorInternalServerError)?;
if let Address::Hash(address_hash) = address {
let preview_store = preview_store.clone();
return if let Address::Hash(address_hash) = address {
let _job_container = state.job_container.clone();
let preview_result = preview_store
let preview_result = state
.preview_store
.get(address_hash, query, _job_container)
.await
.map_err(|e| {
@ -367,21 +367,25 @@ pub async fn get_thumbnail(
file = file.set_content_type(mime);
}
}
return Ok(Either::Left(file));
Ok(Either::Left(file))
} else {
return Ok(Either::Right(
Ok(Either::Right(
HttpResponse::SeeOther()
.append_header((http::header::LOCATION, format!("../../api/raw/{hash}")))
.finish(),
));
))
}
} else {
return Err(ErrorBadRequest(
Err(ErrorBadRequest(
"Address does not refer to a previewable object.",
));
}
))
};
}
#[cfg(not(feature = "previews"))]
{
Err(error::ErrorNotImplemented("Previews not enabled."))
}
Err(error::ErrorNotImplemented("Previews not enabled."))
}
#[post("/api/query")]
@ -1217,6 +1221,7 @@ mod tests {
use std::fs::File;
use super::*;
use anyhow::Result;
use tempfile::TempDir;
use upend_base::hash::UpMultihash;
@ -1402,7 +1407,7 @@ mod tests {
State {
upend,
store,
store: store.clone(),
config: UpEndConfig {
vault_name: Some("TEST VAULT".to_string()),
desktop_enabled: false,
@ -1410,7 +1415,7 @@ mod tests {
secret: "secret".to_string(),
},
job_container,
preview_store: None,
preview_store: Arc::new(PreviewStore::new("", store)),
public: Arc::new(Mutex::new(true)),
}
}