Skip to content

Commit

Permalink
normalize path for gdrive?
Browse files Browse the repository at this point in the history
  • Loading branch information
imWildCat committed Jun 14, 2024
1 parent bbc3d55 commit 1260920
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
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 {
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");
}
}
4 changes: 2 additions & 2 deletions core/src/services/gdrive/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ 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 = name.trim_end_matches("/");
// 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,
Expand Down

0 comments on commit 1260920

Please sign in to comment.