Skip to content

Commit

Permalink
handle PathNotFoundError instead of calling storage.source_file_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Nov 22, 2023
1 parent 65b5734 commit b859e78
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 52 deletions.
35 changes: 0 additions & 35 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,6 @@ impl AsyncStorage {
})
}

pub(crate) async fn source_file_exists(
&self,
name: &str,
version: &str,
latest_build_id: i32,
path: &str,
archive_storage: bool,
) -> Result<bool> {
Ok(if archive_storage {
self.exists_in_archive(&source_archive_path(name, version), latest_build_id, path)
.await?
} else {
// Add sources prefix, name and version to the path for accessing the file stored in the database
let remote_path = format!("sources/{name}/{version}/{path}");
self.exists(&remote_path).await?
})
}

#[context("fetching {path} from {name} {version} (archive: {archive_storage})")]
pub(crate) async fn fetch_source_file(
&self,
Expand Down Expand Up @@ -665,23 +647,6 @@ impl Storage {
))
}

pub(crate) fn source_file_exists(
&self,
name: &str,
version: &str,
latest_build_id: i32,
path: &str,
archive_storage: bool,
) -> Result<bool> {
self.runtime.block_on(self.inner.source_file_exists(
name,
version,
latest_build_id,
path,
archive_storage,
))
}

pub(crate) fn rustdoc_file_exists(
&self,
name: &str,
Expand Down
32 changes: 15 additions & 17 deletions src/web/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{error::AxumResult, match_version_axum};
use crate::{
db::Pool,
impl_axum_webpage,
storage::PathNotFoundError,
utils::get_correct_docsrs_style_file,
web::{
cache::CachePolicy, error::AxumNope, file::File as DbFile, headers::CanonicalUrl,
Expand Down Expand Up @@ -250,30 +251,27 @@ pub(crate) async fn source_browser_handler(

// try to get actual file first
// skip if request is a directory
let blob = if !path.ends_with('/')
&& storage
.source_file_exists(
let blob = if !path.ends_with('/') {
match storage
.fetch_source_file(
&name,
&version,
row.latest_build_id.unwrap_or(0),
&path,
row.archive_storage,
)
.await
.context("error checking source file existence")?
{
Some(
storage
.fetch_source_file(
&name,
&version,
row.latest_build_id.unwrap_or(0),
&path,
row.archive_storage,
)
.await
.context("error fetching source file")?,
)
.context("error fetching source file")
{
Ok(blob) => Some(blob),
Err(err) => {
if err.downcast_ref::<PathNotFoundError>().is_some() {
None
} else {
return Err(err.into());
}
}
}
} else {
None
};
Expand Down

0 comments on commit b859e78

Please sign in to comment.