From 7490697a03ba734ea297b55ffa56272a196760f2 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Thu, 9 Mar 2023 00:59:41 +0100 Subject: [PATCH] refactor: simplify compression_static module --- src/compression_static.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++------------------------------------ src/static_files.rs | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 73 insertions(+), 45 deletions(-) diff --git a/src/compression_static.rs b/src/compression_static.rs index 3b11310..e4dfb8d 100644 --- a/src/compression_static.rs +++ b/src/compression_static.rs @@ -7,60 +7,69 @@ use std::{ use crate::{compression, static_files::file_metadata}; +/// It defines the pre-compressed file variant metadata of a particular file path. +pub struct CompressedFileVariant<'a> { + pub file_path: PathBuf, + pub metadata: Metadata, + pub extension: &'a str, +} + /// Search for the pre-compressed variant of the given file path. pub async fn precompressed_variant<'a>( file_path: &Path, headers: &'a HeaderMap, -) -> Option<(PathBuf, Metadata, &'a str)> { - let mut precompressed = None; - +) -> Option> { tracing::trace!( - "preparing pre-compressed file path variant of {}", + "preparing pre-compressed file variant path of {}", file_path.display() ); // Determine prefered-encoding extension if available - let precomp_ext = match compression::get_prefered_encoding(headers) { + let comp_ext = match compression::get_prefered_encoding(headers) { // https://zlib.net/zlib_faq.html#faq39 - Some(ContentCoding::GZIP | ContentCoding::DEFLATE) => Some("gz"), + Some(ContentCoding::GZIP | ContentCoding::DEFLATE) => "gz", // https://peazip.github.io/brotli-compressed-file-format.html - Some(ContentCoding::BROTLI) => Some("br"), - _ => None, - }; - - if precomp_ext.is_none() { - tracing::trace!("preferred encoding based on the file extension was not determined"); - } - - // Try to find the pre-compressed metadata variant for the given file path - if let Some(ext) = precomp_ext { - let filename = file_path.file_name().and_then(OsStr::to_str); - if let Some(filename) = filename { - let precomp_file_name = [filename, ".", ext].concat(); - let filepath_precomp = file_path.with_file_name(precomp_file_name); - + Some(ContentCoding::BROTLI) => "br", + _ => { tracing::trace!( - "getting metadata for pre-compressed file variant {}", - filepath_precomp.display() + "preferred encoding based on the file extension was not determined, skipping" ); + return None; + } + }; - if let Ok((meta, is_dir)) = file_metadata(&filepath_precomp) { - if is_dir { - tracing::trace!( - "pre-compressed file variant found but it's a directory, skipping" - ); - return None; - } + let comp_name = match file_path.file_name().and_then(OsStr::to_str) { + Some(v) => v, + None => { + tracing::trace!("file name was not determined for the current path, skipping"); + return None; + } + }; - tracing::trace!("pre-compressed file variant found, serving it directly"); + let file_path = file_path.with_file_name([comp_name, ".", comp_ext].concat()); + tracing::trace!( + "trying to get the pre-compressed file variant metadata for {}", + file_path.display() + ); - let encoding = if ext == "gz" { "gzip" } else { ext }; - precompressed = Some((filepath_precomp, meta, encoding)); - } + let (metadata, is_dir) = match file_metadata(&file_path) { + Ok(v) => v, + Err(e) => { + tracing::trace!("pre-compressed file variant error: {:?}", e); + return None; } + }; - // Note: In error case like "no such file or dir" the workflow just continues + if is_dir { + tracing::trace!("pre-compressed file variant found but it's a directory, skipping"); + return None; } - precompressed + tracing::trace!("pre-compressed file variant found, serving it directly"); + + Some(CompressedFileVariant { + file_path, + metadata, + extension: if comp_ext == "gz" { "gzip" } else { comp_ext }, + }) } diff --git a/src/static_files.rs b/src/static_files.rs index 1096bc5..56fe7bc 100644 --- a/src/static_files.rs +++ b/src/static_files.rs @@ -178,10 +178,15 @@ async fn composed_file_metadata<'a>( // Pre-compressed variant check for the autoindex if compression_static { - if let Some((path, meta, ext)) = + if let Some(p) = compression_static::precompressed_variant(file_path, headers).await { - return Ok((file_path, meta, false, Some((path, ext)))); + return Ok(( + file_path, + p.metadata, + false, + Some((p.file_path, p.extension)), + )); } } @@ -205,10 +210,15 @@ async fn composed_file_metadata<'a>( } else { // Fallback pre-compressed variant check for the specific file if compression_static { - if let Some((path, meta, ext)) = + if let Some(p) = compression_static::precompressed_variant(file_path, headers).await { - return Ok((file_path, meta, false, Some((path, ext)))); + return Ok(( + file_path, + p.metadata, + false, + Some((p.file_path, p.extension)), + )); } } } @@ -218,10 +228,14 @@ async fn composed_file_metadata<'a>( Err(err) => { // Pre-compressed variant check for the file not found if compression_static { - if let Some((path, meta, ext)) = - compression_static::precompressed_variant(file_path, headers).await + if let Some(p) = compression_static::precompressed_variant(file_path, headers).await { - return Ok((file_path, meta, false, Some((path, ext)))); + return Ok(( + file_path, + p.metadata, + false, + Some((p.file_path, p.extension)), + )); } } @@ -235,10 +249,15 @@ async fn composed_file_metadata<'a>( _ => { // Last pre-compressed variant check or the prefixed file not found if compression_static { - if let Some((path, meta, ext)) = + if let Some(p) = compression_static::precompressed_variant(file_path, headers).await { - return Ok((file_path, meta, false, Some((path, ext)))); + return Ok(( + file_path, + p.metadata, + false, + Some((p.file_path, p.extension)), + )); } } } -- libgit2 1.7.2