perf: first check for files in /raw/

feat/type-attributes
Tomáš Mládek 2022-09-11 13:03:07 +02:00
parent da5d3ad0c2
commit 160cf59d4a
1 changed files with 15 additions and 18 deletions

View File

@ -129,17 +129,6 @@ pub async fn get_raw(
if let Address::Hash(hash) = address {
let hash = Arc::new(hash);
// First check if there's an entry with this hash
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
let _hash = hash.clone();
let entry = web::block(move || connection.retrieve_entry(_hash.as_ref()))
.await
.map_err(ErrorInternalServerError)?;
if let Some(entry) = entry {
return Ok(Either::B(HttpResponse::Ok().json(entry)));
}
// Then, check the files
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
let _hash = hash.clone();
let files = web::block(move || connection.retrieve_file(_hash.as_ref()))
@ -147,9 +136,8 @@ pub async fn get_raw(
.map_err(ErrorInternalServerError)?;
if let Some(file) = files.get(0) {
let file_path = state.upend.vault_path.join(&file.path);
if query.native.is_none() {
Ok(Either::A(
return Ok(Either::A(
NamedFile::open(file_path)?
.set_content_disposition(ContentDisposition {
disposition: if query.inline.is_some() {
@ -166,7 +154,7 @@ pub async fn get_raw(
CacheDirective::Extension("immutable".into(), None),
]),
),
))
));
} else if state.desktop_enabled {
#[cfg(feature = "desktop")]
{
@ -193,17 +181,26 @@ pub async fn get_raw(
.to_path_buf()
};
opener::open(path).map_err(error::ErrorServiceUnavailable)?;
Ok(Either::B(response.finish()))
return Ok(Either::B(response.finish()));
}
#[cfg(not(feature = "desktop"))]
unreachable!()
} else {
Err(error::ErrorNotImplemented("Desktop features not enabled."))
return Err(error::ErrorNotImplemented("Desktop features not enabled."));
}
} else {
Err(error::ErrorNotFound("NOT FOUND"))
}
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
let _hash = hash.clone();
let entry = web::block(move || connection.retrieve_entry(_hash.as_ref()))
.await
.map_err(ErrorInternalServerError)?;
if let Some(entry) = entry {
return Ok(Either::B(HttpResponse::Ok().json(entry)));
}
Err(error::ErrorNotFound("NOT FOUND"))
} else {
Err(ErrorBadRequest(
"Address does not refer to a rawable object.",