From 5ad8443c8d6d9ed3f9d28ac52af529ceb41d741f Mon Sep 17 00:00:00 2001 From: Vika Date: Wed, 1 Jan 2025 05:42:45 +0300 Subject: Get rid of base64 and hex in favor of data_encoding crate Less dependency duplication = more fun Change-Id: Icbd0497a68fdd5bea3757e3c62c80008b87bce96 --- Cargo.lock | 2 -- Cargo.toml | 2 -- src/login.rs | 8 +++++++- src/main.rs | 3 +-- src/media/storage/file.rs | 16 ++++++++-------- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 66064b3..532723c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1876,7 +1876,6 @@ dependencies = [ "async-trait", "axum", "axum-extra", - "base64 0.22.1", "bytes", "chrono", "clap", @@ -1885,7 +1884,6 @@ dependencies = [ "faker_rand", "futures", "futures-util", - "hex", "html5ever", "http-cache-reqwest", "hyper", diff --git a/Cargo.toml b/Cargo.toml index 18081ac..467ad08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -124,7 +124,6 @@ argon2 = { version = "0.5.3", features = ["std"] } async-trait = "0.1.83" axum = { workspace = true, features = ["multipart", "json", "form", "macros"] } axum-extra = { version = "0.9.6", features = ["cookie", "cookie-signed", "typed-header"] } -base64 = "0.22.1" bytes = "1.9.0" chrono = { workspace = true } clap = { workspace = true, features = ["derive"], optional = true } @@ -132,7 +131,6 @@ data-encoding = { workspace = true } either = "1.13.0" futures = { workspace = true } futures-util = { workspace = true } -hex = "0.4.3" html5ever = "=0.27.0" http-cache-reqwest = { version = "0.15.0", default-features = false, features = ["manager-moka"] } hyper = "1.5.2" diff --git a/src/login.rs b/src/login.rs index 646c832..19bfaf7 100644 --- a/src/login.rs +++ b/src/login.rs @@ -332,7 +332,13 @@ async fn client_metadata( digest.update(b" "); digest.update(crate::OAUTH2_SOFTWARE_ID.as_bytes()); - let etag = String::from("W/") + &hex::encode(digest.finalize()); + let etag = { + let mut etag = String::with_capacity(66); + etag.push_str("W/"); + data_encoding::HEXLOWER.encode_append(&digest.finalize(), &mut etag); + + etag + }; axum_extra::headers::ETag::from_str(&etag).unwrap() }; if let Some(cached) = cached { diff --git a/src/main.rs b/src/main.rs index 3aee6c3..f57b0be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use base64::Engine; use kittybox::{database::Storage, indieauth::backend::AuthBackend, media::storage::MediaStore, webmentions::Webmention, compose_kittybox}; use tokio::{sync::Mutex, task::JoinSet}; use std::{env, future::IntoFuture, sync::Arc}; @@ -82,7 +81,7 @@ async fn main() { // TODO: load from environment let cookie_key = axum_extra::extract::cookie::Key::from(&env::var("COOKIE_KEY") .as_deref() - .map(|s| base64::prelude::BASE64_STANDARD.decode(s.as_bytes()) + .map(|s| data_encoding::BASE64_MIME_PERMISSIVE.decode(s.as_bytes()) .expect("Invalid cookie key: must be base64 encoded") ) .unwrap() diff --git a/src/media/storage/file.rs b/src/media/storage/file.rs index 711b298..e432945 100644 --- a/src/media/storage/file.rs +++ b/src/media/storage/file.rs @@ -99,15 +99,15 @@ impl MediaStore for FileStore { tempfile.flush().await?; tempfile.into_inner().sync_all().await?; - let hash = hasher.finalize(); - debug!("Pending upload hash: {}", hex::encode(hash)); + let hash = data_encoding::HEXLOWER.encode(&hasher.finalize()); + debug!("Pending upload hash: {}", hash); let filename = format!( "{}/{}/{}/{}/{}", - hex::encode([hash[0]]), - hex::encode([hash[1]]), - hex::encode([hash[2]]), - hex::encode([hash[3]]), - hex::encode(&hash[4..32]) + &hash[0..2], + &hash[2..4], + &hash[4..6], + &hash[6..8], + &hash[8..] ); let domain_str = domain.to_string(); let filepath = self.base.join(domain_str.as_str()).join(&filename); @@ -115,7 +115,7 @@ impl MediaStore for FileStore { let metapath = self.base.join(domain_str.as_str()).join(&metafilename); let metatemppath = self.base.join(domain_str.as_str()).join(metafilename + ".tmp"); metadata.length = std::num::NonZeroUsize::new(length); - metadata.etag = Some(hex::encode(hash)); + metadata.etag = Some(hash); debug!("File path: {}, metadata: {}", filepath.display(), metapath.display()); { let parent = filepath.parent().unwrap(); -- cgit 1.4.1