You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2025/04/27 06:30:35 UTC
(opendal) branch main updated: refactor(services/gdrive): move raw requests to core (#6088)
This is an automated email from the ASF dual-hosted git repository.
xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 49aca7347 refactor(services/gdrive): move raw requests to core (#6088)
49aca7347 is described below
commit 49aca734715bc4acc79ffeaa22fd1b54ff1e2eab
Author: Jorge Hermo <jo...@gmail.com>
AuthorDate: Sun Apr 27 08:30:30 2025 +0200
refactor(services/gdrive): move raw requests to core (#6088)
---
core/src/services/gdrive/backend.rs | 47 +------------------------------------
core/src/services/gdrive/core.rs | 42 +++++++++++++++++++++++++++++++++
2 files changed, 43 insertions(+), 46 deletions(-)
diff --git a/core/src/services/gdrive/backend.rs b/core/src/services/gdrive/backend.rs
index 6eb124f03..88f164bdd 100644
--- a/core/src/services/gdrive/backend.rs
+++ b/core/src/services/gdrive/backend.rs
@@ -19,12 +19,9 @@ use std::fmt::Debug;
use std::sync::Arc;
use bytes::Buf;
-use bytes::Bytes;
use chrono::Utc;
-use http::Request;
use http::Response;
use http::StatusCode;
-use serde_json::json;
use super::core::GdriveCore;
use super::core::GdriveFile;
@@ -132,49 +129,7 @@ impl Access for GdriveBackend {
}
async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
- let from = build_abs_path(&self.core.root, from);
-
- let from_file_id = self.core.path_cache.get(&from).await?.ok_or(Error::new(
- ErrorKind::NotFound,
- "the file to copy does not exist",
- ))?;
-
- let to_name = get_basename(to);
- let to_path = build_abs_path(&self.core.root, to);
- let to_parent_id = self
- .core
- .path_cache
- .ensure_dir(get_parent(&to_path))
- .await?;
-
- // copy will overwrite `to`, delete it if exist
- if let Some(id) = self.core.path_cache.get(&to_path).await? {
- let resp = self.core.gdrive_trash(&id).await?;
- let status = resp.status();
- if status != StatusCode::OK {
- return Err(parse_error(resp));
- }
-
- self.core.path_cache.remove(&to_path).await;
- }
-
- let url = format!(
- "https://www.googleapis.com/drive/v3/files/{}/copy",
- from_file_id
- );
-
- let request_body = &json!({
- "name": to_name,
- "parents": [to_parent_id],
- });
- let body = Buffer::from(Bytes::from(request_body.to_string()));
-
- let mut req = Request::post(&url)
- .body(body)
- .map_err(new_request_build_error)?;
- self.core.sign(&mut req).await?;
-
- let resp = self.core.info.http_client().send(req).await?;
+ let resp = self.core.gdrive_copy(from, to).await?;
match resp.status() {
StatusCode::OK => Ok(RpCopy::default()),
diff --git a/core/src/services/gdrive/core.rs b/core/src/services/gdrive/core.rs
index 09d005c41..c24e14105 100644
--- a/core/src/services/gdrive/core.rs
+++ b/core/src/services/gdrive/core.rs
@@ -254,6 +254,48 @@ impl GdriveCore {
let mut signer = self.signer.lock().await;
signer.sign(req).await
}
+
+ pub async fn gdrive_copy(&self, from: &str, to: &str) -> Result<Response<Buffer>> {
+ let from = build_abs_path(&self.root, from);
+
+ let from_file_id = self.path_cache.get(&from).await?.ok_or(Error::new(
+ ErrorKind::NotFound,
+ "the file to copy does not exist",
+ ))?;
+
+ let to_name = get_basename(to);
+ let to_path = build_abs_path(&self.root, to);
+ let to_parent_id = self.path_cache.ensure_dir(get_parent(&to_path)).await?;
+
+ // copy will overwrite `to`, delete it if exist
+ if let Some(id) = self.path_cache.get(&to_path).await? {
+ let resp = self.gdrive_trash(&id).await?;
+ let status = resp.status();
+ if status != StatusCode::OK {
+ return Err(parse_error(resp));
+ }
+
+ self.path_cache.remove(&to_path).await;
+ }
+
+ let url = format!(
+ "https://www.googleapis.com/drive/v3/files/{}/copy",
+ from_file_id
+ );
+
+ let request_body = &json!({
+ "name": to_name,
+ "parents": [to_parent_id],
+ });
+ let body = Buffer::from(Bytes::from(request_body.to_string()));
+
+ let mut req = Request::post(&url)
+ .body(body)
+ .map_err(new_request_build_error)?;
+ self.sign(&mut req).await?;
+
+ self.info.http_client().send(req).await
+ }
}
#[derive(Clone)]