Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core/services/gdrive): Fix gdrive create_dir request: trim trailing / #4732

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
27 changes: 25 additions & 2 deletions core/src/services/gdrive/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ impl PathQuery for GdrivePathQuery {
// Make sure name has been replaced with escaped name.
//
// ref: <https://developers.google.com/drive/api/guides/ref-search-terms>
format!("name = '{}'", name.replace('\'', "\\'")),
format!(
"name = '{}'",
gdrive_normalize_dir_path(name.to_owned()).replace('\'', "\\'")
),
format!("'{}' in parents", parent_id),
"trashed = false".to_string(),
];
Expand Down Expand Up @@ -397,7 +400,7 @@ impl PathQuery for GdrivePathQuery {
let url = "https://www.googleapis.com/drive/v3/files";

let content = serde_json::to_vec(&json!({
"name": name,
"name": gdrive_normalize_dir_path(name.to_owned()),
"mimeType": "application/vnd.google-apps.folder",
// If the parent is not provided, the folder will be created in the root folder.
"parents": [parent_id],
Expand Down Expand Up @@ -454,3 +457,23 @@ pub(crate) struct GdriveFileList {
pub(crate) files: Vec<GdriveFile>,
pub(crate) next_page_token: Option<String>,
}

/// 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");
}
5 changes: 4 additions & 1 deletion core/src/services/gdrive/lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ pub struct GdriveLister {

impl GdriveLister {
pub fn new(path: String, core: Arc<GdriveCore>) -> Self {
Self { path, core }
Self {
path: super::core::gdrive_normalize_dir_path(path),
core,
}
}
}

Expand Down
Loading