dav_server_opendalfs/
metadata.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 dav_server::fs::FsError;
19use dav_server::fs::{DavMetaData, FsResult};
20use opendal::Metadata;
21use std::time::SystemTime;
22
23/// OpendalMetaData is a `DavMetaData` implementation for opendal.
24#[derive(Debug, Clone)]
25pub struct OpendalMetaData {
26    metadata: Metadata,
27}
28
29impl OpendalMetaData {
30    /// Create a new opendal metadata.
31    pub fn new(metadata: Metadata) -> Self {
32        OpendalMetaData { metadata }
33    }
34}
35
36impl DavMetaData for OpendalMetaData {
37    fn len(&self) -> u64 {
38        self.metadata.content_length()
39    }
40
41    fn modified(&self) -> FsResult<SystemTime> {
42        match self.metadata.last_modified() {
43            Some(t) => Ok(t.into()),
44            None => Err(FsError::GeneralFailure),
45        }
46    }
47
48    fn is_dir(&self) -> bool {
49        self.metadata.is_dir()
50    }
51
52    fn etag(&self) -> Option<String> {
53        self.metadata.etag().map(|s| s.to_string())
54    }
55
56    fn is_file(&self) -> bool {
57        self.metadata.is_file()
58    }
59
60    fn status_changed(&self) -> FsResult<SystemTime> {
61        self.metadata
62            .last_modified()
63            .map_or(Err(FsError::GeneralFailure), |t| Ok(t.into()))
64    }
65}