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
32 changes: 29 additions & 3 deletions core/src/services/gdrive/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Debug for GdriveCore {

impl GdriveCore {
pub async fn gdrive_stat(&self, path: &str) -> Result<Response<Buffer>> {
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),
Expand All @@ -77,7 +77,7 @@ impl GdriveCore {
}

pub async fn gdrive_get(&self, path: &str, range: BytesRange) -> Result<Response<HttpBody>> {
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),
Expand Down Expand Up @@ -184,7 +184,10 @@ impl GdriveCore {
size: u64,
body: Buffer,
) -> Result<Response<Buffer>> {
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";

Expand Down Expand Up @@ -396,6 +399,9 @@ impl PathQuery for GdrivePathQuery {
async fn create_dir(&self, parent_id: &str, name: &str) -> Result<String> {
let url = "https://www.googleapis.com/drive/v3/files";

// trim "/" at the end of name
let name = gdrive_normalize_dir_path(name.to_owned());

let content = serde_json::to_vec(&json!({
"name": name,
"mimeType": "application/vnd.google-apps.folder",
Expand Down Expand Up @@ -454,3 +460,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