From 4d6057ba266a2a71182a2d547d8665a9892a5787 Mon Sep 17 00:00:00 2001 From: Jorge Hermo Date: Sat, 2 Nov 2024 13:21:25 +0100 Subject: [PATCH] refactor: apply parse_prefixed_metadata for oss --- core/src/services/azblob/backend.rs | 1 - core/src/services/oss/backend.rs | 4 +--- core/src/services/oss/core.rs | 29 ++++++----------------------- 3 files changed, 7 insertions(+), 27 deletions(-) diff --git a/core/src/services/azblob/backend.rs b/core/src/services/azblob/backend.rs index 90c57de584e..4f733593ccd 100644 --- a/core/src/services/azblob/backend.rs +++ b/core/src/services/azblob/backend.rs @@ -33,7 +33,6 @@ use reqsign::AzureStorageSigner; use sha2::Digest; use sha2::Sha256; -use super::core::constants; use super::core::constants::X_MS_META_NAME_PREFIX; use super::error::parse_error; use super::lister::AzblobLister; diff --git a/core/src/services/oss/backend.rs b/core/src/services/oss/backend.rs index 7e2d67b3caf..a627e1d4a0d 100644 --- a/core/src/services/oss/backend.rs +++ b/core/src/services/oss/backend.rs @@ -489,9 +489,7 @@ impl Access for OssBackend { match status { StatusCode::OK => { let headers = resp.headers(); - let mut meta = - self.core - .parse_metadata(path, constants::X_OSS_META_PREFIX, resp.headers())?; + let mut meta = self.core.parse_metadata(path, resp.headers())?; if let Some(v) = parse_header_to_str(headers, "x-oss-version-id")? { meta.set_version(v); diff --git a/core/src/services/oss/core.rs b/core/src/services/oss/core.rs index d4dff76348e..85554277076 100644 --- a/core/src/services/oss/core.rs +++ b/core/src/services/oss/core.rs @@ -15,13 +15,13 @@ // specific language governing permissions and limitations // under the License. -use std::collections::HashMap; use std::fmt::Debug; use std::fmt::Formatter; use std::fmt::Write; use std::time::Duration; use bytes::Bytes; +use constants::X_OSS_META_PREFIX; use http::header::CACHE_CONTROL; use http::header::CONTENT_DISPOSITION; use http::header::CONTENT_LENGTH; @@ -190,7 +190,7 @@ impl OssCore { "the format of the user metadata key is invalid, please refer the document", )); } - req = req.header(format!("{}{}", constants::X_OSS_META_PREFIX, key), value) + req = req.header(format!("{X_OSS_META_PREFIX}{key}"), value) } } @@ -213,28 +213,11 @@ impl OssCore { /// # Notes /// /// before return the user defined metadata, we'll strip the user_metadata_prefix from the key - pub fn parse_metadata( - &self, - path: &str, - user_metadata_prefix: &str, - headers: &HeaderMap, - ) -> Result { + pub fn parse_metadata(&self, path: &str, headers: &HeaderMap) -> Result { let mut m = parse_into_metadata(path, headers)?; - - let data: HashMap = headers - .iter() - .filter_map(|(key, _)| { - key.as_str() - .strip_prefix(user_metadata_prefix) - .and_then(|stripped_key| { - parse_header_to_str(headers, key) - .unwrap_or(None) - .map(|val| (stripped_key.to_string(), val.to_string())) - }) - }) - .collect(); - if !data.is_empty() { - m.with_user_metadata(data); + let user_meta = parse_prefixed_headers(&headers, X_OSS_META_PREFIX); + if !user_meta.is_empty() { + m.with_user_metadata(user_meta); } Ok(m)