Skip to content

Commit

Permalink
test convet
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Sep 8, 2023
1 parent c760a45 commit fef0324
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
38 changes: 38 additions & 0 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,32 @@ impl Storage {
}
}

pub(crate) async fn fetch_rustdoc_file_async(
&self,
name: &str,
version: &str,
path: &str,
archive_storage: bool,
fetch_time: Option<&mut RenderingTimesRecorder>,
) -> Result<Blob> {
trace!("fetch rustdoc file");
Ok(if archive_storage {
self.get_from_archive(
&rustdoc_archive_path(name, version),
path,
self.max_file_size_for(path),
fetch_time,
)?
} else {
if let Some(fetch_time) = fetch_time {
fetch_time.step("fetch from storage");
}
// Add rustdoc prefix, name and version to the path for accessing the file stored in the database
let remote_path = format!("rustdoc/{name}/{version}/{path}");
self.get(&remote_path, self.max_file_size_for(path))?
})
}

#[instrument(skip(fetch_time))]
pub(crate) fn fetch_rustdoc_file(
&self,
Expand Down Expand Up @@ -258,6 +284,18 @@ impl Storage {
}
}

pub(crate) async fn get_async(&self, path: &str, max_size: usize) -> Result<Blob> {
let mut blob = match &self.backend {
StorageBackend::Database(db) => db.get(path, max_size, None),
StorageBackend::S3(s3) => s3.get(path, max_size, None).await,
}?;
if let Some(alg) = blob.compression {
blob.content = decompress(blob.content.as_slice(), alg, max_size)?;
blob.compression = None;
}
Ok(blob)
}

pub(crate) fn get(&self, path: &str, max_size: usize) -> Result<Blob> {
let mut blob = match &self.backend {
StorageBackend::Database(db) => db.get(path, max_size, None),
Expand Down
19 changes: 11 additions & 8 deletions src/web/build_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
},
Config, Storage,
};
use anyhow::Context as _;
use axum::{
extract::{Extension, Path},
response::IntoResponse,
Expand Down Expand Up @@ -64,14 +65,7 @@ pub(crate) async fn build_details_handler(
)?
.ok_or(AxumNope::BuildNotFound)?;

let output = if let Some(output) = row.get("output") {
output
} else {
let target: String = row.get("default_target");
let path = format!("build-logs/{id}/{target}.txt");
let file = File::from_path(&storage, &path, &config)?;
String::from_utf8(file.0.content)?
};
let output: Option<String> = row.get("output");

Ok((
row,
Expand All @@ -81,6 +75,15 @@ pub(crate) async fn build_details_handler(
})
.await?;

let output = if let Some(output) = output {
output
} else {
let target: String = row.get("default_target");
let path = format!("build-logs/{id}/{target}.txt");
let file = File::from_path(&storage, &path, &config).await?;
String::from_utf8(file.0.content).context("non utf8")?
};

Ok(BuildDetailsPage {
metadata,
build_details: BuildDetails {
Expand Down
4 changes: 2 additions & 2 deletions src/web/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ pub(crate) struct File(pub(crate) Blob);

impl File {
/// Gets file from database
pub(super) fn from_path(storage: &Storage, path: &str, config: &Config) -> Result<File> {
pub(super) async fn from_path(storage: &Storage, path: &str, config: &Config) -> Result<File> {
let max_size = if path.ends_with(".html") {
config.max_file_size_html
} else {
config.max_file_size
};

Ok(File(storage.get(path, max_size)?))
Ok(File(storage.get_async(path, max_size).await?))
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ async fn try_serve_legacy_toolchain_asset(
// since new nightly versions will always put their
// toolchain specific resources into the new folder,
// which is reached via the new handler.
Ok(
spawn_blocking(move || File::from_path(&storage, &path, &config))
.await
.map(IntoResponse::into_response)?,
)
Ok(File::from_path(&storage, &path, &config)
.await
.map(IntoResponse::into_response)?)
}

/// Handler called for `/:crate` and `/:crate/:version` URLs. Automatically redirects to the docs
Expand Down Expand Up @@ -927,7 +925,7 @@ pub(crate) async fn static_asset_handler(
) -> AxumResult<impl IntoResponse> {
let storage_path = format!("{RUSTDOC_STATIC_STORAGE_PREFIX}{path}");

Ok(spawn_blocking(move || File::from_path(&storage, &storage_path, &config)).await?)
Ok(File::from_path(&storage, &storage_path, &config).await?)
}

#[cfg(test)]
Expand Down

0 comments on commit fef0324

Please sign in to comment.