From 5b5e561afbb161e25d77f3ae5f2ae50ffa2b1755 Mon Sep 17 00:00:00 2001 From: "Nathan.fooo" <86001920+appflowy@users.noreply.github.com> Date: Tue, 3 Sep 2024 21:30:33 +0800 Subject: [PATCH] chore: decode file url (#786) --- libs/client-api/src/http_blob.rs | 26 +++++++++++++++++++++++++- tests/file_test/delete_dir_test.rs | 6 ++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/libs/client-api/src/http_blob.rs b/libs/client-api/src/http_blob.rs index 6a4467070..d9aecac6c 100644 --- a/libs/client-api/src/http_blob.rs +++ b/libs/client-api/src/http_blob.rs @@ -5,12 +5,13 @@ use app_error::AppError; use bytes::Bytes; use futures_util::TryStreamExt; use mime::Mime; -use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; +use percent_encoding::{percent_decode_str, utf8_percent_encode, NON_ALPHANUMERIC}; use reqwest::{header, Method, StatusCode}; use shared_entity::dto::workspace_dto::{BlobMetadata, RepeatedBlobMetaData}; use shared_entity::response::{AppResponse, AppResponseError}; use tracing::instrument; +use url::Url; impl Client { pub fn get_blob_url(&self, workspace_id: &str, file_id: &str) -> String { @@ -75,6 +76,29 @@ impl Client { ) } + /// Returns the workspace_id, parent_dir, and file_id from the given blob url. + pub fn parse_blob_url_v1(&self, url: &str) -> Option<(String, String, String)> { + let parsed_url = Url::parse(url).ok()?; + let segments: Vec<&str> = parsed_url.path_segments()?.collect(); + // Check if the path has the expected number of segments + if segments.len() < 6 { + return None; + } + + // Extract the workspace_id, parent_dir, and file_id from the segments + let workspace_id = segments[2].to_string(); + let encoded_parent_dir = segments[5].to_string(); + let file_id = segments[6].to_string(); + + // Decode the percent-encoded parent_dir + let parent_dir = percent_decode_str(&encoded_parent_dir) + .decode_utf8() + .ok()? + .to_string(); + + Some((workspace_id, parent_dir, file_id)) + } + #[instrument(level = "info", skip_all)] pub async fn get_blob_v1( &self, diff --git a/tests/file_test/delete_dir_test.rs b/tests/file_test/delete_dir_test.rs index 40bcfc46b..9167afdb4 100644 --- a/tests/file_test/delete_dir_test.rs +++ b/tests/file_test/delete_dir_test.rs @@ -81,6 +81,12 @@ async fn delete_workspace_sub_folder_resource_test() { .unwrap() .1; let blob_text = String::from_utf8(blob.to_vec()).unwrap(); + + let url = c1.get_blob_url_v1(&workspace_id, &parent_dir, &file_id); + let (workspace_id_2, parent_dir_2, file_id_2) = c1.parse_blob_url_v1(&url).unwrap(); + assert_eq!(workspace_id, workspace_id_2); + assert_eq!(parent_dir, parent_dir_2); + assert_eq!(file_id, file_id_2); assert_eq!(blob_text, text); } c1.delete_workspace(&workspace_id).await.unwrap();