diff --git a/src/main/java/im/zhaojun/zfile/cache/MyTimedCache.java b/src/main/java/im/zhaojun/zfile/cache/MyTimedCache.java index ee90975..c33fe08 100644 --- a/src/main/java/im/zhaojun/zfile/cache/MyTimedCache.java +++ b/src/main/java/im/zhaojun/zfile/cache/MyTimedCache.java @@ -27,18 +27,29 @@ public class MyTimedCache extends TimedCache { @Override protected void onRemove(K key, V cachedObject) { - log.debug("尝试刷新缓存: " + key); if (driveContext == null) { driveContext = SpringContextHolder.getBean(DriveContext.class); } + DriveCacheKey cacheKey = (DriveCacheKey) key; AbstractBaseFileService baseFileService = driveContext.get(cacheKey.getDriveId()); + + if (log.isDebugEnabled()) { + log.debug("尝试刷新缓存: {}", cacheKey); + } + + if (baseFileService == null) { + log.error("尝试刷新缓存: {}, 时出现异常, 驱动器已不存在", cacheKey); + return; + } + try { baseFileService.fileList(cacheKey.getKey()); } catch (Exception e) { - log.error("尝试刷新驱动器 {} 的 {} 失败, ", cacheKey.getDriveId(), cacheKey.getKey()); + log.error("尝试刷新缓存 {} 失败", cacheKey); e.printStackTrace(); } + } } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/cache/ZFileCache.java b/src/main/java/im/zhaojun/zfile/cache/ZFileCache.java index 173e039..d9cf396 100644 --- a/src/main/java/im/zhaojun/zfile/cache/ZFileCache.java +++ b/src/main/java/im/zhaojun/zfile/cache/ZFileCache.java @@ -7,6 +7,7 @@ import im.zhaojun.zfile.model.dto.FileItemDTO; import im.zhaojun.zfile.model.dto.SystemConfigDTO; import im.zhaojun.zfile.model.entity.DriveConfig; import im.zhaojun.zfile.repository.DriverConfigRepository; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -26,8 +27,12 @@ import java.util.concurrent.ConcurrentMap; * @author zhaojun */ @Component +@Slf4j public class ZFileCache { + @Resource + private DriverConfigRepository driverConfigRepository; + /** * 缓存过期时间 */ @@ -40,7 +45,6 @@ public class ZFileCache { @Value("${zfile.cache.auto-refresh.interval}") private long autoRefreshInterval; - /** * 文件/文件对象缓存. * @@ -58,9 +62,6 @@ public class ZFileCache { */ private SystemConfigDTO systemConfigCache; - @Resource - private DriverConfigRepository driverConfigRepository; - /** * 写入缓存 @@ -102,6 +103,9 @@ public class ZFileCache { * 驱动器 ID */ public void clear(Integer driveId) { + if (log.isDebugEnabled()) { + log.debug("清空驱动器所有缓存, driveId: {}", driveId); + } getCacheByDriveId(driveId).clear(); } @@ -286,12 +290,15 @@ public class ZFileCache { /** - * 开启缓存自动刷新, 仅当数据库设置为开启时, 才会真正开启缓存自动刷新. + * 开启缓存自动刷新 * * @param driveId * 驱动器 ID */ public void startAutoCacheRefresh(Integer driveId) { + if (log.isDebugEnabled()) { + log.debug("开启缓存自动刷新 driveId: {}", driveId); + } DriveConfig driveConfig = driverConfigRepository.findById(driveId).get(); Boolean autoRefreshCache = driveConfig.getAutoRefreshCache(); if (autoRefreshCache != null && autoRefreshCache) { @@ -312,6 +319,9 @@ public class ZFileCache { * 驱动器 ID */ public void stopAutoCacheRefresh(Integer driveId) { + if (log.isDebugEnabled()) { + log.debug("停止缓存自动刷新 driveId: {}", driveId); + } MyTimedCache> driveCache = drivesCache.get(driveId); if (driveCache != null) { driveCache.cancelPruneSchedule(); diff --git a/src/main/java/im/zhaojun/zfile/config/ZFileConfiguration.java b/src/main/java/im/zhaojun/zfile/config/ZFileConfiguration.java index 37d1d8d..ff6c512 100644 --- a/src/main/java/im/zhaojun/zfile/config/ZFileConfiguration.java +++ b/src/main/java/im/zhaojun/zfile/config/ZFileConfiguration.java @@ -31,4 +31,4 @@ public class ZFileConfiguration { return restTemplate; } -} +} \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/context/DriveContext.java b/src/main/java/im/zhaojun/zfile/context/DriveContext.java index b4274bf..8894785 100644 --- a/src/main/java/im/zhaojun/zfile/context/DriveContext.java +++ b/src/main/java/im/zhaojun/zfile/context/DriveContext.java @@ -1,5 +1,6 @@ package im.zhaojun.zfile.context; +import com.alibaba.fastjson.JSON; import im.zhaojun.zfile.exception.InvalidDriveException; import im.zhaojun.zfile.model.entity.DriveConfig; import im.zhaojun.zfile.model.enums.StorageTypeEnum; @@ -40,7 +41,7 @@ public class DriveContext implements ApplicationContextAware { /** - * 项目启动时, 自动调用所有驱动器进行初始化. + * 项目启动时, 自动调用数据库已存储的所有驱动器进行初始化. */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { @@ -48,8 +49,10 @@ public class DriveContext implements ApplicationContextAware { for (DriveConfig driveConfig : list) { try { init(driveConfig.getId()); + log.info("启动时初始化驱动器成功, 驱动器信息: {}", JSON.toJSONString(driveConfig)); + throw new RuntimeException("xx"); } catch (Exception e) { - log.debug(driveConfig.getName() + " 初始化异常, 已跳过", e); + log.error("启动时初始化驱动器失败, 驱动器信息: {}", JSON.toJSONString(driveConfig), e); } } } @@ -64,7 +67,13 @@ public class DriveContext implements ApplicationContextAware { public void init(Integer driveId) { AbstractBaseFileService baseFileService = getBeanByDriveId(driveId); if (baseFileService != null) { + if (log.isDebugEnabled()) { + log.debug("尝试初始化驱动器, driveId: {}", driveId); + } baseFileService.init(driveId); + if (log.isDebugEnabled()) { + log.debug("初始化驱动器成功, driveId: {}", driveId); + } drivesServiceMap.put(driveId, baseFileService); } } @@ -94,6 +103,9 @@ public class DriveContext implements ApplicationContextAware { * 驱动器 ID */ public void destroy(Integer driveId) { + if (log.isDebugEnabled()) { + log.debug("清理驱动器上下文对象, driveId: {}", driveId); + } drivesServiceMap.remove(driveId); } diff --git a/src/main/java/im/zhaojun/zfile/controller/admin/AdminController.java b/src/main/java/im/zhaojun/zfile/controller/admin/AdminController.java index fd36467..1ecc0ab 100644 --- a/src/main/java/im/zhaojun/zfile/controller/admin/AdminController.java +++ b/src/main/java/im/zhaojun/zfile/controller/admin/AdminController.java @@ -3,7 +3,6 @@ package im.zhaojun.zfile.controller.admin; import im.zhaojun.zfile.model.dto.SystemConfigDTO; import im.zhaojun.zfile.model.support.ResultBean; import im.zhaojun.zfile.service.SystemConfigService; -import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -17,7 +16,6 @@ import javax.annotation.Resource; */ @RestController @RequestMapping("/admin") -@Slf4j public class AdminController { @Resource diff --git a/src/main/java/im/zhaojun/zfile/controller/admin/DriveController.java b/src/main/java/im/zhaojun/zfile/controller/admin/DriveController.java index 619e522..7f9bdd4 100644 --- a/src/main/java/im/zhaojun/zfile/controller/admin/DriveController.java +++ b/src/main/java/im/zhaojun/zfile/controller/admin/DriveController.java @@ -4,7 +4,6 @@ import im.zhaojun.zfile.model.dto.DriveConfigDTO; import im.zhaojun.zfile.model.entity.DriveConfig; import im.zhaojun.zfile.model.support.ResultBean; import im.zhaojun.zfile.service.DriveConfigService; -import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -22,7 +21,6 @@ import java.util.List; */ @RestController @RequestMapping("/admin") -@Slf4j public class DriveController { @Resource diff --git a/src/main/java/im/zhaojun/zfile/controller/admin/LogController.java b/src/main/java/im/zhaojun/zfile/controller/admin/LogController.java index d5e2032..ba716ea 100644 --- a/src/main/java/im/zhaojun/zfile/controller/admin/LogController.java +++ b/src/main/java/im/zhaojun/zfile/controller/admin/LogController.java @@ -27,6 +27,9 @@ public class LogController { */ @GetMapping("/log") public ResponseEntity downloadLog(HttpServletResponse response) { + if (log.isDebugEnabled()) { + log.debug("下载诊断日志"); + } String userHome = System.getProperty("user.home"); File fileZip = ZipUtil.zip(userHome + "/.zfile/logs"); String currentDate = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"); diff --git a/src/main/java/im/zhaojun/zfile/controller/admin/MateDataController.java b/src/main/java/im/zhaojun/zfile/controller/admin/MateDataController.java index 1bef85c..e3e065f 100644 --- a/src/main/java/im/zhaojun/zfile/controller/admin/MateDataController.java +++ b/src/main/java/im/zhaojun/zfile/controller/admin/MateDataController.java @@ -5,7 +5,6 @@ import im.zhaojun.zfile.model.entity.StorageConfig; import im.zhaojun.zfile.model.enums.StorageTypeEnum; import im.zhaojun.zfile.model.support.ResultBean; import im.zhaojun.zfile.service.base.AbstractBaseFileService; -import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -18,7 +17,6 @@ import java.util.List; */ @RestController @RequestMapping("/admin") -@Slf4j public class MateDataController { /** diff --git a/src/main/java/im/zhaojun/zfile/controller/admin/MonitorController.java b/src/main/java/im/zhaojun/zfile/controller/admin/MonitorController.java index 0a9c5a4..a188736 100644 --- a/src/main/java/im/zhaojun/zfile/controller/admin/MonitorController.java +++ b/src/main/java/im/zhaojun/zfile/controller/admin/MonitorController.java @@ -2,7 +2,6 @@ package im.zhaojun.zfile.controller.admin; import im.zhaojun.zfile.model.support.ResultBean; import im.zhaojun.zfile.model.support.SystemMonitorInfo; -import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -13,7 +12,6 @@ import org.springframework.web.bind.annotation.RestController; */ @RestController @RequestMapping("/admin") -@Slf4j public class MonitorController { diff --git a/src/main/java/im/zhaojun/zfile/controller/home/FileController.java b/src/main/java/im/zhaojun/zfile/controller/home/FileController.java index fbafa42..f1644a1 100644 --- a/src/main/java/im/zhaojun/zfile/controller/home/FileController.java +++ b/src/main/java/im/zhaojun/zfile/controller/home/FileController.java @@ -1,5 +1,6 @@ package im.zhaojun.zfile.controller.home; +import com.alibaba.fastjson.JSON; import im.zhaojun.zfile.context.DriveContext; import im.zhaojun.zfile.model.constant.ZFileConstant; import im.zhaojun.zfile.model.dto.FileItemDTO; @@ -84,7 +85,8 @@ public class FileController { @RequestParam(required = false) String password, @RequestParam(defaultValue = "1") Integer page) throws Exception { AbstractBaseFileService fileService = driveContext.get(driveId); - List fileItemList = fileService.fileList(StringUtils.removeDuplicateSeparator(ZFileConstant.PATH_SEPARATOR + path + ZFileConstant.PATH_SEPARATOR)); + List fileItemList = + fileService.fileList(StringUtils.removeDuplicateSeparator(ZFileConstant.PATH_SEPARATOR + path + ZFileConstant.PATH_SEPARATOR)); for (FileItemDTO fileItemDTO : fileItemList) { if (ZFileConstant.PASSWORD_FILE_NAME.equals(fileItemDTO.getName())) { @@ -92,13 +94,14 @@ public class FileController { try { expectedPasswordContent = HttpUtil.getTextContent(fileItemDTO.getUrl()); } catch (HttpClientErrorException httpClientErrorException) { - log.debug("尝试重新获取密码文件缓存中链接后仍失败", httpClientErrorException); + log.error("尝试重新获取密码文件缓存中链接后仍失败, driveId: {}, path: {}, inputPassword: {}, passwordFile:{} ", + driveId, path, password, JSON.toJSONString(fileItemDTO), httpClientErrorException); try { String fullPath = StringUtils.removeDuplicateSeparator(fileItemDTO.getPath() + ZFileConstant.PATH_SEPARATOR + fileItemDTO.getName()); FileItemDTO fileItem = fileService.getFileItem(fullPath); expectedPasswordContent = HttpUtil.getTextContent(fileItem.getUrl()); } catch (Exception e) { - log.debug("尝试重新获取密码文件链接后仍失败, 已暂时取消密码", e); + log.error("尝试重新获取密码文件链接后仍失败, 已暂时取消密码", e); break; } } @@ -130,12 +133,13 @@ public class FileController { AbstractBaseFileService fileService = driveContext.get(driveId); String fullPath = StringUtils.removeDuplicateSeparator(path + ZFileConstant.PATH_SEPARATOR + ZFileConstant.README_FILE_NAME); + FileItemDTO fileItem = null; try { - FileItemDTO fileItem = fileService.getFileItem(fullPath); + fileItem = fileService.getFileItem(fullPath); String readme = HttpUtil.getTextContent(fileItem.getUrl()); systemConfig.setReadme(readme); } catch (Exception e) { - // ignore + log.error("获取 README 文件异常, fullPath: {}, fileItem: {}", fullPath, JSON.toJSONString(fileItem), e); } return ResultBean.successData(systemConfig); diff --git a/src/main/java/im/zhaojun/zfile/controller/home/LocalController.java b/src/main/java/im/zhaojun/zfile/controller/home/LocalController.java index faed5c5..3768dba 100644 --- a/src/main/java/im/zhaojun/zfile/controller/home/LocalController.java +++ b/src/main/java/im/zhaojun/zfile/controller/home/LocalController.java @@ -38,8 +38,7 @@ public class LocalController { @GetMapping("/file/{driveId}/**") @ResponseBody public ResponseEntity downAttachment(@PathVariable("driveId") Integer driveId, final HttpServletRequest request) { - String path = (String) request.getAttribute( - HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); + String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); AntPathMatcher apm = new AntPathMatcher(); String filePath = apm.extractPathWithinPattern(bestMatchPattern, path); diff --git a/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java b/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java index 91a52da..1c1a951 100644 --- a/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java +++ b/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java @@ -20,17 +20,6 @@ public class GlobleExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobleExceptionHandler.class); - @ExceptionHandler - @ResponseBody - @ResponseStatus - public ResultBean searchDisableExceptionHandler(StorageStrategyUninitializedException e) { - if (log.isDebugEnabled()) { - log.debug(e.getMessage(), e); - } - return ResultBean.error(e.getMessage()); - } - - /** * 不存在的文件异常 */ @@ -76,15 +65,11 @@ public class GlobleExceptionHandler { } - - @ExceptionHandler @ResponseBody @ResponseStatus(code= HttpStatus.INTERNAL_SERVER_ERROR) public ResultBean searchDisableExceptionHandler(Exception e) { - if (log.isDebugEnabled()) { - log.debug(e.getMessage(), e); - } + log.error(e.getMessage(), e); if (e.getClass() == Exception.class) { return ResultBean.error("系统异常, 请联系管理员"); diff --git a/src/main/java/im/zhaojun/zfile/exception/RefreshCacheException.java b/src/main/java/im/zhaojun/zfile/exception/RefreshCacheException.java new file mode 100644 index 0000000..17150e2 --- /dev/null +++ b/src/main/java/im/zhaojun/zfile/exception/RefreshCacheException.java @@ -0,0 +1,29 @@ +package im.zhaojun.zfile.exception; + +/** + * 刷新缓存时出现的异常信息 + * + * @author zhaojun + */ +public class RefreshCacheException extends RuntimeException { + + public RefreshCacheException() { + } + + public RefreshCacheException(String message) { + super(message); + } + + public RefreshCacheException(String message, Throwable cause) { + super(message, cause); + } + + public RefreshCacheException(Throwable cause) { + super(cause); + } + + public RefreshCacheException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/exception/StorageStrategyUninitializedException.java b/src/main/java/im/zhaojun/zfile/exception/StorageStrategyUninitializedException.java index d08c1d8..55eb89b 100644 --- a/src/main/java/im/zhaojun/zfile/exception/StorageStrategyUninitializedException.java +++ b/src/main/java/im/zhaojun/zfile/exception/StorageStrategyUninitializedException.java @@ -26,4 +26,5 @@ public class StorageStrategyUninitializedException extends RuntimeException { public StorageStrategyUninitializedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } -} + +} \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/schedule/OneDriveTokenRefreshSchedule.java b/src/main/java/im/zhaojun/zfile/schedule/OneDriveTokenRefreshSchedule.java index b1a1272..34d814c 100644 --- a/src/main/java/im/zhaojun/zfile/schedule/OneDriveTokenRefreshSchedule.java +++ b/src/main/java/im/zhaojun/zfile/schedule/OneDriveTokenRefreshSchedule.java @@ -1,5 +1,6 @@ package im.zhaojun.zfile.schedule; +import com.alibaba.fastjson.JSON; import im.zhaojun.zfile.context.DriveContext; import im.zhaojun.zfile.model.entity.DriveConfig; import im.zhaojun.zfile.model.enums.StorageTypeEnum; @@ -11,7 +12,6 @@ import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import javax.annotation.Resource; -import java.time.LocalDateTime; import java.util.List; /** @@ -48,15 +48,15 @@ public class OneDriveTokenRefreshSchedule { try { AbstractOneDriveServiceBase driveService = (AbstractOneDriveServiceBase) driveContext.get(driveConfig.getId()); driveService.refreshOneDriveToken(); - log.info("刷新驱动器 {}, {} key 时间: {}", name, storageType.getDescription(), LocalDateTime.now()); + log.info("尝试刷新 OneDrive Token, DriveInfo: {}", JSON.toJSONString(driveConfig)); } catch (Exception e) { - log.debug("刷新驱动器 " + name + " Token 失败.", e); + log.error("刷新 OneDrive Token 失败, DriveInfo: {}", JSON.toJSONString(driveConfig), e); } }); } catch (Throwable e) { - log.debug("尝试调用 OneDrive 自动刷新 AccessToken 定时任务出现未知异常", e); + log.error("尝试调用 OneDrive 自动刷新 AccessToken 定时任务出现未知异常", e); } } diff --git a/src/main/java/im/zhaojun/zfile/service/DriveConfigService.java b/src/main/java/im/zhaojun/zfile/service/DriveConfigService.java index 8d2865c..78c2b47 100644 --- a/src/main/java/im/zhaojun/zfile/service/DriveConfigService.java +++ b/src/main/java/im/zhaojun/zfile/service/DriveConfigService.java @@ -101,9 +101,7 @@ public class DriveConfigService { declaredField.set(storageStrategyConfig, value); } } catch (NoSuchFieldException | IllegalAccessException e) { - if (log.isDebugEnabled()) { - log.debug("通过反射, 将字段 {" + key + "}注入 DriveConfigDTO 时出现异常:", e); - } + log.error("通过反射, 将字段 {} 注入 DriveConfigDTO 时出现异常:", key, e); } } @@ -142,12 +140,6 @@ public class DriveConfigService { BeanUtils.copyProperties(driveConfigDTO, driveConfig); driverConfigRepository.save(driveConfig); - if (driveConfig.getAutoRefreshCache()) { - startAutoCacheRefresh(driveConfig.getId()); - } else { - stopAutoCacheRefresh(driveConfig.getId()); - } - // 保存存储策略设置. StorageStrategyConfig storageStrategyConfig = driveConfigDTO.getStorageStrategyConfig(); @@ -172,9 +164,7 @@ public class DriveConfigService { storageConfig.setType(storageType); storageConfig.setDriveId(driveConfig.getId()); } catch (IllegalAccessException | NoSuchFieldException e) { - if (log.isDebugEnabled()) { - log.debug("通过反射, 从 StorageStrategyConfig 中获取字段 {" + key + "} 时出现异常:", e); - } + log.error("通过反射, 从 StorageStrategyConfig 中获取字段 {} 时出现异常:", key, e); } } @@ -186,6 +176,13 @@ public class DriveConfigService { if (driveService.getIsUnInitialized()) { throw new InitializeDriveException("初始化异常, 请检查配置是否正确."); } + + if (driveConfig.getAutoRefreshCache()) { + startAutoCacheRefresh(driveConfig.getId()); + } else if (updateFlag){ + stopAutoCacheRefresh(driveConfig.getId()); + } + } @@ -197,9 +194,20 @@ public class DriveConfigService { */ @Transactional(rollbackFor = Exception.class) public void deleteById(Integer id) { + if (log.isDebugEnabled()) { + log.debug("尝试删除驱动器, driveId: {}", id); + } + DriveConfig driveConfig = driverConfigRepository.getOne(id); driverConfigRepository.deleteById(id); storageConfigRepository.deleteByDriveId(id); + if (driveConfig.getEnableCache()) { + zFileCache.stopAutoCacheRefresh(id); + zFileCache.clear(id); + } driveContext.destroy(id); + if (log.isDebugEnabled()) { + log.debug("尝试删除驱动器成功, 已清理相关数据, driveId: {}", id); + } } @@ -267,7 +275,6 @@ public class DriveConfigService { } - /** * 刷新指定 key 的缓存: * 1. 清空此 key 的缓存. @@ -280,15 +287,17 @@ public class DriveConfigService { * 缓存 key (文件夹名称) */ public void refreshCache(Integer driveId, String key) throws Exception { + if (log.isDebugEnabled()) { + log.debug("手动刷新缓存 driveId: {}, key: {}", driveId, key); + } zFileCache.remove(driveId, key); AbstractBaseFileService baseFileService = driveContext.get(driveId); baseFileService.fileList(key); } - /** - * 开启缓存自动刷新, 仅当数据库设置为开启时, 才会真正开启缓存自动刷新. + * 开启缓存自动刷新 * * @param driveId * 驱动器 ID @@ -324,5 +333,4 @@ public class DriveConfigService { zFileCache.clear(driveId); } - } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/SystemConfigService.java b/src/main/java/im/zhaojun/zfile/service/SystemConfigService.java index f66109a..23ee04a 100644 --- a/src/main/java/im/zhaojun/zfile/service/SystemConfigService.java +++ b/src/main/java/im/zhaojun/zfile/service/SystemConfigService.java @@ -61,9 +61,7 @@ public class SystemConfigService { Object convertVal = Convert.convert(field.getType(), strVal); field.set(systemConfigDTO, convertVal); } catch (NoSuchFieldException | IllegalAccessException e) { - if (log.isDebugEnabled()) { - log.debug("通过反射, 将字段 {" + key + "}注入 SystemConfigDTO 时出现异常:", e); - } + log.error("通过反射, 将字段 {} 注入 SystemConfigDTO 时出现异常:", key, e); } } @@ -93,9 +91,7 @@ public class SystemConfigService { try { val = field.get(systemConfigDTO); } catch (IllegalAccessException e) { - if (log.isDebugEnabled()) { - log.debug("通过反射, 从 SystemConfigDTO 获取字段 {" + key + "} 时出现异常:", e); - } + log.error("通过反射, 从 SystemConfigDTO 获取字段 {} 时出现异常:", key, e); } if (val != null) { diff --git a/src/main/java/im/zhaojun/zfile/service/SystemService.java b/src/main/java/im/zhaojun/zfile/service/SystemService.java deleted file mode 100644 index fa23a07..0000000 --- a/src/main/java/im/zhaojun/zfile/service/SystemService.java +++ /dev/null @@ -1,69 +0,0 @@ -// package im.zhaojun.zfile.service; -// -// import im.zhaojun.zfile.model.constant.ZFileConstant; -// import im.zhaojun.zfile.model.dto.FileItemDTO; -// import im.zhaojun.zfile.model.dto.SiteConfigDTO; -// import im.zhaojun.zfile.model.enums.StorageTypeEnum; -// import im.zhaojun.zfile.service.base.AbstractBaseFileService; -// import im.zhaojun.zfile.context.DriveContext; -// import im.zhaojun.zfile.util.HttpUtil; -// import im.zhaojun.zfile.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; -// import java.util.List; -// import java.util.Objects; -// -// /** -// * @author zhaojun -// */ -// @Slf4j -// @Service -// public class SystemService { -// -// @Resource -// private DriveContext driveContext; -// -// /** -// * 构建指定路径下标题, 页面文档信息 -// * @param path 路径 -// */ -// public SiteConfigDTO getConfig(Integer driveId, String path) throws Exception { -// -// SiteConfigDTO siteConfigDTO = new SiteConfigDTO(); -// -// AbstractBaseFileService fileService = driveContext.getDriveService(driveId); -// -// List fileItemList; -// -// if (Objects.equals(fileService.getStorageTypeEnum(), StorageTypeEnum.FTP)) { -// fileItemList = new ArrayList<>(); -// } else { -// fileItemList = fileService.fileList(path); -// } -// -// for (FileItemDTO fileItemDTO : fileItemList) { -// if (ZFileConstant.README_FILE_NAME.equalsIgnoreCase(fileItemDTO.getName())) { -// 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; -// } -// -// } diff --git a/src/main/java/im/zhaojun/zfile/service/base/AbstractOneDriveServiceBase.java b/src/main/java/im/zhaojun/zfile/service/base/AbstractOneDriveServiceBase.java index 8ab17c1..c39488c 100644 --- a/src/main/java/im/zhaojun/zfile/service/base/AbstractOneDriveServiceBase.java +++ b/src/main/java/im/zhaojun/zfile/service/base/AbstractOneDriveServiceBase.java @@ -135,7 +135,7 @@ public abstract class AbstractOneDriveServiceBase extends AbstractBaseFileServic try { root = oneDriveRestTemplate.exchange(requestUrl, HttpMethod.GET, entity, JSONObject.class, getGraphEndPoint(), fullPath).getBody(); } catch (HttpClientErrorException e) { - log.debug("调用 OneDrive 时出现了网络异常: {} , 已尝试重新刷新 token 后再试.", e.getMessage()); + log.debug("调用 OneDrive 时出现了网络异常, 已尝试重新刷新 token 后再试.", e); refreshOneDriveToken(); root = oneDriveRestTemplate.exchange(requestUrl, HttpMethod.GET, entity, JSONObject.class, getGraphEndPoint(), fullPath).getBody(); } diff --git a/src/main/java/im/zhaojun/zfile/service/base/AbstractS3BaseFileService.java b/src/main/java/im/zhaojun/zfile/service/base/AbstractS3BaseFileService.java index 69bcbd0..88dd0ac 100644 --- a/src/main/java/im/zhaojun/zfile/service/base/AbstractS3BaseFileService.java +++ b/src/main/java/im/zhaojun/zfile/service/base/AbstractS3BaseFileService.java @@ -44,12 +44,14 @@ public abstract class AbstractS3BaseFileService extends AbstractBaseFileService return s3FileList(path); } + @Override public String getDownloadUrl(String path) { this.path = path; return s3ObjectUrl(path); } + /** * 获取 S3 指定目录下的对象列表 * @param path 路径 @@ -89,6 +91,7 @@ public abstract class AbstractS3BaseFileService extends AbstractBaseFileService return fileItemList; } + /** * 获取对象的访问链接, 如果指定了域名, 则替换为自定义域名. * @return S3 对象访问地址 @@ -112,6 +115,7 @@ public abstract class AbstractS3BaseFileService extends AbstractBaseFileService return URLUtil.decode(defaultUrl); } + @Override public FileItemDTO getFileItem(String path) { List list; @@ -131,4 +135,5 @@ public abstract class AbstractS3BaseFileService extends AbstractBaseFileService throw new NotExistFileException(); } + } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/base/BaseFileService.java b/src/main/java/im/zhaojun/zfile/service/base/BaseFileService.java index 1f9b4dd..b623f6b 100644 --- a/src/main/java/im/zhaojun/zfile/service/base/BaseFileService.java +++ b/src/main/java/im/zhaojun/zfile/service/base/BaseFileService.java @@ -17,6 +17,7 @@ public interface BaseFileService { */ List fileList(String path) throws Exception; + /** * 获取文件下载地址 * @param path 文件路径 @@ -24,4 +25,4 @@ public interface BaseFileService { */ String getDownloadUrl(String path); -} +} \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/impl/AliyunServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/AliyunServiceImpl.java index 08754ca..de0cee0 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/AliyunServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/AliyunServiceImpl.java @@ -54,6 +54,7 @@ public class AliyunServiceImpl extends AbstractS3BaseFileService implements Base .withCredentials(new AWSStaticCredentialsProvider(credentials)) .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "oss")).build(); testConnection(); + isInitialized = true; } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/FtpServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/FtpServiceImpl.java index 301a192..b31a437 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/FtpServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/FtpServiceImpl.java @@ -68,6 +68,7 @@ public class FtpServiceImpl extends AbstractBaseFileService implements BaseFileS ftp.getClient().configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX)); ftp.getClient().type(FTP.BINARY_FILE_TYPE); testConnection(); + isInitialized = true; } } @@ -140,4 +141,5 @@ public class FtpServiceImpl extends AbstractBaseFileService implements BaseFileS add(new StorageConfig("basePath", "基路径")); }}; } + } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/impl/HuaweiServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/HuaweiServiceImpl.java index 6ece436..bda8069 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/HuaweiServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/HuaweiServiceImpl.java @@ -54,6 +54,7 @@ public class HuaweiServiceImpl extends AbstractS3BaseFileService implements Base .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "obs")).build(); testConnection(); + isInitialized = true; } } @@ -75,5 +76,4 @@ public class HuaweiServiceImpl extends AbstractS3BaseFileService implements Base }}; } - } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/impl/LocalServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/LocalServiceImpl.java index 451c0e2..344019d 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/LocalServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/LocalServiceImpl.java @@ -64,6 +64,7 @@ public class LocalServiceImpl extends AbstractBaseFileService implements BaseFil throw new InitializeDriveException("文件路径: \"" + file.getAbsolutePath() + "\"不存在, 请检查是否填写正确."); } else { testConnection(); + isInitialized = true; } } @@ -149,4 +150,5 @@ public class LocalServiceImpl extends AbstractBaseFileService implements BaseFil add(new StorageConfig("filePath", "文件路径")); }}; } + } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/impl/MinIOServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/MinIOServiceImpl.java index 47c361f..a4efbec 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/MinIOServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/MinIOServiceImpl.java @@ -53,6 +53,7 @@ public class MinIOServiceImpl extends AbstractS3BaseFileService implements BaseF .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "minio")).build(); testConnection(); + isInitialized = true; } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/OneDriveChinaServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/OneDriveChinaServiceImpl.java index db078b6..0edf2ad 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/OneDriveChinaServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/OneDriveChinaServiceImpl.java @@ -54,6 +54,7 @@ public class OneDriveChinaServiceImpl extends AbstractOneDriveServiceBase implem } else { refreshOneDriveToken(); testConnection(); + isInitialized = true; } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/OneDriveServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/OneDriveServiceImpl.java index 37a079e..85db90e 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/OneDriveServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/OneDriveServiceImpl.java @@ -54,6 +54,7 @@ public class OneDriveServiceImpl extends AbstractOneDriveServiceBase implements } else { refreshOneDriveToken(); testConnection(); + isInitialized = true; } } @@ -96,4 +97,5 @@ public class OneDriveServiceImpl extends AbstractOneDriveServiceBase implements public String getScope() { return scope; } + } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/impl/QiniuServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/QiniuServiceImpl.java index b832efa..3a19b0b 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/QiniuServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/QiniuServiceImpl.java @@ -54,6 +54,7 @@ public class QiniuServiceImpl extends AbstractS3BaseFileService implements BaseF .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "kodo")).build(); testConnection(); + isInitialized = true; } } diff --git a/src/main/java/im/zhaojun/zfile/service/impl/S3ServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/S3ServiceImpl.java index 90b62b7..739ec59 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/S3ServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/S3ServiceImpl.java @@ -59,6 +59,7 @@ public class S3ServiceImpl extends AbstractS3BaseFileService implements BaseFile .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "")).build(); testConnection(); + isInitialized = true; } } @@ -80,4 +81,5 @@ public class S3ServiceImpl extends AbstractS3BaseFileService implements BaseFile add(new StorageConfig("isPrivate", "是否是私有空间")); }}; } + } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/impl/TencentServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/TencentServiceImpl.java index 8e12c6e..972fc69 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/TencentServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/TencentServiceImpl.java @@ -53,6 +53,7 @@ public class TencentServiceImpl extends AbstractS3BaseFileService implements Bas .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "cos")).build(); testConnection(); + isInitialized = true; } } @@ -74,4 +75,5 @@ public class TencentServiceImpl extends AbstractS3BaseFileService implements Bas add(new StorageConfig("isPrivate", "是否是私有空间")); }}; } + } \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/impl/UFileServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/UFileServiceImpl.java index be9e259..cbc7802 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/UFileServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/UFileServiceImpl.java @@ -17,4 +17,4 @@ public class UFileServiceImpl extends UpYunServiceImpl { return StorageTypeEnum.UFILE; } -} +} \ No newline at end of file diff --git a/src/main/java/im/zhaojun/zfile/service/impl/UpYunServiceImpl.java b/src/main/java/im/zhaojun/zfile/service/impl/UpYunServiceImpl.java index d87c86d..36e5f96 100644 --- a/src/main/java/im/zhaojun/zfile/service/impl/UpYunServiceImpl.java +++ b/src/main/java/im/zhaojun/zfile/service/impl/UpYunServiceImpl.java @@ -66,6 +66,7 @@ public class UpYunServiceImpl extends AbstractBaseFileService implements BaseFil } else { upYun = new UpYun(bucketName, username, password); testConnection(); + isInitialized = true; } }