opendal/services/ftp/
lister.rs1use std::str;
19use std::str::FromStr;
20use std::vec::IntoIter;
21
22use suppaftp::list::File;
23
24use crate::raw::*;
25use crate::*;
26
27pub struct FtpLister {
28 path: String,
29 file_iter: IntoIter<String>,
30}
31
32impl FtpLister {
33 pub fn new(path: &str, files: Vec<String>) -> Self {
34 Self {
35 path: path.to_string(),
36 file_iter: files.into_iter(),
37 }
38 }
39}
40
41impl oio::List for FtpLister {
42 async fn next(&mut self) -> Result<Option<oio::Entry>> {
43 let de = match self.file_iter.next() {
44 Some(file_str) => File::from_str(file_str.as_str()).map_err(|e| {
45 Error::new(ErrorKind::Unexpected, "parse file from response").set_source(e)
46 })?,
47 None => return Ok(None),
48 };
49
50 let path = self.path.to_string() + de.name();
51
52 let mut meta = if de.is_file() {
53 Metadata::new(EntryMode::FILE)
54 } else if de.is_directory() {
55 Metadata::new(EntryMode::DIR)
56 } else {
57 Metadata::new(EntryMode::Unknown)
58 };
59 meta.set_content_length(de.size() as u64);
60 meta.set_last_modified(de.modified().into());
61
62 let entry = if de.is_file() {
63 oio::Entry::new(&path, meta)
64 } else if de.is_directory() {
65 oio::Entry::new(&format!("{}/", &path), meta)
66 } else {
67 oio::Entry::new(&path, meta)
68 };
69
70 Ok(Some(entry))
71 }
72}