opendal/services/dropbox/
lister.rs1use std::sync::Arc;
19
20use bytes::Buf;
21
22use super::core::*;
23use super::error::parse_error;
24use crate::raw::*;
25use crate::*;
26
27pub struct DropboxLister {
28 core: Arc<DropboxCore>,
29 path: String,
30 recursive: bool,
31 limit: Option<usize>,
32}
33
34impl DropboxLister {
35 pub fn new(
36 core: Arc<DropboxCore>,
37 path: String,
38 recursive: bool,
39 limit: Option<usize>,
40 ) -> Self {
41 Self {
42 core,
43 path,
44 recursive,
45 limit,
46 }
47 }
48}
49
50impl oio::PageList for DropboxLister {
51 async fn next_page(&self, ctx: &mut oio::PageContext) -> Result<()> {
52 let response = if !ctx.token.is_empty() {
56 self.core.dropbox_list_continue(&ctx.token).await?
57 } else {
58 self.core
59 .dropbox_list(&self.path, self.recursive, self.limit)
60 .await?
61 };
62
63 let status_code = response.status();
64
65 if !status_code.is_success() {
66 let error = parse_error(response);
67
68 let result = match error.kind() {
69 ErrorKind::NotFound => Ok(()),
70 _ => Err(error),
71 };
72
73 ctx.done = true;
74 return result;
75 }
76
77 let bytes = response.into_body();
78 let decoded_response: DropboxListResponse =
79 serde_json::from_reader(bytes.reader()).map_err(new_json_deserialize_error)?;
80
81 for entry in decoded_response.entries {
82 let entry_mode = match entry.tag.as_str() {
83 "file" => EntryMode::FILE,
84 "folder" => EntryMode::DIR,
85 _ => EntryMode::Unknown,
86 };
87
88 let mut name = entry.name;
89 let mut meta = Metadata::new(entry_mode);
90
91 if entry_mode == EntryMode::DIR && !name.ends_with('/') {
93 name.push('/');
94 }
95
96 if entry_mode == EntryMode::FILE {
98 let date_utc_last_modified = parse_datetime_from_rfc3339(&entry.client_modified)?;
99 meta.set_last_modified(date_utc_last_modified);
100
101 if let Some(size) = entry.size {
102 meta.set_content_length(size);
103 }
104 }
105
106 ctx.entries.push_back(oio::Entry::with(name, meta));
107 }
108
109 if decoded_response.has_more {
110 ctx.token = decoded_response.cursor;
111 ctx.done = false;
112 } else {
113 ctx.done = true;
114 }
115 Ok(())
116 }
117}