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
34 changes: 32 additions & 2 deletions core/src/raw/path_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ impl<Q: PathQuery> PathCacher<Q> {
pub async fn insert(&self, path: &str, id: &str) {
let _guard = self.lock().await;

let path = normalize_dir_path(path);

// This should never happen, but let's ignore the insert if happened.
if self.cache.contains_key(path) {
debug_assert!(
Expand All @@ -109,13 +111,17 @@ impl<Q: PathQuery> PathCacher<Q> {
pub async fn remove(&self, path: &str) {
let _guard = self.lock().await;

let path = normalize_dir_path(path);

self.cache.invalidate(path)
}

/// Get the id for the given path.
pub async fn get(&self, path: &str) -> Result<Option<String>> {
let _guard = self.lock().await;

let path = normalize_dir_path(path);

if let Some(id) = self.cache.get(path) {
return Ok(Some(id));
}
Expand Down Expand Up @@ -158,6 +164,7 @@ impl<Q: PathQuery> PathCacher<Q> {
/// Ensure input dir exists.
pub async fn ensure_dir(&self, path: &str) -> Result<String> {
let _guard = self.lock().await;
let path = normalize_dir_path(path);

let mut tmp = "".to_string();
// All parents that need to check.
Expand All @@ -177,7 +184,8 @@ impl<Q: PathQuery> PathCacher<Q> {
None => self.query.root().await?,
};
for parent in parents {
parent_id = match self.cache.get(&parent) {
let parent = normalize_dir_path(&parent);
parent_id = match self.cache.get(parent) {
Some(value) => value,
None => {
let value = match self.query.query(&parent_id, get_basename(&parent)).await? {
Expand All @@ -188,7 +196,7 @@ impl<Q: PathQuery> PathCacher<Q> {
.await?
}
};
self.cache.insert(parent, value.clone());
self.cache.insert(parent.to_owned(), value.clone());
value
}
}
Expand All @@ -198,9 +206,22 @@ impl<Q: PathQuery> PathCacher<Q> {
}
}

/// Normalize the path for dirs
pub fn normalize_dir_path(path: &str) -> &str {
imWildCat marked this conversation as resolved.
Show resolved Hide resolved
if path == "/" {
path
} else if path.ends_with('/') {
path.split_at(path.len() - 1).0
} else {
path
}
}

#[cfg(test)]
mod tests {

use raw::normalize_dir_path;

use crate::raw::PathCacher;
use crate::raw::PathQuery;
use crate::*;
Expand Down Expand Up @@ -241,4 +262,13 @@ mod tests {
assert_eq!(actual.as_deref(), expect, "{}", name)
}
}

#[test]
fn test_normalize_dir_path() {
assert_eq!(normalize_dir_path("/"), "/");
assert_eq!(normalize_dir_path("/a"), "/a");
assert_eq!(normalize_dir_path("/a/"), "/a");
assert_eq!(normalize_dir_path("/a/b"), "/a/b");
assert_eq!(normalize_dir_path("/a/b/"), "/a/b");
}
}
3 changes: 3 additions & 0 deletions core/src/services/gdrive/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,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 because Google Drive API includes "/" in the name of folder
let name = normalize_dir_path(name);

let content = serde_json::to_vec(&json!({
"name": name,
"mimeType": "application/vnd.google-apps.folder",
Expand Down
Loading