opendal/services/onedrive/
writer.rs1use std::sync::Arc;
19
20use bytes::Buf;
21use bytes::Bytes;
22use http::StatusCode;
23
24use super::core::OneDriveCore;
25use super::error::parse_error;
26use super::graph_model::{OneDriveItem, OneDriveUploadSessionCreationResponseBody};
27use crate::raw::*;
28use crate::*;
29
30pub struct OneDriveWriter {
31 core: Arc<OneDriveCore>,
32 op: OpWrite,
33 path: String,
34}
35
36impl OneDriveWriter {
37 const MAX_SIMPLE_SIZE: usize = 4 * 1024 * 1024; const CHUNK_SIZE_FACTOR: usize = 327_680 * 12; pub fn new(core: Arc<OneDriveCore>, op: OpWrite, path: String) -> Self {
44 OneDriveWriter { core, op, path }
45 }
46}
47
48impl oio::OneShotWrite for OneDriveWriter {
49 async fn write_once(&self, bs: Buffer) -> Result<Metadata> {
50 let size = bs.len();
51
52 let meta = if size <= Self::MAX_SIMPLE_SIZE {
53 self.write_simple(bs).await?
54 } else {
55 self.write_chunked(bs).await?
56 };
57
58 Ok(meta)
59 }
60}
61
62impl OneDriveWriter {
63 async fn write_simple(&self, bs: Buffer) -> Result<Metadata> {
64 let response = self
65 .core
66 .onedrive_upload_simple(&self.path, &self.op, bs)
67 .await?;
68
69 match response.status() {
70 StatusCode::CREATED | StatusCode::OK => {
71 let item: OneDriveItem = serde_json::from_reader(response.into_body().reader())
72 .map_err(new_json_deserialize_error)?;
73
74 let mut meta = Metadata::new(EntryMode::FILE)
75 .with_etag(item.e_tag)
76 .with_content_length(item.size.max(0) as u64);
77
78 let last_modified = item.last_modified_date_time;
79 let date_utc_last_modified = parse_datetime_from_rfc3339(&last_modified)?;
80 meta.set_last_modified(date_utc_last_modified);
81
82 Ok(meta)
83 }
84 _ => Err(parse_error(response)),
85 }
86 }
87
88 pub(crate) async fn write_chunked(&self, bs: Buffer) -> Result<Metadata> {
89 let session_response = self.create_upload_session().await?;
95
96 let mut offset = 0;
97 let total_bytes = bs.to_bytes();
98 let total_len = total_bytes.len();
99 let chunks = total_bytes.chunks(OneDriveWriter::CHUNK_SIZE_FACTOR);
100
101 for chunk in chunks {
102 let mut end = offset + OneDriveWriter::CHUNK_SIZE_FACTOR;
103 if end > total_bytes.len() {
104 end = total_bytes.len();
105 }
106 let chunk_end = end - 1;
107
108 let response = self
109 .core
110 .onedrive_chunked_upload(
111 &session_response.upload_url,
112 &self.op,
113 offset,
114 chunk_end,
115 total_len,
116 Buffer::from(Bytes::copy_from_slice(chunk)),
117 )
118 .await?;
119
120 match response.status() {
121 StatusCode::ACCEPTED | StatusCode::OK => {} StatusCode::CREATED => {
125 let item: OneDriveItem = serde_json::from_reader(response.into_body().reader())
127 .map_err(new_json_deserialize_error)?;
128
129 let mut meta = Metadata::new(EntryMode::FILE)
130 .with_etag(item.e_tag)
131 .with_content_length(item.size.max(0) as u64);
132
133 let last_modified = item.last_modified_date_time;
134 let date_utc_last_modified = parse_datetime_from_rfc3339(&last_modified)?;
135 meta.set_last_modified(date_utc_last_modified);
136 return Ok(meta);
137 }
138 _ => return Err(parse_error(response)),
139 }
140
141 offset += OneDriveWriter::CHUNK_SIZE_FACTOR;
142 }
143
144 debug_assert!(false, "should have returned");
145
146 Ok(Metadata::default()) }
148
149 async fn create_upload_session(&self) -> Result<OneDriveUploadSessionCreationResponseBody> {
150 let response = self
151 .core
152 .onedrive_create_upload_session(&self.path, &self.op)
153 .await?;
154 match response.status() {
155 StatusCode::OK => {
156 let bs = response.into_body();
157 let result: OneDriveUploadSessionCreationResponseBody =
158 serde_json::from_reader(bs.reader()).map_err(new_json_deserialize_error)?;
159 Ok(result)
160 }
161 _ => Err(parse_error(response)),
162 }
163 }
164}