Skip to content

Commit

Permalink
fix: better handle extensions for unrecognized mimetypes
Browse files Browse the repository at this point in the history
Previously we'd error out if we couldn't decide on a filename for the
file. Now, if we don't assign an extension based on the mimetype, we'll
simply return the original filename unmodified.
  • Loading branch information
mistydemeo committed May 22, 2024
1 parent 144c15b commit 174418b
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,11 @@ impl RemoteAsset {
}
}

fn extension(mimetype: mime::Mime, origin_path: &str) -> Result<String> {
fn extension(mimetype: mime::Mime, origin_path: &str) -> Option<String> {
match mimetype.type_() {
mime::IMAGE => RemoteAsset::image_extension(mimetype, origin_path),
mime::TEXT => RemoteAsset::text_extension(mimetype, origin_path),
_ => Err(AxoassetError::RemoteAssetMimeTypeNotSupported {
origin_path: origin_path.to_string(),
mimetype: mimetype.to_string(),
}),
mime::IMAGE => RemoteAsset::image_extension(mimetype, origin_path).ok(),
mime::TEXT => RemoteAsset::text_extension(mimetype, origin_path).ok(),
_ => None,
}
}

Expand Down Expand Up @@ -179,10 +176,12 @@ impl RemoteAsset {
filestem.remove(0);
if filestem.contains('.') {
Ok(filestem)
} else {
let extension =
RemoteAsset::extension(RemoteAsset::mimetype(headers, origin_path)?, origin_path)?;
} else if let Some(extension) =
RemoteAsset::extension(RemoteAsset::mimetype(headers, origin_path)?, origin_path)
{
Ok(format!("{filestem}.{extension}"))
} else {
Ok(filestem)
}
}
}

0 comments on commit 174418b

Please sign in to comment.