From 8db25bb39d96a1145eacb09ac816c3271ad9970a Mon Sep 17 00:00:00 2001 From: haotian2546 Date: Fri, 11 Jan 2019 18:18:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=84=E4=BB=B6=E6=A8=A1=E5=9D=97=E3=80=81ck?= =?UTF-8?q?editor=E9=99=84=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E9=99=90=E5=88=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AttachmentController.java | 22 ++++++++++-- .../controller/CKEditorController.java | 34 +++++++++++++++---- .../views/admin/attachment/setting.html | 16 +++++++++ .../views/admin/attachment/upload.html | 2 ++ 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/jpress-web/src/main/java/io/jpress/web/commons/controller/AttachmentController.java b/jpress-web/src/main/java/io/jpress/web/commons/controller/AttachmentController.java index c349dab85..a3ad38812 100644 --- a/jpress-web/src/main/java/io/jpress/web/commons/controller/AttachmentController.java +++ b/jpress-web/src/main/java/io/jpress/web/commons/controller/AttachmentController.java @@ -23,9 +23,11 @@ import io.jpress.commons.utils.AliyunOssUtils; import io.jpress.commons.utils.AttachmentUtils; import io.jpress.model.Attachment; import io.jpress.service.AttachmentService; +import io.jpress.service.OptionService; import io.jpress.web.base.UserControllerBase; import javax.inject.Inject; +import java.io.File; /** * @author Michael Yang 杨福海 (fuhai999@gmail.com) @@ -39,7 +41,8 @@ public class AttachmentController extends UserControllerBase { @Inject private AttachmentService as; - + @Inject + OptionService optionService; public void upload() { if (!isMultipartRequest()) { @@ -49,10 +52,25 @@ public class AttachmentController extends UserControllerBase { UploadFile uploadFile = getFile(); if (uploadFile == null) { - renderJson(Ret.fail().set("success", false)); + renderJson(Ret.fail().set("message", "请提交上传的文件")); return; } + String mineType = uploadFile.getContentType(); + String fileType = mineType.split("/")[0]; + Integer maxImgSize = optionService.findAsIntegerByKey("attachment_img_maxsize"); + Integer maxOtherSize = optionService.findAsIntegerByKey("attachment_other_maxsize"); + maxImgSize = maxImgSize == null ? 2 : maxImgSize; //没设置 默认2M + maxOtherSize = maxOtherSize == null ? 20 : maxOtherSize; //没设置 默认20M + Integer maxSize = fileType.equals("image") ? maxImgSize : maxOtherSize; + File file = uploadFile.getFile(); + if (file != null) { + int fileSize = Math.round(file.length() / 1024 * 100) / 100; + if (fileSize > maxSize*1024) { + renderJson(Ret.fail().set("message", "文件大小不能超过" + maxSize + "MB")); + return; + } + } String path = AttachmentUtils.moveFile(uploadFile); AliyunOssUtils.upload(path, AttachmentUtils.file(path)); diff --git a/jpress-web/src/main/java/io/jpress/web/commons/controller/CKEditorController.java b/jpress-web/src/main/java/io/jpress/web/commons/controller/CKEditorController.java index dad844a03..5044ad56a 100644 --- a/jpress-web/src/main/java/io/jpress/web/commons/controller/CKEditorController.java +++ b/jpress-web/src/main/java/io/jpress/web/commons/controller/CKEditorController.java @@ -22,8 +22,10 @@ import io.jboot.web.controller.annotation.RequestMapping; import io.jpress.commons.utils.AliyunOssUtils; import io.jpress.commons.utils.AttachmentUtils; import io.jpress.model.Attachment; +import io.jpress.service.OptionService; import io.jpress.web.base.UserControllerBase; +import javax.inject.Inject; import java.io.File; import java.util.HashMap; import java.util.Map; @@ -34,6 +36,9 @@ import java.util.Map; @RequestMapping("/commons/ckeditor") public class CKEditorController extends UserControllerBase { + @Inject + OptionService optionService; + public void index() { renderError(404); } @@ -46,16 +51,31 @@ public class CKEditorController extends UserControllerBase { return; } + Map result = new HashMap(); UploadFile uploadFile = getFile(); if (uploadFile == null) { - renderText("请提交上传的文件。"); + Map msgMap = new HashMap(); + msgMap.put("message", "请提交上传的文件"); + result.put("error", msgMap); + renderJson(result); return; } + + String mineType = uploadFile.getContentType(); + String fileType = mineType.split("/")[0]; + Integer maxImgSize = optionService.findAsIntegerByKey("attachment_img_maxsize"); + Integer maxOtherSize = optionService.findAsIntegerByKey("attachment_other_maxsize"); + maxImgSize = maxImgSize == null ? 2 : maxImgSize; //没设置 默认2M + maxOtherSize = maxOtherSize == null ? 20 : maxOtherSize; //没设置 默认20M + Integer maxSize = fileType.equals("image") ? maxImgSize : maxOtherSize; File file = uploadFile.getFile(); if (file != null) { int fileSize = Math.round(file.length() / 1024 * 100) / 100; - if (fileSize > 2048) { - renderText("图片大小不能超过2MB"); + if (fileSize > maxSize * 1024) { + Map msgMap = new HashMap(); + msgMap.put("message", "文件大小不能超过" + maxSize + "MB"); + result.put("error", msgMap); + renderJson(result); return; } } @@ -69,7 +89,7 @@ public class CKEditorController extends UserControllerBase { attachment.setTitle(uploadFile.getOriginalFileName()); attachment.setPath(path.replace("\\", "/")); attachment.setSuffix(FileUtils.getSuffix(uploadFile.getFileName())); - attachment.setMimeType(uploadFile.getContentType()); + attachment.setMimeType(mineType); if (attachment.save()) { @@ -82,9 +102,11 @@ public class CKEditorController extends UserControllerBase { map.put("url", JFinal.me().getContextPath() + attachment.getPath()); renderJson(map); } else { - renderText("系统错误"); + Map msgMap = new HashMap(); + msgMap.put("message", "系统错误"); + result.put("error", msgMap); + renderJson(result); } } - } diff --git a/jpress-web/src/main/resources/WEB-INF/views/admin/attachment/setting.html b/jpress-web/src/main/resources/WEB-INF/views/admin/attachment/setting.html index 32e42db94..aadfbf8e7 100755 --- a/jpress-web/src/main/resources/WEB-INF/views/admin/attachment/setting.html +++ b/jpress-web/src/main/resources/WEB-INF/views/admin/attachment/setting.html @@ -125,6 +125,22 @@ value="#option('attachment_watermark_img')"> + + +
+ +
+ +
+
+
+ +
+ +
+