@@ -34,14 +34,6 @@ use super::error::parse_error;
3434use crate :: raw:: * ;
3535use 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-
4537pub 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" ) ]
501476pub 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
0 commit comments