Use usize type for file sizes

This commit is contained in:
silverpill 2023-01-20 00:18:50 +00:00
parent c26fc9235d
commit 6c6eb731f9
4 changed files with 7 additions and 5 deletions

View file

@ -19,7 +19,7 @@ use crate::models::profiles::{
}, },
}; };
const ACTOR_IMAGE_MAX_SIZE: u64 = 5 * 1000 * 1000; // 5 MB const ACTOR_IMAGE_MAX_SIZE: usize = 5 * 1000 * 1000; // 5 MB
async fn fetch_actor_images( async fn fetch_actor_images(
instance: &Instance, instance: &Instance,

View file

@ -108,7 +108,7 @@ pub async fn fetch_file(
instance: &Instance, instance: &Instance,
url: &str, url: &str,
maybe_media_type: Option<&str>, maybe_media_type: Option<&str>,
file_max_size: u64, file_max_size: usize,
output_dir: &Path, output_dir: &Path,
) -> Result<(String, Option<String>), FetchError> { ) -> Result<(String, Option<String>), FetchError> {
let client = build_client(instance)?; let client = build_client(instance)?;
@ -116,12 +116,14 @@ pub async fn fetch_file(
build_request(instance, client, Method::GET, url); build_request(instance, client, Method::GET, url);
let response = request_builder.send().await?.error_for_status()?; let response = request_builder.send().await?.error_for_status()?;
if let Some(file_size) = response.content_length() { if let Some(file_size) = response.content_length() {
let file_size: usize = file_size.try_into()
.expect("value should be within bounds");
if file_size > file_max_size { if file_size > file_max_size {
return Err(FetchError::OtherError("file is too large")); return Err(FetchError::OtherError("file is too large"));
}; };
}; };
let file_data = response.bytes().await?; let file_data = response.bytes().await?;
if file_data.len() > file_max_size as usize { if file_data.len() > file_max_size {
return Err(FetchError::OtherError("file is too large")); return Err(FetchError::OtherError("file is too large"));
}; };
let maybe_media_type = maybe_media_type let maybe_media_type = maybe_media_type

View file

@ -142,7 +142,7 @@ fn get_note_visibility(
Visibility::Direct Visibility::Direct
} }
const ATTACHMENT_MAX_SIZE: u64 = 20 * 1000 * 1000; const ATTACHMENT_MAX_SIZE: usize = 20 * 1000 * 1000; // 20 MB
fn is_gnu_social_link(author_id: &str, attachment: &Attachment) -> bool { fn is_gnu_social_link(author_id: &str, attachment: &Attachment) -> bool {
if !author_id.contains("/index.php/user/") { if !author_id.contains("/index.php/user/") {

View file

@ -2,7 +2,7 @@ use crate::errors::ValidationError;
use crate::utils::html::clean_html_strict; use crate::utils::html::clean_html_strict;
pub const ATTACHMENTS_MAX_NUM: usize = 15; pub const ATTACHMENTS_MAX_NUM: usize = 15;
pub const EMOJI_MAX_SIZE: u64 = 250 * 1000; // 250 kB pub const EMOJI_MAX_SIZE: usize = 250 * 1000; // 250 kB
pub const EMOJI_MEDIA_TYPES: [&str; 2] = [ pub const EMOJI_MEDIA_TYPES: [&str; 2] = [
"image/gif", "image/gif",
"image/png", "image/png",