From 13268cb5f82f85144e0a1762eae8a37dae82adc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90?= <73240868+JustinLiang522@users.noreply.github.com> Date: Thu, 18 Aug 2022 16:06:11 +0800 Subject: [PATCH] fix: fail to delete multiple files if the files are removed from upstream storage (#2317) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug #### What this PR does / why we need it: 修复了在云端删除文件后,在Halo同时删除多个附件失败的bug。 Halo数据库里的附件并不会和云端文件实时同步,如果在云端删除文件多个后又在Halo删除,在删除第一个文件后则会抛出异常导致其它文件无法在halo的数据库中正常删除。这个PR通过删除在删除又拍云和七牛云的附件后判断返回状态和抛出异常的代码来解决这个问题。 #### Which issue(s) this PR fixes: Fixes #2283 #### Special notes for your reviewer: 该bug只在七牛云和又拍云存在,其它云储存没有判断返回状态和抛出异常的代码,因此不存在这个问题。 #### Does this PR introduce a user-facing change? ```release-note NONE ``` --- .../java/run/halo/app/handler/file/QiniuOssFileHandler.java | 4 +++- .../java/run/halo/app/handler/file/UpOssFileHandler.java | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/run/halo/app/handler/file/QiniuOssFileHandler.java b/src/main/java/run/halo/app/handler/file/QiniuOssFileHandler.java index 6e890b9c5..32f465664 100644 --- a/src/main/java/run/halo/app/handler/file/QiniuOssFileHandler.java +++ b/src/main/java/run/halo/app/handler/file/QiniuOssFileHandler.java @@ -190,7 +190,9 @@ public class QiniuOssFileHandler implements FileHandler { } } catch (QiniuException e) { log.error("Qiniu oss error response: [{}]", e.response); - throw new FileOperationException("附件 " + key + " 从七牛云删除失败", e); + if (e.response.statusCode != 612) { + throw new FileOperationException("附件 " + key + " 从七牛云删除失败", e); + } } } diff --git a/src/main/java/run/halo/app/handler/file/UpOssFileHandler.java b/src/main/java/run/halo/app/handler/file/UpOssFileHandler.java index 322b446e9..f07453de6 100644 --- a/src/main/java/run/halo/app/handler/file/UpOssFileHandler.java +++ b/src/main/java/run/halo/app/handler/file/UpOssFileHandler.java @@ -21,6 +21,7 @@ import run.halo.app.model.support.UploadResult; import run.halo.app.repository.AttachmentRepository; import run.halo.app.service.OptionService; import run.halo.app.utils.ImageUtils; +import run.halo.app.utils.JsonUtils; /** * Up oss file handler. @@ -137,7 +138,9 @@ public class UpOssFileHandler implements FileHandler { try { Response result = manager.deleteFile(key, null); - if (!result.isSuccessful()) { + HashMap respondBody = JsonUtils.jsonToObject(result.body().string(), HashMap.class); + if (!result.isSuccessful() + && !(result.code() == 404 && respondBody.get("code").equals(40400001))) { log.warn("附件 " + key + " 从又拍云删除失败"); throw new FileOperationException("附件 " + key + " 从又拍云删除失败"); }