From fee42a6b11cab97f8dd66357bf147033f0058ee7 Mon Sep 17 00:00:00 2001 From: Mike Chong Date: Tue, 18 Jun 2024 14:28:55 +0000 Subject: [PATCH] fix --- core/src/services/gdrive/core.rs | 31 ++++++++++++++++++++++++++---- core/src/services/gdrive/lister.rs | 5 ++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/core/src/services/gdrive/core.rs b/core/src/services/gdrive/core.rs index 2d0d60f8e828..3accf002b393 100644 --- a/core/src/services/gdrive/core.rs +++ b/core/src/services/gdrive/core.rs @@ -57,7 +57,7 @@ impl Debug for GdriveCore { impl GdriveCore { pub async fn gdrive_stat(&self, path: &str) -> Result> { - let path = build_abs_path(&self.root, path); + let path = gdrive_normalize_dir_path(build_abs_path(&self.root, path)); let file_id = self.path_cache.get(&path).await?.ok_or(Error::new( ErrorKind::NotFound, format!("path not found: {}", path), @@ -77,7 +77,7 @@ impl GdriveCore { } pub async fn gdrive_get(&self, path: &str, range: BytesRange) -> Result> { - let path = build_abs_path(&self.root, path); + let path = gdrive_normalize_dir_path(build_abs_path(&self.root, path)); let path_id = self.path_cache.get(&path).await?.ok_or(Error::new( ErrorKind::NotFound, format!("path not found: {}", path), @@ -184,7 +184,10 @@ impl GdriveCore { size: u64, body: Buffer, ) -> Result> { - let parent = self.path_cache.ensure_dir(get_parent(path)).await?; + let parent = self + .path_cache + .ensure_dir(&gdrive_normalize_dir_path(get_parent(path).to_owned())) + .await?; let url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"; @@ -397,7 +400,7 @@ impl PathQuery for GdrivePathQuery { let url = "https://www.googleapis.com/drive/v3/files"; // trim "/" at the end of name - let name = name.trim_end_matches("/"); + let name = gdrive_normalize_dir_path(name.to_owned()); let content = serde_json::to_vec(&json!({ "name": name, @@ -457,3 +460,23 @@ pub(crate) struct GdriveFileList { pub(crate) files: Vec, pub(crate) next_page_token: Option, } + +/// On Google Drive, dir path name cannot end with slash (/) because if we add a slash, it would be different from the dir name +pub(super) fn gdrive_normalize_dir_path(path: String) -> String { + if path == "/" { + path + } else if path.ends_with('/') { + path.split_at(path.len() - 1).0.to_owned() + } else { + path + } +} + +#[test] +fn test_normalize_dir_path() { + assert_eq!(gdrive_normalize_dir_path("/".to_owned()), "/"); + assert_eq!(gdrive_normalize_dir_path("/a".to_owned()), "/a"); + assert_eq!(gdrive_normalize_dir_path("/a/".to_owned()), "/a"); + assert_eq!(gdrive_normalize_dir_path("/a/b".to_owned()), "/a/b"); + assert_eq!(gdrive_normalize_dir_path("/a/b/".to_owned()), "/a/b"); +} diff --git a/core/src/services/gdrive/lister.rs b/core/src/services/gdrive/lister.rs index e77a5022da7a..f99a519b829d 100644 --- a/core/src/services/gdrive/lister.rs +++ b/core/src/services/gdrive/lister.rs @@ -32,7 +32,10 @@ pub struct GdriveLister { impl GdriveLister { pub fn new(path: String, core: Arc) -> Self { - Self { path, core } + Self { + path: super::core::gdrive_normalize_dir_path(path), + core, + } } }