in core/src/services/gdrive/builder.rs [140:233]
fn build(self) -> Result<impl Access> {
let root = normalize_root(&self.config.root.unwrap_or_default());
debug!("backend use root {}", root);
let info = AccessorInfo::default();
info.set_scheme(Scheme::Gdrive)
.set_root(&root)
.set_native_capability(Capability {
stat: true,
stat_has_content_length: true,
stat_has_content_type: true,
stat_has_last_modified: true,
read: true,
list: true,
list_has_content_type: true,
list_has_content_length: true,
list_has_etag: true,
write: true,
create_dir: true,
delete: true,
rename: true,
copy: true,
shared: true,
..Default::default()
});
// allow deprecated api here for compatibility
#[allow(deprecated)]
if let Some(client) = self.http_client {
info.update_http_client(|_| client);
}
let accessor_info = Arc::new(info);
let mut signer = GdriveSigner::new(accessor_info.clone());
match (self.config.access_token, self.config.refresh_token) {
(Some(access_token), None) => {
signer.access_token = access_token;
// We will never expire user specified access token.
signer.expires_in = DateTime::<Utc>::MAX_UTC;
}
(None, Some(refresh_token)) => {
let client_id = self.config.client_id.ok_or_else(|| {
Error::new(
ErrorKind::ConfigInvalid,
"client_id must be set when refresh_token is set",
)
.with_context("service", Scheme::Gdrive)
})?;
let client_secret = self.config.client_secret.ok_or_else(|| {
Error::new(
ErrorKind::ConfigInvalid,
"client_secret must be set when refresh_token is set",
)
.with_context("service", Scheme::Gdrive)
})?;
signer.refresh_token = refresh_token;
signer.client_id = client_id;
signer.client_secret = client_secret;
}
(Some(_), Some(_)) => {
return Err(Error::new(
ErrorKind::ConfigInvalid,
"access_token and refresh_token cannot be set at the same time",
)
.with_context("service", Scheme::Gdrive))
}
(None, None) => {
return Err(Error::new(
ErrorKind::ConfigInvalid,
"access_token or refresh_token must be set",
)
.with_context("service", Scheme::Gdrive))
}
};
let signer = Arc::new(Mutex::new(signer));
Ok(GdriveBackend {
core: Arc::new(GdriveCore {
info: accessor_info.clone(),
root,
signer: signer.clone(),
path_cache: PathCacher::new(GdrivePathQuery::new(accessor_info, signer))
.with_lock(),
}),
})
}