From 67e42d97530bb185cd8b3e1ac60d675fdf19d88f Mon Sep 17 00:00:00 2001 From: zhaojun1998 Date: Sat, 29 Feb 2020 15:45:11 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=20OneDrive=20?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=96=87=E6=A1=A3=E5=8C=BA=E6=88=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=E5=AF=86=E7=A0=81=E6=97=B6,=20=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E8=B6=85=E6=97=B6=E5=AF=BC=E8=87=B4=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=9A=84=20BUG.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/config/GlobalScheduleTask.java | 43 +++++++++++-------- .../zhaojun/common/service/SystemService.java | 19 +++++++- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/main/java/im/zhaojun/common/config/GlobalScheduleTask.java b/src/main/java/im/zhaojun/common/config/GlobalScheduleTask.java index 8510d6e..deaac8e 100644 --- a/src/main/java/im/zhaojun/common/config/GlobalScheduleTask.java +++ b/src/main/java/im/zhaojun/common/config/GlobalScheduleTask.java @@ -45,26 +45,33 @@ public class GlobalScheduleTask { @Scheduled(fixedRate = 1000 * 60 * 10, initialDelay = 1000 * 30) public void autoRefreshOneDriveToken() { - AbstractFileService currentFileService = systemConfigService.getCurrentFileService(); - - if (!(currentFileService instanceof OneDriveServiceImpl - || currentFileService instanceof OneDriveChinaServiceImpl)) { - log.debug("当前启用存储类型, 不是 OneDrive, 跳过自动刷新 AccessToken"); - return; - } - - if (currentFileService.getIsUnInitialized()) { - log.debug("当前启用 OneDrive 未初始化成功, 跳过自动刷新 AccessToken"); - return; - } - - StorageTypeEnum currentStorageTypeEnum = currentFileService.getStorageTypeEnum(); - try { - refreshOneDriveToken(currentStorageTypeEnum); - } catch (Exception e) { - log.debug("刷新 " + currentStorageTypeEnum.getDescription() + " Token 失败.", e); + log.debug("尝试调用 OneDrive 自动刷新 AccessToken 定时任务"); + + AbstractFileService currentFileService = systemConfigService.getCurrentFileService(); + + if (!(currentFileService instanceof OneDriveServiceImpl + || currentFileService instanceof OneDriveChinaServiceImpl)) { + log.debug("当前启用存储类型, 不是 OneDrive, 跳过自动刷新 AccessToken"); + return; + } + + if (currentFileService.getIsUnInitialized()) { + log.debug("当前启用 OneDrive 未初始化成功, 跳过自动刷新 AccessToken"); + return; + } + + StorageTypeEnum currentStorageTypeEnum = currentFileService.getStorageTypeEnum(); + + try { + refreshOneDriveToken(currentStorageTypeEnum); + } catch (Exception e) { + log.debug("刷新 " + currentStorageTypeEnum.getDescription() + " Token 失败.", e); + } + } catch (Throwable e) { + log.debug("尝试调用 OneDrive 自动刷新 AccessToken 定时任务出现未知异常", e); } + } /** diff --git a/src/main/java/im/zhaojun/common/service/SystemService.java b/src/main/java/im/zhaojun/common/service/SystemService.java index c73a502..89274ac 100644 --- a/src/main/java/im/zhaojun/common/service/SystemService.java +++ b/src/main/java/im/zhaojun/common/service/SystemService.java @@ -5,7 +5,10 @@ import im.zhaojun.common.model.dto.FileItemDTO; import im.zhaojun.common.model.dto.SiteConfigDTO; import im.zhaojun.common.model.enums.StorageTypeEnum; import im.zhaojun.common.util.HttpUtil; +import im.zhaojun.common.util.StringUtils; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.web.client.HttpClientErrorException; import javax.annotation.Resource; import java.util.ArrayList; @@ -15,6 +18,7 @@ import java.util.Objects; /** * @author zhaojun */ +@Slf4j @Service public class SystemService { @@ -40,7 +44,20 @@ public class SystemService { for (FileItemDTO fileItemDTO : fileItemList) { if (ZFileConstant.README_FILE_NAME.equalsIgnoreCase(fileItemDTO.getName())) { - siteConfigDTO.setReadme(HttpUtil.getTextContent(fileItemDTO.getUrl())); + String textContent = null; + try { + textContent = HttpUtil.getTextContent(fileItemDTO.getUrl()); + } catch (HttpClientErrorException httpClientErrorException) { + log.debug("尝试重新获取文档区缓存中链接后仍失败", httpClientErrorException); + try { + String fullPath = StringUtils.removeDuplicateSeparator(fileItemDTO.getPath() + "/" + fileItemDTO.getName()); + FileItemDTO fileItem = fileService.getFileItem(fullPath); + textContent = HttpUtil.getTextContent(fileItem.getUrl()); + } catch (Exception e) { + log.debug("尝试重新获取文档区链接后仍失败, 已置为空", e); + } + } + siteConfigDTO.setReadme(textContent); } } return siteConfigDTO;