opendal_core/services/onedrive/
backend.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::Response;
21use http::StatusCode;
22
23use super::core::OneDriveCore;
24use super::deleter::OneDriveDeleter;
25use super::error::parse_error;
26use super::lister::OneDriveLister;
27use super::writer::OneDriveWriter;
28use crate::raw::*;
29use crate::*;
30
31#[derive(Clone, Debug)]
32pub struct OnedriveBackend {
33    pub core: Arc<OneDriveCore>,
34}
35
36impl Access for OnedriveBackend {
37    type Reader = HttpBody;
38    type Writer = oio::OneShotWriter<OneDriveWriter>;
39    type Lister = oio::PageLister<OneDriveLister>;
40    type Deleter = oio::OneShotDeleter<OneDriveDeleter>;
41
42    fn info(&self) -> Arc<AccessorInfo> {
43        self.core.info.clone()
44    }
45
46    async fn create_dir(&self, path: &str, _args: OpCreateDir) -> Result<RpCreateDir> {
47        if path == "/" {
48            // skip, the root path exists in the personal OneDrive.
49            return Ok(RpCreateDir::default());
50        }
51
52        let response = self.core.onedrive_create_dir(path).await?;
53        match response.status() {
54            StatusCode::CREATED | StatusCode::OK => Ok(RpCreateDir::default()),
55            _ => Err(parse_error(response)),
56        }
57    }
58
59    async fn stat(&self, path: &str, args: OpStat) -> Result<RpStat> {
60        let meta = self.core.onedrive_stat(path, args).await?;
61
62        Ok(RpStat::new(meta))
63    }
64
65    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
66        let response = self.core.onedrive_get_content(path, &args).await?;
67        match response.status() {
68            StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
69                Ok((RpRead::default(), response.into_body()))
70            }
71            _ => {
72                let (part, mut body) = response.into_parts();
73                let buf = body.to_buffer().await?;
74                Err(parse_error(Response::from_parts(part, buf)))
75            }
76        }
77    }
78
79    async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
80        Ok((
81            RpWrite::default(),
82            oio::OneShotWriter::new(OneDriveWriter::new(
83                self.core.clone(),
84                args,
85                path.to_string(),
86            )),
87        ))
88    }
89
90    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
91        Ok((
92            RpDelete::default(),
93            oio::OneShotDeleter::new(OneDriveDeleter::new(self.core.clone())),
94        ))
95    }
96
97    async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
98        let monitor_url = self.core.initialize_copy(from, to).await?;
99        self.core.wait_until_complete(monitor_url).await?;
100        Ok(RpCopy::default())
101    }
102
103    async fn rename(&self, from: &str, to: &str, _args: OpRename) -> Result<RpRename> {
104        if from == to {
105            return Ok(RpRename::default());
106        }
107
108        self.core.onedrive_move(from, to).await?;
109
110        Ok(RpRename::default())
111    }
112
113    async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
114        let l = OneDriveLister::new(path.to_string(), self.core.clone(), &args);
115        Ok((RpList::default(), oio::PageLister::new(l)))
116    }
117}