Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
imWildCat committed Jun 18, 2024
1 parent 24e93ca commit fee42a6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
31 changes: 27 additions & 4 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 @@ -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,
Expand Down Expand Up @@ -457,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

0 comments on commit fee42a6

Please sign in to comment.