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