opendal/services/webdav/
writer.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 http::StatusCode;
21
22use super::core::*;
23use super::error::parse_error;
24use crate::raw::*;
25use crate::*;
26
27pub struct WebdavWriter {
28    core: Arc<WebdavCore>,
29
30    op: OpWrite,
31    path: String,
32}
33
34impl WebdavWriter {
35    pub fn new(core: Arc<WebdavCore>, op: OpWrite, path: String) -> Self {
36        WebdavWriter { core, op, path }
37    }
38
39    fn parse_metadata(headers: &http::HeaderMap) -> Result<Metadata> {
40        let mut metadata = Metadata::default();
41
42        if let Some(etag) = parse_etag(headers)? {
43            metadata.set_etag(etag);
44        }
45
46        if let Some(last_modified) = parse_last_modified(headers)? {
47            metadata.set_last_modified(last_modified);
48        }
49
50        Ok(metadata)
51    }
52}
53
54impl oio::OneShotWrite for WebdavWriter {
55    async fn write_once(&self, bs: Buffer) -> Result<Metadata> {
56        let resp = self
57            .core
58            .webdav_put(&self.path, Some(bs.len() as u64), &self.op, bs)
59            .await?;
60
61        let status = resp.status();
62
63        match status {
64            StatusCode::CREATED | StatusCode::OK | StatusCode::NO_CONTENT => {
65                let metadata = WebdavWriter::parse_metadata(resp.headers())?;
66                Ok(metadata)
67            }
68            _ => Err(parse_error(resp)),
69        }
70    }
71}