opendal/services/seafile/lister.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19
20use super::core::SeafileCore;
21use crate::raw::oio::Entry;
22use crate::raw::*;
23use crate::*;
24
25pub struct SeafileLister {
26 core: Arc<SeafileCore>,
27
28 path: String,
29}
30
31impl SeafileLister {
32 pub(super) fn new(core: Arc<SeafileCore>, path: &str) -> Self {
33 SeafileLister {
34 core,
35 path: path.to_string(),
36 }
37 }
38}
39
40impl oio::PageList for SeafileLister {
41 async fn next_page(&self, ctx: &mut oio::PageContext) -> Result<()> {
42 let list_response = self.core.list(&self.path).await?;
43 match list_response.infos {
44 Some(infos) => {
45 // add path itself
46 ctx.entries.push_back(Entry::new(
47 self.path.as_str(),
48 Metadata::new(EntryMode::DIR),
49 ));
50
51 for info in infos {
52 if !info.name.is_empty() {
53 let rel_path = build_rel_path(
54 &self.core.root,
55 &format!("{}{}", list_response.rooted_abs_path, info.name),
56 );
57
58 let entry = if info.type_field == "file" {
59 let meta = Metadata::new(EntryMode::FILE)
60 .with_last_modified(parse_datetime_from_from_timestamp(info.mtime)?)
61 .with_content_length(info.size.unwrap_or(0));
62 Entry::new(&rel_path, meta)
63 } else {
64 let path = format!("{}/", rel_path);
65 Entry::new(&path, Metadata::new(EntryMode::DIR))
66 };
67
68 ctx.entries.push_back(entry);
69 }
70 }
71
72 ctx.done = true;
73
74 Ok(())
75 }
76 // return nothing when not exist
77 None => {
78 ctx.done = true;
79 Ok(())
80 }
81 }
82 }
83}