Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(service/gdrive): add gdrive list support #3025

Merged
merged 3 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/src/services/gdrive/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use http::StatusCode;

use super::core::GdriveCore;
use super::error::parse_error;
use super::pager::GdrivePager;
use super::writer::GdriveWriter;
use crate::raw::*;
use crate::services::gdrive::core::GdriveFile;
Expand All @@ -42,7 +43,7 @@ impl Accessor for GdriveBackend {
type BlockingReader = ();
type Writer = GdriveWriter;
type BlockingWriter = ();
type Pager = ();
type Pager = GdrivePager;
type BlockingPager = ();

fn info(&self) -> AccessorInfo {
Expand All @@ -54,6 +55,8 @@ impl Accessor for GdriveBackend {

read: true,

list: true,

write: true,

create_dir: true,
Expand Down Expand Up @@ -244,6 +247,13 @@ impl Accessor for GdriveBackend {
Err(e)
}
}

async fn list(&self, path: &str, _args: OpList) -> Result<(RpList, Self::Pager)> {
Ok((
RpList::default(),
GdrivePager::new(self.core.root.clone(), path.into(), self.core.clone()),
Young-Flash marked this conversation as resolved.
Show resolved Hide resolved
))
}
}

impl GdriveBackend {
Expand Down
40 changes: 39 additions & 1 deletion core/src/services/gdrive/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl GdriveCore {
"name = \"{}\" and \"{}\" in parents and trashed = false",
item, parent_id
);
if i != file_path_items.len() - 1 {
if i != file_path_items.len() - 1 || path.ends_with('/') {
query += " and mimeType = 'application/vnd.google-apps.folder'";
}

Expand Down Expand Up @@ -325,6 +325,43 @@ impl GdriveCore {
self.client.send(req).await
}

pub async fn gdrive_list(
&self,
path: &str,
page_size: i32,
next_page_token: Option<String>,
) -> Result<Response<IncomingAsyncBody>> {
let q = format!(
"'{}' in parents and trashed = false",
self.get_file_id_by_path(path).await?
);

let url = match next_page_token {
Some(page_token) => {
format!(
"https://www.googleapis.com/drive/v3/files?pageSize={}&pageToken={}&q={}",
page_size,
page_token,
percent_encode_path(q.as_str())
)
}
None => {
format!(
"https://www.googleapis.com/drive/v3/files?pageSize={}&q={}",
page_size,
percent_encode_path(q.as_str())
)
}
};

let mut req = Request::get(&url)
.body(AsyncBody::Empty)
.map_err(new_request_build_error)?;
self.sign(&mut req).await?;

self.client.send(req).await
}

// Update with content and metadata
pub async fn gdrive_patch_metadata_request(
&self,
Expand Down Expand Up @@ -556,4 +593,5 @@ pub struct GdriveFile {
#[serde(rename_all = "camelCase")]
pub(crate) struct GdriveFileList {
pub(crate) files: Vec<GdriveFile>,
pub(crate) next_page_token: Option<String>,
}
1 change: 1 addition & 0 deletions core/src/services/gdrive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ mod core;
mod error;

pub use builder::GdriveBuilder as Gdrive;
mod pager;
mod writer;
105 changes: 105 additions & 0 deletions core/src/services/gdrive/pager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use std::sync::Arc;

use crate::{
raw::{
build_rel_path, build_rooted_abs_path, new_json_deserialize_error,
oio::{self},
},
EntryMode, Metadata, Result,
};
use async_trait::async_trait;
use http::StatusCode;

use super::{
core::{GdriveCore, GdriveFileList},
error::parse_error,
};
pub struct GdrivePager {
root: String,
path: String,
core: Arc<GdriveCore>,
next_page_token: Option<String>,
done: bool,
}

impl GdrivePager {
pub fn new(root: String, path: String, core: Arc<GdriveCore>) -> Self {
Self {
root,
path,
core,
next_page_token: None,
done: false,
}
}
}

#[async_trait]
impl oio::Page for GdrivePager {
async fn next(&mut self) -> Result<Option<Vec<oio::Entry>>> {
if self.done {
return Ok(None);
}

let resp = self
.core
.gdrive_list(&self.path, 100, self.next_page_token.clone())
.await?;

match resp.status() {
Young-Flash marked this conversation as resolved.
Show resolved Hide resolved
StatusCode::OK => {
let bytes = resp.into_body().bytes().await?;
let decoded_response = serde_json::from_slice::<GdriveFileList>(&bytes)
.map_err(new_json_deserialize_error)?;

if let Some(next_page_token) = decoded_response.next_page_token {
self.next_page_token = Some(next_page_token);
} else {
self.done = true;
}

let entries: Vec<oio::Entry> = decoded_response
.files
.into_iter()
.map(|mut file| {
let file_type =
if file.mime_type.as_str() == "application/vnd.google-apps.folder" {
file.name = format!("{}/", file.name);
EntryMode::DIR
} else {
EntryMode::FILE
};

let path = format!(
"{}{}",
build_rooted_abs_path(&self.root, &self.path),
file.name
);
let normalized_path = build_rel_path(&self.root, &path);
oio::Entry::new(&normalized_path, Metadata::new(file_type))
})
.collect();

Ok(Some(entries))
}
_ => Err(parse_error(resp).await?),
}
}
}