feat(snix/castore-http): pass extension to respond_file()
respond_file only uses requested_path to (only) calculate an extension for mime type guessing, and getting to there is a bit messy. Use PathComponent::extension to get the extension, and replace `requested_path: Option<std::path::Path>` with `extension: Option<&str>`. Change-Id: I0582a1127f8b505fdf77406fafb3d3b206d694f2 Reviewed-on: https://cl.snix.dev/c/snix/+/30320 Tested-by: besadii Reviewed-by: Stefan Junker <mail@stefanjunker.de>
This commit is contained in:
parent
15b1415860
commit
fd5f316fb8
1 changed files with 10 additions and 12 deletions
|
|
@ -81,13 +81,13 @@ pub async fn get_root_node_contents<BS: BlobService, DS: DirectoryService, S: As
|
||||||
{
|
{
|
||||||
match found_index_node {
|
match found_index_node {
|
||||||
Node::File { digest, size, .. } => {
|
Node::File { digest, size, .. } => {
|
||||||
let found_index_file_path = found_index_file_path.to_string();
|
let extension = found_index_file_path
|
||||||
let found_index_file_path = path::Path::new(OsStr::from_bytes(
|
.extension()
|
||||||
found_index_file_path.as_bytes(),
|
.and_then(|b| std::str::from_utf8(b).ok());
|
||||||
));
|
|
||||||
return respond_file(
|
return respond_file(
|
||||||
blob_service,
|
blob_service,
|
||||||
Some(found_index_file_path),
|
extension,
|
||||||
range_header,
|
range_header,
|
||||||
digest,
|
digest,
|
||||||
*size,
|
*size,
|
||||||
|
|
@ -111,11 +111,11 @@ pub async fn get_root_node_contents<BS: BlobService, DS: DirectoryService, S: As
|
||||||
Err(StatusCode::FORBIDDEN)
|
Err(StatusCode::FORBIDDEN)
|
||||||
}
|
}
|
||||||
Node::File { digest, size, .. } => {
|
Node::File { digest, size, .. } => {
|
||||||
let requested_path =
|
|
||||||
path::Path::new(OsStr::from_bytes(requested_path.as_bytes()));
|
|
||||||
respond_file(
|
respond_file(
|
||||||
blob_service,
|
blob_service,
|
||||||
Some(requested_path),
|
requested_path
|
||||||
|
.extension()
|
||||||
|
.and_then(|b| std::str::from_utf8(b).ok()),
|
||||||
range_header,
|
range_header,
|
||||||
&digest,
|
&digest,
|
||||||
size,
|
size,
|
||||||
|
|
@ -221,7 +221,7 @@ pub async fn respond_directory_list(
|
||||||
#[instrument(level = "trace", skip_all, fields(digest, size))]
|
#[instrument(level = "trace", skip_all, fields(digest, size))]
|
||||||
pub async fn respond_file<BS: BlobService>(
|
pub async fn respond_file<BS: BlobService>(
|
||||||
blob_service: BS,
|
blob_service: BS,
|
||||||
requested_path: Option<&path::Path>,
|
extension: Option<&str>,
|
||||||
range_header: Option<TypedHeader<Range>>,
|
range_header: Option<TypedHeader<Range>>,
|
||||||
digest: &B3Digest,
|
digest: &B3Digest,
|
||||||
size: u64,
|
size: u64,
|
||||||
|
|
@ -238,9 +238,7 @@ pub async fn respond_file<BS: BlobService>(
|
||||||
StatusCode::NOT_FOUND
|
StatusCode::NOT_FOUND
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let mime_type = requested_path
|
let mime_type = extension
|
||||||
.and_then(path::Path::extension)
|
|
||||||
.and_then(std::ffi::OsStr::to_str)
|
|
||||||
.and_then(|extension| mime_guess::from_ext(extension).first())
|
.and_then(|extension| mime_guess::from_ext(extension).first())
|
||||||
.unwrap_or(mime::APPLICATION_OCTET_STREAM);
|
.unwrap_or(mime::APPLICATION_OCTET_STREAM);
|
||||||
match range_header {
|
match range_header {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue