From 47cfba4c222a1c6d8afa322f56fc1a8135e0ad1a Mon Sep 17 00:00:00 2001 From: Michal Pokrywka Date: Sat, 9 May 2026 10:19:03 +0200 Subject: [PATCH] Added `ImageUpload` helper struct to `DropImageFile` --- CHANGES.md | 3 ++- src/form/data/mod.rs | 2 +- src/image_upload.rs | 33 +++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/image_upload.rs diff --git a/CHANGES.md b/CHANGES.md index 6c2da3d..2141bf6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,7 +6,8 @@ ### Added * `ResourceTable` -* "Select file" button to `DropImageFile` component +* `ImageUpload` helper struct to `DropImageFile` +* "Select file" button to `DropImageFile` ### Fixed diff --git a/src/form/data/mod.rs b/src/form/data/mod.rs index b6eea43..454e19c 100644 --- a/src/form/data/mod.rs +++ b/src/form/data/mod.rs @@ -4,7 +4,7 @@ use vertigo::Value; use crate::ValidationErrors; mod data_field; -pub use data_field::{DataFieldValue, ImageValue, TextAreaValue}; +pub use data_field::{CustomValue, DataFieldValue, ImageValue, TextAreaValue}; mod form_export; pub use form_export::{FieldExport, FormExport}; diff --git a/src/image_upload.rs b/src/image_upload.rs new file mode 100644 index 0000000..0c40eee --- /dev/null +++ b/src/image_upload.rs @@ -0,0 +1,33 @@ +use base64::{Engine as _, engine::general_purpose::STANDARD_NO_PAD as BASE_64}; +use vertigo::DropFileItem; + +use crate::name_to_mime; + +#[derive(Clone, Debug, PartialEq, vertigo::AutoJsJson)] +pub struct ImageUpload { + pub name: String, + pub mime: Option, + pub data: String, // base64 +} + +impl ImageUpload { + pub fn to_vec(&self) -> Result, base64::DecodeError> { + BASE_64.decode(&self.data) + } +} + +impl From for ImageUpload { + fn from(item: DropFileItem) -> Self { + let mime_str = name_to_mime(&item.name); + let mime = if mime_str == "application/octet-stream" { + None + } else { + Some(mime_str.to_string()) + }; + Self { + name: item.name.clone(), + mime, + data: BASE_64.encode(&*item.data), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index e192524..62dcfa8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ use std::{collections::HashMap, fmt::Display, str::FromStr}; pub mod button; mod drop_image_file; pub mod form; +mod image_upload; mod input; pub mod login; mod popup; @@ -18,6 +19,7 @@ mod with_loader; pub use { button::{Button, ButtonColor, ButtonVariant, TableButton}, drop_image_file::{DropImageFile, DropImageFileParams, image_as_uri, name_to_mime}, + image_upload::ImageUpload, input::{Input, InputWithButton, InputWithButtonParams, ListInput}, popup::{Popup, PopupOnHover, PopupParams}, search_panel::{SearchPanel, SearchPanelParams, SearchResult},