Skip to content

Commit

Permalink
chore: decode file url (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy authored Sep 3, 2024
1 parent 7265208 commit 5b5e561
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
26 changes: 25 additions & 1 deletion libs/client-api/src/http_blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions tests/file_test/delete_dir_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 5b5e561

Please sign in to comment.