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
242 fn info(&self) -> Arc<AccessorInfo> {
243 self.core.info.clone()
244 }
245
246 async fn create_dir(&self, path: &str, _args: OpCreateDir) -> Result<RpCreateDir> {
247 self.core.ensure_dir_exists(path).await?;
248
249 Ok(RpCreateDir::default())
250 }
251
252 async fn rename(&self, from: &str, to: &str, _args: OpRename) -> Result<RpRename> {
253 if from == to {
254 return Ok(RpRename::default());
255 }
256 let res = self.core.get_by_path(from).await?;
257 let file: AliyunDriveFile =
258 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
259 match self.core.get_by_path(to).await {
261 Err(err) if err.kind() == ErrorKind::NotFound => {}
262 Err(err) => return Err(err),
263 Ok(res) => {
264 let file: AliyunDriveFile =
265 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
266 self.core.delete_path(&file.file_id).await?;
267 }
268 };
269
270 let parent_file_id = self.core.ensure_dir_exists(get_parent(to)).await?;
271 self.core.move_path(&file.file_id, &parent_file_id).await?;
272
273 let from_name = get_basename(from);
274 let to_name = get_basename(to);
275
276 if from_name != to_name {
277 self.core.update_path(&file.file_id, to_name).await?;
278 }
279
280 Ok(RpRename::default())
281 }
282
283 async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
284 if from == to {
285 return Ok(RpCopy::default());
286 }
287 let res = self.core.get_by_path(from).await?;
288 let file: AliyunDriveFile =
289 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
290 match self.core.get_by_path(to).await {
292 Err(err) if err.kind() == ErrorKind::NotFound => {}
293 Err(err) => return Err(err),
294 Ok(res) => {
295 let file: AliyunDriveFile =
296 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
297 self.core.delete_path(&file.file_id).await?;
298 }
299 };
300 let parent_path = get_parent(to);
303 let parent_file_id = self.core.ensure_dir_exists(parent_path).await?;
304
305 let auto_rename = file.parent_file_id == parent_file_id;
309 let res = self
310 .core
311 .copy_path(&file.file_id, &parent_file_id, auto_rename)
312 .await?;
313 let file: CopyResponse =
314 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
315 let file_id = file.file_id;
316
317 let from_name = get_basename(from);
318 let to_name = get_basename(to);
319
320 if from_name != to_name {
321 self.core.update_path(&file_id, to_name).await?;
322 }
323
324 Ok(RpCopy::default())
325 }
326
327 async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> {
328 let res = self.core.get_by_path(path).await?;
329 let file: AliyunDriveFile =
330 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
331
332 if file.path_type == "folder" {
333 let meta = Metadata::new(EntryMode::DIR).with_last_modified(
334 file.updated_at
335 .parse::<chrono::DateTime<Utc>>()
336 .map_err(|e| {
337 Error::new(ErrorKind::Unexpected, "parse last modified time").set_source(e)
338 })?,
339 );
340
341 return Ok(RpStat::new(meta));
342 }
343
344 let mut meta = Metadata::new(EntryMode::FILE).with_last_modified(
345 file.updated_at
346 .parse::<chrono::DateTime<Utc>>()
347 .map_err(|e| {
348 Error::new(ErrorKind::Unexpected, "parse last modified time").set_source(e)
349 })?,
350 );
351 if let Some(v) = file.size {
352 meta = meta.with_content_length(v);
353 }
354 if let Some(v) = file.content_type {
355 meta = meta.with_content_type(v);
356 }
357
358 Ok(RpStat::new(meta))
359 }
360
361 async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
362 let res = self.core.get_by_path(path).await?;
363 let file: AliyunDriveFile =
364 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
365 let resp = self.core.download(&file.file_id, args.range()).await?;
366
367 let status = resp.status();
368 match status {
369 StatusCode::OK | StatusCode::PARTIAL_CONTENT => {
370 Ok((RpRead::default(), resp.into_body()))
371 }
372 _ => {
373 let (part, mut body) = resp.into_parts();
374 let buf = body.to_buffer().await?;
375 Err(parse_error(Response::from_parts(part, buf)))
376 }
377 }
378 }
379
380 async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
381 Ok((
382 RpDelete::default(),
383 oio::OneShotDeleter::new(AliyunDriveDeleter::new(self.core.clone())),
384 ))
385 }
386
387 async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
388 let parent = match self.core.get_by_path(path).await {
389 Err(err) if err.kind() == ErrorKind::NotFound => None,
390 Err(err) => return Err(err),
391 Ok(res) => {
392 let file: AliyunDriveFile =
393 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
394 Some(AliyunDriveParent {
395 file_id: file.file_id,
396 path: path.to_string(),
397 updated_at: file.updated_at,
398 })
399 }
400 };
401
402 let l = AliyunDriveLister::new(self.core.clone(), parent, args.limit());
403
404 Ok((RpList::default(), oio::PageLister::new(l)))
405 }
406
407 async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
408 let parent_path = get_parent(path);
409 let parent_file_id = self.core.ensure_dir_exists(parent_path).await?;
410
411 match self.core.get_by_path(path).await {
413 Err(err) if err.kind() == ErrorKind::NotFound => {}
414 Err(err) => return Err(err),
415 Ok(res) => {
416 let file: AliyunDriveFile =
417 serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?;
418 self.core.delete_path(&file.file_id).await?;
419 }
420 };
421
422 let writer =
423 AliyunDriveWriter::new(self.core.clone(), &parent_file_id, get_basename(path), args);
424
425 Ok((RpWrite::default(), writer))
426 }
427}