From fef032485cc57bd8dd9f51245e067349889b0332 Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Fri, 8 Sep 2023 22:00:16 +0200 Subject: [PATCH] test convet --- src/storage/mod.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/web/build_details.rs | 19 +++++++++++-------- src/web/file.rs | 4 ++-- src/web/rustdoc.rs | 10 ++++------ 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/storage/mod.rs b/src/storage/mod.rs index ed8f96f34..8f9cedc33 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -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 { + 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, @@ -258,6 +284,18 @@ impl Storage { } } + pub(crate) async fn get_async(&self, path: &str, max_size: usize) -> Result { + 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 { let mut blob = match &self.backend { StorageBackend::Database(db) => db.get(path, max_size, None), diff --git a/src/web/build_details.rs b/src/web/build_details.rs index 79c59bea7..04b8a3997 100644 --- a/src/web/build_details.rs +++ b/src/web/build_details.rs @@ -9,6 +9,7 @@ use crate::{ }, Config, Storage, }; +use anyhow::Context as _; use axum::{ extract::{Extension, Path}, response::IntoResponse, @@ -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 = row.get("output"); Ok(( row, @@ -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 { diff --git a/src/web/file.rs b/src/web/file.rs index 34641c97b..444742fd4 100644 --- a/src/web/file.rs +++ b/src/web/file.rs @@ -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 { + pub(super) async fn from_path(storage: &Storage, path: &str, config: &Config) -> Result { 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?)) } } diff --git a/src/web/rustdoc.rs b/src/web/rustdoc.rs index fc0174d64..066bebfd9 100644 --- a/src/web/rustdoc.rs +++ b/src/web/rustdoc.rs @@ -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 @@ -927,7 +925,7 @@ pub(crate) async fn static_asset_handler( ) -> AxumResult { 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)]