From 6c6eb731f97dc02a67199e05df529313c9bb6644 Mon Sep 17 00:00:00 2001 From: silverpill Date: Fri, 20 Jan 2023 00:18:50 +0000 Subject: [PATCH] Use usize type for file sizes --- src/activitypub/actors/helpers.rs | 2 +- src/activitypub/fetcher/fetchers.rs | 6 ++++-- src/activitypub/handlers/create.rs | 2 +- src/models/posts/validators.rs | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/activitypub/actors/helpers.rs b/src/activitypub/actors/helpers.rs index b37b1f1..6b2fc99 100644 --- a/src/activitypub/actors/helpers.rs +++ b/src/activitypub/actors/helpers.rs @@ -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( instance: &Instance, diff --git a/src/activitypub/fetcher/fetchers.rs b/src/activitypub/fetcher/fetchers.rs index 4ad07ee..0c986b0 100644 --- a/src/activitypub/fetcher/fetchers.rs +++ b/src/activitypub/fetcher/fetchers.rs @@ -108,7 +108,7 @@ pub async fn fetch_file( instance: &Instance, url: &str, maybe_media_type: Option<&str>, - file_max_size: u64, + file_max_size: usize, output_dir: &Path, ) -> Result<(String, Option), FetchError> { let client = build_client(instance)?; @@ -116,12 +116,14 @@ pub async fn fetch_file( build_request(instance, client, Method::GET, url); let response = request_builder.send().await?.error_for_status()?; 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 { return Err(FetchError::OtherError("file is too large")); }; }; 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")); }; let maybe_media_type = maybe_media_type diff --git a/src/activitypub/handlers/create.rs b/src/activitypub/handlers/create.rs index b85deb4..7d3b995 100644 --- a/src/activitypub/handlers/create.rs +++ b/src/activitypub/handlers/create.rs @@ -142,7 +142,7 @@ fn get_note_visibility( 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 { if !author_id.contains("/index.php/user/") { diff --git a/src/models/posts/validators.rs b/src/models/posts/validators.rs index bae6f78..594b774 100644 --- a/src/models/posts/validators.rs +++ b/src/models/posts/validators.rs @@ -2,7 +2,7 @@ use crate::errors::ValidationError; use crate::utils::html::clean_html_strict; 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] = [ "image/gif", "image/png",