Skip to content

Commit 0363f3f

Browse files
authored
Revert "feat(services/gdrive): Implement write returns metadata (#6683)" (#6699)
This reverts commit 48d9f5f.
1 parent 48d9f5f commit 0363f3f

File tree

3 files changed

+48
-89
lines changed

3 files changed

+48
-89
lines changed

core/src/services/gdrive/backend.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,32 @@ impl Access for GdriveBackend {
5353
}
5454

5555
async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> {
56-
let meta = self.core.gdrive_get_metadata(path).await?;
56+
let resp = self.core.gdrive_stat(path).await?;
57+
58+
if resp.status() != StatusCode::OK {
59+
return Err(parse_error(resp));
60+
}
61+
62+
let bs = resp.into_body();
63+
let gdrive_file: GdriveFile =
64+
serde_json::from_reader(bs.reader()).map_err(new_json_deserialize_error)?;
65+
66+
let file_type = if gdrive_file.mime_type == "application/vnd.google-apps.folder" {
67+
EntryMode::DIR
68+
} else {
69+
EntryMode::FILE
70+
};
71+
let mut meta = Metadata::new(file_type).with_content_type(gdrive_file.mime_type);
72+
if let Some(v) = gdrive_file.size {
73+
meta = meta.with_content_length(v.parse::<u64>().map_err(|e| {
74+
Error::new(ErrorKind::Unexpected, "parse content length").set_source(e)
75+
})?);
76+
}
77+
if let Some(v) = gdrive_file.modified_time {
78+
meta = meta.with_last_modified(v.parse::<Timestamp>().map_err(|e| {
79+
Error::new(ErrorKind::Unexpected, "parse last modified time").set_source(e)
80+
})?);
81+
}
5782
Ok(RpStat::new(meta))
5883
}
5984

core/src/services/gdrive/core.rs

Lines changed: 18 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ use super::error::parse_error;
3434
use crate::raw::*;
3535
use crate::*;
3636

37-
// We append this as part of URL to request Google Drive file fields.
38-
// Must be kept in sync with `GdriveFile` struct fields.
39-
// For now, we only load the below fields for a smaller response.
40-
// Read more: https://developers.google.com/workspace/drive/api/guides/fields-parameter
41-
pub(crate) const DRIVE_FILE_FIELDS: &str = "id,name,mimeType,size,modifiedTime,md5Checksum,version";
42-
43-
const GDRIVE_FOLDER_MIME_TYPE: &str = "application/vnd.google-apps.folder";
44-
4537
pub struct GdriveCore {
4638
pub info: Arc<AccessorInfo>,
4739

@@ -69,32 +61,19 @@ impl GdriveCore {
6961
format!("path not found: {path}"),
7062
))?;
7163

64+
// The file metadata in the Google Drive API is very complex.
65+
// For now, we only need the file id, name, mime type and modified time.
7266
let mut req = Request::get(format!(
73-
"https://www.googleapis.com/drive/v3/files/{file_id}?fields={DRIVE_FILE_FIELDS}"
67+
"https://www.googleapis.com/drive/v3/files/{file_id}?fields=id,name,mimeType,size,modifiedTime"
7468
))
75-
.extension(Operation::Stat)
76-
.body(Buffer::new())
77-
.map_err(new_request_build_error)?;
69+
.extension(Operation::Stat)
70+
.body(Buffer::new())
71+
.map_err(new_request_build_error)?;
7872
self.sign(&mut req).await?;
7973

8074
self.info.http_client().send(req).await
8175
}
8276

83-
/// Get metadata for a file from Google Drive.
84-
pub async fn gdrive_get_metadata(&self, path: &str) -> Result<Metadata> {
85-
let resp = self.gdrive_stat(path).await?;
86-
87-
if resp.status() != StatusCode::OK {
88-
return Err(parse_error(resp));
89-
}
90-
91-
let bs = resp.into_body();
92-
let gdrive_file: GdriveFile =
93-
serde_json::from_reader(bs.reader()).map_err(new_json_deserialize_error)?;
94-
95-
gdrive_file.to_metadata()
96-
}
97-
9877
pub async fn gdrive_get(&self, path: &str, range: BytesRange) -> Result<Response<HttpBody>> {
9978
let path = build_abs_path(&self.root, path);
10079
let path_id = self.path_cache.get(&path).await?.ok_or(Error::new(
@@ -202,9 +181,7 @@ impl GdriveCore {
202181
) -> Result<Response<Buffer>> {
203182
let parent = self.path_cache.ensure_dir(get_parent(path)).await?;
204183

205-
let url = format!(
206-
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields={DRIVE_FILE_FIELDS}"
207-
);
184+
let url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart";
208185

209186
let file_name = get_basename(path);
210187

@@ -254,9 +231,8 @@ impl GdriveCore {
254231
size: u64,
255232
body: Buffer,
256233
) -> Result<Response<Buffer>> {
257-
let url = format!(
258-
"https://www.googleapis.com/upload/drive/v3/files/{file_id}?uploadType=media&fields={DRIVE_FILE_FIELDS}"
259-
);
234+
let url =
235+
format!("https://www.googleapis.com/upload/drive/v3/files/{file_id}?uploadType=media");
260236

261237
let mut req = Request::patch(url)
262238
.header(header::CONTENT_TYPE, "application/octet-stream")
@@ -492,61 +468,22 @@ pub struct GdriveTokenResponse {
492468
expires_in: u64,
493469
}
494470

495-
/// File struct for Google Drive API
496-
/// We select a few arbitrary fields.
497-
/// When update fields, keep `DRIVE_FILE_FIELDS` in sync to fetch related data.
498-
/// Read more [here](https://developers.google.com/drive/api/reference/rest/v3/files#File)
471+
/// This is the file struct returned by the Google Drive API.
472+
/// This is a complex struct, but we only add the fields we need.
473+
/// refer to https://developers.google.com/drive/api/reference/rest/v3/files#File
499474
#[derive(Deserialize, Debug)]
500475
#[serde(rename_all = "camelCase")]
501476
pub struct GdriveFile {
502477
pub mime_type: String,
503478
pub id: String,
504479
pub name: String,
505-
// Size may be null for folders or shortcuts
506480
pub size: Option<String>,
507-
pub modified_time: String,
508-
// Only applicable to files with binary content in Google Drive.
509-
pub md5_checksum: Option<String>,
510-
// A short-lived link to the file's version.
511-
pub version: Option<String>,
512-
}
513-
514-
impl GdriveFile {
515-
/// Converts Google Drive file metadata to OpenDAL Metadata.
516-
///
517-
/// This method parses the Google Drive API response fields and maps them
518-
/// to OpenDAL's standard metadata fields.
519-
pub(crate) fn to_metadata(&self) -> Result<Metadata> {
520-
let file_type = if self.mime_type == GDRIVE_FOLDER_MIME_TYPE {
521-
EntryMode::DIR
522-
} else {
523-
EntryMode::FILE
524-
};
525-
let mut metadata = Metadata::new(file_type);
526-
metadata.set_content_type(&self.mime_type);
527-
528-
if let Some(ref size) = self.size {
529-
let content_length = size.parse::<u64>().map_err(|e| {
530-
Error::new(ErrorKind::Unexpected, "parse content length").set_source(e)
531-
})?;
532-
metadata.set_content_length(content_length);
533-
}
534-
535-
let last_modified = self.modified_time.parse::<Timestamp>().map_err(|e| {
536-
Error::new(ErrorKind::Unexpected, "parse last modified time").set_source(e)
537-
})?;
538-
metadata.set_last_modified(last_modified);
539-
540-
if let Some(ref md5_checksum) = self.md5_checksum {
541-
metadata.set_content_md5(md5_checksum);
542-
}
543-
544-
if let Some(ref version) = self.version {
545-
metadata.set_version(version);
546-
}
547-
548-
Ok(metadata)
549-
}
481+
// The modified time is not returned unless the `fields`
482+
// query parameter contains `modifiedTime`.
483+
// As we only need the modified time when we do `stat` operation,
484+
// if other operations(such as search) do not specify the `fields` query parameter,
485+
// try to access this field, it will be `None`.
486+
pub modified_time: Option<String>,
550487
}
551488

552489
/// refer to https://developers.google.com/drive/api/reference/rest/v3/files/list

core/src/services/gdrive/writer.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,14 @@ impl oio::OneShotWrite for GdriveWriter {
6262
let status = resp.status();
6363
match status {
6464
StatusCode::OK | StatusCode::CREATED => {
65-
let bs = resp.into_body();
66-
let file: GdriveFile =
67-
serde_json::from_reader(bs.reader()).map_err(new_json_deserialize_error)?;
68-
6965
// If we don't have the file id before, let's update the cache to avoid re-fetching.
7066
if self.file_id.is_none() {
67+
let bs = resp.into_body();
68+
let file: GdriveFile =
69+
serde_json::from_reader(bs.reader()).map_err(new_json_deserialize_error)?;
7170
self.core.path_cache.insert(&self.path, &file.id).await;
7271
}
73-
74-
let metadata = file.to_metadata()?;
75-
Ok(metadata)
72+
Ok(Metadata::default())
7673
}
7774
_ => Err(parse_error(resp)),
7875
}

0 commit comments

Comments
 (0)