From 23bb3960ab5d9d06f49f33e73ffaa284ff68bf71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=BF=8A?= <873019219@qq.com> Date: Sun, 11 Apr 2021 15:56:08 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=B7=B2=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E7=9A=84=E9=A9=B1=E5=8A=A8=E5=99=A8=E4=B8=8D=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/home/DirectLinkController.java | 13 +++++++-- .../zfile/controller/home/FileController.java | 8 +++++- .../exception/GlobleExceptionHandler.java | 14 ++++++++-- .../exception/NotEnabledDriveException.java | 27 +++++++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/main/java/im/zhaojun/zfile/exception/NotEnabledDriveException.java diff --git a/src/main/java/im/zhaojun/zfile/controller/home/DirectLinkController.java b/src/main/java/im/zhaojun/zfile/controller/home/DirectLinkController.java index d7f3420..c2d8e30 100644 --- a/src/main/java/im/zhaojun/zfile/controller/home/DirectLinkController.java +++ b/src/main/java/im/zhaojun/zfile/controller/home/DirectLinkController.java @@ -3,10 +3,12 @@ package im.zhaojun.zfile.controller.home; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.URLUtil; import im.zhaojun.zfile.context.DriveContext; -import im.zhaojun.zfile.exception.NotAllowedDownloadException; +import im.zhaojun.zfile.exception.NotEnabledDriveException; import im.zhaojun.zfile.model.constant.ZFileConstant; import im.zhaojun.zfile.model.dto.FileItemDTO; +import im.zhaojun.zfile.model.entity.DriveConfig; import im.zhaojun.zfile.model.enums.FileTypeEnum; +import im.zhaojun.zfile.service.DriveConfigService; import im.zhaojun.zfile.service.base.AbstractBaseFileService; import org.springframework.stereotype.Controller; import org.springframework.util.AntPathMatcher; @@ -37,7 +39,14 @@ public class DirectLinkController { */ @GetMapping("/directlink/{driveId}/**") public String directlink(@PathVariable("driveId") Integer driveId, - final HttpServletRequest request) { + final HttpServletRequest request, + final HttpServletResponse response) throws IOException { + DriveConfig driveConfig = driveConfigService.findById(driveId); + Boolean enable = driveConfig.getEnable(); + if (!enable) { + throw new NotEnabledDriveException(); + } + String path = (String) request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); 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 d7e4b45..ce14071 100644 --- a/src/main/java/im/zhaojun/zfile/controller/home/FileController.java +++ b/src/main/java/im/zhaojun/zfile/controller/home/FileController.java @@ -3,7 +3,7 @@ package im.zhaojun.zfile.controller.home; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import im.zhaojun.zfile.context.DriveContext; -import im.zhaojun.zfile.exception.NotExistFileException; +import im.zhaojun.zfile.exception.NotEnabledDriveException; import im.zhaojun.zfile.exception.PasswordVerifyException; import im.zhaojun.zfile.model.constant.ZFileConstant; import im.zhaojun.zfile.model.dto.FileItemDTO; @@ -113,6 +113,12 @@ public class FileController { // 开始获取参数信息 SystemFrontConfigDTO systemConfig = systemConfigService.getSystemFrontConfig(driveId); DriveConfig driveConfig = driveConfigService.findById(driveId); + Boolean enable = driveConfig.getEnable(); + if (!enable) { + throw new NotEnabledDriveException(); + } + + systemConfig.setDebugMode(debug); systemConfig.setDefaultSwitchToImgMode(driveConfig.getDefaultSwitchToImgMode()); diff --git a/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java b/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java index 0ed4743..fbb3112 100644 --- a/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java +++ b/src/main/java/im/zhaojun/zfile/exception/GlobleExceptionHandler.java @@ -19,12 +19,22 @@ public class GlobleExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobleExceptionHandler.class); + + /** + * 不存在的文件异常 + */ + @ExceptionHandler({NotEnabledDriveException.class}) + @ResponseBody + public ResultBean notEnabledDrive() { + return ResultBean.error("驱动器已关闭"); + } + /** * 不存在的文件异常 */ @ExceptionHandler({NotExistFileException.class}) @ResponseBody - public ResultBean notExistFile(Exception ex) { + public ResultBean notExistFile() { return ResultBean.error("文件不存在"); } @@ -35,7 +45,7 @@ public class GlobleExceptionHandler { @ExceptionHandler({HttpMediaTypeNotAcceptableException.class, ClientAbortException.class}) @ResponseBody @ResponseStatus - public void clientAbortException(Exception ex) { + public void clientAbortException() { // if (log.isDebugEnabled()) { // log.debug("出现了断开异常:", ex); // } diff --git a/src/main/java/im/zhaojun/zfile/exception/NotEnabledDriveException.java b/src/main/java/im/zhaojun/zfile/exception/NotEnabledDriveException.java new file mode 100644 index 0000000..5bd2439 --- /dev/null +++ b/src/main/java/im/zhaojun/zfile/exception/NotEnabledDriveException.java @@ -0,0 +1,27 @@ +package im.zhaojun.zfile.exception; + +/** + * 未启用的驱动器异常 + */ +public class NotEnabledDriveException extends RuntimeException { + + public NotEnabledDriveException() { + } + + public NotEnabledDriveException(String message) { + super(message); + } + + public NotEnabledDriveException(String message, Throwable cause) { + super(message, cause); + } + + public NotEnabledDriveException(Throwable cause) { + super(cause); + } + + public NotEnabledDriveException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} \ No newline at end of file