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;