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 browser_enabled = !args.no_desktop && webui_enabled && !args.no_browser;
let preview_path = upend.path.join("previews"); let preview_path = upend.path.join("previews");
#[cfg(feature = "previews")] let preview_store = Arc::new(previews::PreviewStore::new(
let preview_store = Some(Arc::new(crate::previews::PreviewStore::new(
preview_path.clone(), preview_path.clone(),
store.clone(), store.clone(),
))); ));
if args.clean { if args.clean {
info!("Cleaning temporary directories..."); 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 mut bind: SocketAddr = args.bind.parse().expect("Incorrect bind format.");
let secret = args.secret.unwrap_or_else(|| { let secret = args.secret.unwrap_or_else(|| {

View file

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

View file

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