From e7fa6c5a208347b0e5aa779b3ea477f5f1fe41c6 Mon Sep 17 00:00:00 2001 From: sigoden Date: Tue, 30 Apr 2024 00:25:35 +0000 Subject: refactor: improve code quality --- src/config/input.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/config') diff --git a/src/config/input.rs b/src/config/input.rs index eaa567a..b7b896c 100644 --- a/src/config/input.rs +++ b/src/config/input.rs @@ -2,10 +2,9 @@ use super::role::Role; use super::session::Session; use crate::client::{ImageUrl, MessageContent, MessageContentPart, ModelCapabilities}; -use crate::utils::sha256sum; +use crate::utils::{base64_encode, sha256}; use anyhow::{bail, Context, Result}; -use base64::{self, engine::general_purpose::STANDARD, Engine}; use fancy_regex::Regex; use lazy_static::lazy_static; use mime_guess::from_path; @@ -56,7 +55,7 @@ impl Input { if is_image { let data_url = read_media_to_data_url(&file_path) .with_context(|| format!("Unable to read media file '{file_item}'"))?; - data_urls.insert(sha256sum(&data_url), file_path.display().to_string()); + data_urls.insert(sha256(&data_url), file_path.display().to_string()); medias.push(data_url) } else { let text = read_file(&file_path) @@ -211,7 +210,7 @@ impl InputContext { pub fn resolve_data_url(data_urls: &HashMap, data_url: String) -> String { if data_url.starts_with("data:") { - let hash = sha256sum(&data_url); + let hash = sha256(&data_url); if let Some(path) = data_urls.get(&hash) { return path.to_string(); } @@ -251,7 +250,7 @@ fn read_media_to_data_url>(image_path: P) -> Result { let mut buffer = Vec::new(); file.read_to_end(&mut buffer)?; - let encoded_image = STANDARD.encode(buffer); + let encoded_image = base64_encode(buffer); let data_url = format!("data:{};base64,{}", mime_type, encoded_image); Ok(data_url) -- cgit v1.2.3