chore: decode file url (#786)

This commit is contained in:
Nathan.fooo 2024-09-03 21:30:33 +08:00 committed by GitHub
parent 72652086f5
commit 5b5e561afb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View File

@ -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,

View File

@ -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();