opendal/services/aliyun_drive/
backend.rs1use std::fmt::Debug;
19use std::fmt::Formatter;
20use std::sync::Arc;
21
22use bytes::Buf;
23use chrono::Utc;
24use http::Response;
25use http::StatusCode;
26use log::debug;
27use tokio::sync::Mutex;
28
29use super::core::*;
30use super::delete::AliyunDriveDeleter;
31use super::error::parse_error;
32use super::lister::AliyunDriveLister;
33use super::lister::AliyunDriveParent;
34use super::writer::AliyunDriveWriter;
35use crate::raw::*;
36use crate::services::AliyunDriveConfig;
37use crate::*;
38
39impl Configurator for AliyunDriveConfig {
40 type Builder = AliyunDriveBuilder;
41
42 #[allow(deprecated)]
43 fn into_builder(self) -> Self::Builder {
44 AliyunDriveBuilder {
45 config: self,
46 http_client: None,
47 }
48 }
49}
50
51#[doc = include_str!("docs.md")]
52#[derive(Default)]
53pub struct AliyunDriveBuilder {
54 config: AliyunDriveConfig,
55
56 #[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
57 http_client: Option<HttpClient>,
58}
59
60impl Debug for AliyunDriveBuilder {
61 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
62 let mut d = f.debug_struct("AliyunDriveBuilder");
63
64 d.field("config", &self.config);
65 d.finish_non_exhaustive()
66 }
67}
68
69impl AliyunDriveBuilder {
70 pub fn root(mut self, root: &str) -> Self {
74 self.config.root = if root.is_empty() {
75 None
76 } else {
77 Some(root.to_string())
78 };
79
80 self
81 }
82
83 pub fn access_token(mut self, access_token: &str) -> Self {
85 self.config.access_token = Some(access_token.to_string());
86
87 self
88 }
89
90 pub fn client_id(mut self, client_id: &str) -> Self {
92 self.config.client_id = Some(client_id.to_string());
93
94 self
95 }
96
97 pub fn client_secret(mut self, client_secret: &str) -> Self {
99 self.config.client_secret = Some(client_secret.to_string());
100
101 self
102 }
103
104 pub fn refresh_token(mut self, refresh_token: &str) -> Self {
106 self.config.refresh_token = Some(refresh_token.to_string());
107
108 self
109 }
110
111 pub fn drive_type(mut self, drive_type: &str) -> Self {
113 self.config.drive_type = drive_type.to_string();
114
115 self
116 }
117
118 #[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
125 #[allow(deprecated)]
126 pub fn http_client(mut self, client: HttpClient) -> Self {
127 self.http_client = Some(client);
128 self
129 }
130}
131
132impl Builder for AliyunDriveBuilder {
133 const SCHEME: Scheme = Scheme::AliyunDrive;
134 type Config = AliyunDriveConfig;
135
136 fn build(self) -> Result<impl Access> {
137 debug!("backend build started: {:?}", &self);
138
139 let root = normalize_root(&self.config.root.clone().unwrap_or_default());
140 debug!("backend use root {}", &root);
141
142 let sign = match self.config.access_token.clone() {
143 Some(access_token) if !access_token.is_empty() => {
144 AliyunDriveSign::Access(access_token)
145 }
146 _ => match (
147 self.config.client_id.clone(),
148 self.config.client_secret.clone(),
149 self.config.refresh_token.clone(),
150 ) {
151 (Some(client_id), Some(client_secret), Some(refresh_token)) if
152 !client_id.is_empty() && !client_secret.is_empty() && !refresh_token.is_empty() => {
153 AliyunDriveSign::Refresh(client_id, client_secret, refresh_token, None, 0)
154 }
155 _ => return Err(Error::new(
156 ErrorKind::ConfigInvalid,
157 "access_token and a set of client_id, client_secret, and refresh_token are both missing.")
158 .with_operation("Builder::build")
159 .with_context("service", Scheme::AliyunDrive)),
160 },
161 };
162
163 let drive_type = match self.config.drive_type.as_str() {
164 "" | "default" => DriveType::Default,
165 "resource" => DriveType::Resource,
166 "backup" => DriveType::Backup,
167 _ => {
168 return Err(Error::new(
169 ErrorKind::ConfigInvalid,
170 "drive_type is invalid.",
171 ))
172 }
173 };
174 debug!("backend use drive_type {:?}", drive_type);
175
176 Ok(AliyunDriveBackend {
177 core: Arc::new(AliyunDriveCore {
178 info: {
179 let am = AccessorInfo::default();
180 am.set_scheme(Scheme::AliyunDrive)
181 .set_root(&root)
182 .set_native_capability(Capability {
183 stat: true,
184 create_dir: true,
185 read: true,
186 write: true,
187 write_can_multi: true,
188 write_multi_min_size: Some(100 * 1024),
190 write_multi_max_size: if cfg!(target_pointer_width = "64") {
192 Some(5 * 1024 * 1024 * 1024)
193 } else {
194 Some(usize::MAX)
195 },
196 delete: true,
197 copy: true,
198 rename: true,
199 list: true,
200 list_with_limit: true,
201 shared: true,
202 stat_has_content_length: true,
203 stat_has_content_type: true,
204 list_has_last_modified: true,
205 list_has_content_length: true,
206 list_has_content_type: true,
207 ..Default::default()
208 });
209
210 #[allow(deprecated)]
212 if let Some(client) = self.http_client {
213 am.update_http_client(|_| client);
214 }
215
216 am.into()
217 },
218 endpoint: "https://openapi.alipan.com".to_string(),
219 root,
220 drive_type,
221 signer: Arc::new(Mutex::new(AliyunDriveSigner {
222 drive_id: None,
223 sign,
224 })),
225 dir_lock: Arc::new(Mutex::new(())),
226 }),
227 })
228 }
229}
230
231#[derive(Clone, Debug)]
232pub struct AliyunDriveBackend {
233 core: Arc<AliyunDriveCore>,
234}
235
236impl Access for AliyunDriveBackend {
237 type Reader = HttpBody;
238 type Writer = AliyunDriveWriter;
239 type Lister = oio::PageLister<AliyunDriveLister>;
240 type Deleter = oio::OneShotDeleter<AliyunDriveDeleter>;
241 type BlockingReader = ();
242 type BlockingWriter = ();
243 type BlockingLister = ();
244 type BlockingDeleter = ();
245
246 fn info(&self) -> Arc<AccessorInfo> {
247 self.core.info.clone()
248 }
249
250 async fn create_dir(&self, path: &str, _args: OpCreateDir) -> Result<RpCreateDir> {
251 self.core.ensure_dir_exists(path).await?;
252
253 Ok(RpCreateDir::default())
254 }
255
256 async fn rename(&self, from: &str, to: &str, _args: OpRename) -> Result<RpRename> {
257 if from == to {
258 return Ok(RpRename::default());
259 }
260 let res = self.core.get_by_path(from).await?;
261 let file: AliyunDriveFile =
262 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
263 match self.core.get_by_path(to).await {
265 Err(err) if err.kind() == ErrorKind::NotFound => {}
266 Err(err) => return Err(err),
267 Ok(res) => {
268 let file: AliyunDriveFile =
269 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
270 self.core.delete_path(&file.file_id).await?;
271 }
272 };
273
274 let parent_file_id = self.core.ensure_dir_exists(get_parent(to)).await?;
275 self.core.move_path(&file.file_id, &parent_file_id).await?;
276
277 let from_name = get_basename(from);
278 let to_name = get_basename(to);
279
280 if from_name != to_name {
281 self.core.update_path(&file.file_id, to_name).await?;
282 }
283
284 Ok(RpRename::default())
285 }
286
287 async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
288 if from == to {
289 return Ok(RpCopy::default());
290 }
291 let res = self.core.get_by_path(from).await?;
292 let file: AliyunDriveFile =
293 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
294 match self.core.get_by_path(to).await {
296 Err(err) if err.kind() == ErrorKind::NotFound => {}
297 Err(err) => return Err(err),
298 Ok(res) => {
299 let file: AliyunDriveFile =
300 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
301 self.core.delete_path(&file.file_id).await?;
302 }
303 };
304 let parent_path = get_parent(to);
307 let parent_file_id = self.core.ensure_dir_exists(parent_path).await?;
308
309 let auto_rename = file.parent_file_id == parent_file_id;
313 let res = self
314 .core
315 .copy_path(&file.file_id, &parent_file_id, auto_rename)
316 .await?;
317 let file: CopyResponse =
318 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
319 let file_id = file.file_id;
320
321 let from_name = get_basename(from);
322 let to_name = get_basename(to);
323
324 if from_name != to_name {
325 self.core.update_path(&file_id, to_name).await?;
326 }
327
328 Ok(RpCopy::default())
329 }
330
331 async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> {
332 let res = self.core.get_by_path(path).await?;
333 let file: AliyunDriveFile =
334 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
335
336 if file.path_type == "folder" {
337 let meta = Metadata::new(EntryMode::DIR).with_last_modified(
338 file.updated_at
339 .parse::<chrono::DateTime<Utc>>()
340 .map_err(|e| {
341 Error::new(ErrorKind::Unexpected, "parse last modified time").set_source(e)
342 })?,
343 );
344
345 return Ok(RpStat::new(meta));
346 }
347
348 let mut meta = Metadata::new(EntryMode::FILE).with_last_modified(
349 file.updated_at
350 .parse::<chrono::DateTime<Utc>>()
351 .map_err(|e| {
352 Error::new(ErrorKind::Unexpected, "parse last modified time").set_source(e)
353 })?,
354 );
355 if let Some(v) = file.size {
356 meta = meta.with_content_length(v);
357 }
358 if let Some(v) = file.content_type {
359 meta = meta.with_content_type(v);
360 }
361
362 Ok(RpStat::new(meta))
363 }
364
365 async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
366 let res = self.core.get_by_path(path).await?;
367 let file: AliyunDriveFile =
368 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
369 let resp = self.core.download(&file.file_id, args.range()).await?;
370
371 let status = resp.status();
372 match status {
373 StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
374 Ok((RpRead::default(), resp.into_body()))
375 }
376 _ => {
377 let (part, mut body) = resp.into_parts();
378 let buf = body.to_buffer().await?;
379 Err(parse_error(Response::from_parts(part, buf)))
380 }
381 }
382 }
383
384 async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
385 Ok((
386 RpDelete::default(),
387 oio::OneShotDeleter::new(AliyunDriveDeleter::new(self.core.clone())),
388 ))
389 }
390
391 async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
392 let parent = match self.core.get_by_path(path).await {
393 Err(err) if err.kind() == ErrorKind::NotFound => None,
394 Err(err) => return Err(err),
395 Ok(res) => {
396 let file: AliyunDriveFile =
397 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
398 Some(AliyunDriveParent {
399 file_id: file.file_id,
400 path: path.to_string(),
401 updated_at: file.updated_at,
402 })
403 }
404 };
405
406 let l = AliyunDriveLister::new(self.core.clone(), parent, args.limit());
407
408 Ok((RpList::default(), oio::PageLister::new(l)))
409 }
410
411 async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
412 let parent_path = get_parent(path);
413 let parent_file_id = self.core.ensure_dir_exists(parent_path).await?;
414
415 match self.core.get_by_path(path).await {
417 Err(err) if err.kind() == ErrorKind::NotFound => {}
418 Err(err) => return Err(err),
419 Ok(res) => {
420 let file: AliyunDriveFile =
421 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
422 self.core.delete_path(&file.file_id).await?;
423 }
424 };
425
426 let writer =
427 AliyunDriveWriter::new(self.core.clone(), &parent_file_id, get_basename(path), args);
428
429 Ok((RpWrite::default(), writer))
430 }
431}