From 839cbb1deec7c8f88f2cc860aea4368daee75143 Mon Sep 17 00:00:00 2001 From: yuchanns Date: Fri, 21 Jun 2024 11:34:41 +0800 Subject: [PATCH] fix(services/aliyun-drive): write op cannot overwrite existing files --- core/src/services/aliyun_drive/backend.rs | 12 ++++++++++++ core/src/services/aliyun_drive/core.rs | 2 ++ 2 files changed, 14 insertions(+) diff --git a/core/src/services/aliyun_drive/backend.rs b/core/src/services/aliyun_drive/backend.rs index a1497462ef87..b0b93f6e8102 100644 --- a/core/src/services/aliyun_drive/backend.rs +++ b/core/src/services/aliyun_drive/backend.rs @@ -240,6 +240,7 @@ impl Builder for AliyunDriveBuilder { sign, })), client, + dir_lock: Arc::new(Mutex::new(())), }), }) } @@ -462,6 +463,17 @@ impl Access for AliyunDriveBackend { let parent_path = get_parent(path); let parent_file_id = self.core.ensure_dir_exists(parent_path).await?; + // write can overwrite + match self.core.get_by_path(path).await { + Err(err) if err.kind() == ErrorKind::NotFound => {} + Err(err) => return Err(err), + Ok(res) => { + let file: AliyunDriveFile = + serde_json::from_reader(res.reader()).map_err(new_json_serialize_error)?; + self.core.delete_path(&file.file_id).await?; + } + }; + let executor = args.executor().cloned(); let writer = diff --git a/core/src/services/aliyun_drive/core.rs b/core/src/services/aliyun_drive/core.rs index 1ec229e6863d..f6397edbdda8 100644 --- a/core/src/services/aliyun_drive/core.rs +++ b/core/src/services/aliyun_drive/core.rs @@ -66,6 +66,7 @@ pub struct AliyunDriveCore { pub signer: Arc>, pub client: HttpClient, + pub dir_lock: Arc>, } impl Debug for AliyunDriveCore { @@ -210,6 +211,7 @@ impl AliyunDriveCore { let paths = file_path.split('/').collect::>(); let mut parent: Option = None; for path in paths { + let _guard = self.dir_lock.lock().await; let res = self .create( parent.as_deref(),