Skip to content

Commit

Permalink
refactor: simplify compression_static module
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Mar 8, 2023
1 parent b9fa2bf commit 7490697
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 45 deletions.
81 changes: 45 additions & 36 deletions src/compression_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HeaderValue>,
) -> Option<(PathBuf, Metadata, &'a str)> {
let mut precompressed = None;

) -> Option<CompressedFileVariant<'a>> {
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 },
})
}
37 changes: 28 additions & 9 deletions src/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
));
}
}

Expand All @@ -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)),
));
}
}
}
Expand All @@ -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)),
));
}
}

Expand All @@ -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)),
));
}
}
}
Expand Down

0 comments on commit 7490697

Please sign in to comment.