Don't sniff media type in save_file()
This commit is contained in:
parent
5b3aa2a24b
commit
68e464c813
3 changed files with 22 additions and 12 deletions
|
@ -12,7 +12,7 @@ use crate::http_signatures::create::{
|
||||||
create_http_signature,
|
create_http_signature,
|
||||||
HttpSignatureError,
|
HttpSignatureError,
|
||||||
};
|
};
|
||||||
use crate::utils::files::save_file;
|
use crate::utils::files::{save_file, sniff_media_type};
|
||||||
use crate::utils::urls::guess_protocol;
|
use crate::utils::urls::guess_protocol;
|
||||||
use crate::webfinger::types::JsonResourceDescriptor;
|
use crate::webfinger::types::JsonResourceDescriptor;
|
||||||
|
|
||||||
|
@ -118,8 +118,13 @@ pub async fn fetch_file(
|
||||||
if file_data.len() > FILE_MAX_SIZE as usize {
|
if file_data.len() > FILE_MAX_SIZE as usize {
|
||||||
return Err(FetchError::OtherError("file is too large"));
|
return Err(FetchError::OtherError("file is too large"));
|
||||||
};
|
};
|
||||||
let (file_name, media_type) = save_file(file_data.to_vec(), output_dir, None)?;
|
let maybe_media_type = sniff_media_type(&file_data);
|
||||||
Ok((file_name, media_type))
|
let file_name = save_file(
|
||||||
|
file_data.to_vec(),
|
||||||
|
output_dir,
|
||||||
|
maybe_media_type.as_deref(),
|
||||||
|
)?;
|
||||||
|
Ok((file_name, maybe_media_type))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn perform_webfinger_query(
|
pub async fn perform_webfinger_query(
|
||||||
|
|
|
@ -40,11 +40,18 @@ pub fn save_b64_file(
|
||||||
if data.len() > UPLOAD_MAX_SIZE {
|
if data.len() > UPLOAD_MAX_SIZE {
|
||||||
return Err(UploadError::TooLarge);
|
return Err(UploadError::TooLarge);
|
||||||
};
|
};
|
||||||
|
// Sniff media type if not provided
|
||||||
|
maybe_media_type = maybe_media_type.or(sniff_media_type(&data));
|
||||||
if maybe_media_type.as_deref() == Some("image/svg+xml") {
|
if maybe_media_type.as_deref() == Some("image/svg+xml") {
|
||||||
// Don't treat SVG files as images
|
// Don't treat SVG files as images
|
||||||
maybe_media_type = None;
|
maybe_media_type = None;
|
||||||
};
|
};
|
||||||
Ok(save_file(data, output_dir, maybe_media_type)?)
|
let file_name = save_file(
|
||||||
|
data,
|
||||||
|
output_dir,
|
||||||
|
maybe_media_type.as_deref(),
|
||||||
|
)?;
|
||||||
|
Ok((file_name, maybe_media_type))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save_validated_b64_file(
|
pub fn save_validated_b64_file(
|
||||||
|
@ -61,7 +68,6 @@ pub fn save_validated_b64_file(
|
||||||
if !media_type.starts_with(media_type_prefix) {
|
if !media_type.starts_with(media_type_prefix) {
|
||||||
return Err(UploadError::InvalidMediaType);
|
return Err(UploadError::InvalidMediaType);
|
||||||
};
|
};
|
||||||
let (file_name, _) =
|
let file_name = save_file(data, output_dir, Some(&media_type))?;
|
||||||
save_file(data, output_dir, Some(media_type.clone()))?;
|
|
||||||
Ok((file_name, media_type))
|
Ok((file_name, media_type))
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,17 +43,16 @@ pub fn set_file_permissions(file_path: &Path, mode: u32) -> Result<(), Error> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Save validated file to specified directory
|
||||||
pub fn save_file(
|
pub fn save_file(
|
||||||
data: Vec<u8>,
|
data: Vec<u8>,
|
||||||
output_dir: &Path,
|
output_dir: &Path,
|
||||||
media_type: Option<String>,
|
media_type: Option<&str>,
|
||||||
) -> Result<(String, Option<String>), Error> {
|
) -> Result<String, Error> {
|
||||||
// Sniff media type if not provided
|
let file_name = get_file_name(&data, media_type);
|
||||||
let media_type = media_type.or(sniff_media_type(&data));
|
|
||||||
let file_name = get_file_name(&data, media_type.as_deref());
|
|
||||||
let file_path = output_dir.join(&file_name);
|
let file_path = output_dir.join(&file_name);
|
||||||
write_file(&data, &file_path)?;
|
write_file(&data, &file_path)?;
|
||||||
Ok((file_name, media_type))
|
Ok(file_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_file_url(instance_url: &str, file_name: &str) -> String {
|
pub fn get_file_url(instance_url: &str, file_name: &str) -> String {
|
||||||
|
|
Loading…
Reference in a new issue