Administrator 1 жил өмнө
parent
commit
8d3515b1da

+ 97 - 36
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/UEditorController.java

@@ -1,17 +1,30 @@
 package com.ruoyi.web.controller.common;
 
-import com.ruoyi.system.domain.Ueditor;
-import com.ruoyi.system.service.impl.UploadService;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import javax.servlet.http.HttpServletRequest;
+
+import com.ruoyi.framework.config.ServerConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
-
-import javax.annotation.Resource;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.ruoyi.common.config.RuoYiConfig;
+import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.common.utils.file.FileUploadUtils;
 
 
 /**
  * 百度富文本编辑器
+ *
  * @Author: tjf
  * @Date: 2023/10/13 10:35
  * @Describe:
@@ -19,38 +32,86 @@ import java.io.IOException;
 @RestController
 @RequestMapping("/ueditor")
 @CrossOrigin
-public class UEditorController {
-
+public class UEditorController extends BaseController{
+    private final String METHOD_HEAD = "ueditor";
+    private final String IMGE_PATH = "/ueditor/images/";
+    private final String VIDEO_PATH = "/ueditor/videos/";
+    private final String FILE_PATH = "/ueditor/files/";
 
-    @Resource
-    UploadService uploadFile;
+    @Autowired
+    private ServerConfig serverConfig;
 
-    @RequestMapping(value = "/upload")
+    /**
+     * ueditor
+     */
     @ResponseBody
-    public Object server(String action, String callback, MultipartFile upfile, HttpServletResponse response) {
-        Object result = null;
-        switch (action) {
-            case Ueditor.ACTION_CONFIG:
-                String result1 = "";
-                if (callback != null) {
-                    result1 = callback + "(" + Ueditor.UEDITOR_CONFIG + ")";
-                } else {
-                    result1 = Ueditor.UEDITOR_CONFIG;
-                }
-                try {
-                    response.getWriter().write(result1);//返回的十回调函数
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-                break;
-            case Ueditor.ACTION_UPLOADFILE:
-            case Ueditor.ACTION_UPLOADVIDEO:
-            case Ueditor.ACTION_UPLOADIMAGE:
-                Ueditor ueditor = uploadFile.uploadPicForUeditor(upfile);
-                result = ueditor;
-                break;
-            default:
-        }
-        return result;
+    @RequestMapping(value = "/config")
+    public Object ueditor(HttpServletRequest request, @RequestParam(value = "action", required = true) String action,
+                          MultipartFile upfile) throws Exception
+    {
+        List<Object> param = new ArrayList<Object>()
+        {
+            {
+                add(action);
+                add(upfile);
+            }
+        };
+        Method method = this.getClass().getMethod(METHOD_HEAD + action, List.class, String.class);
+        return method.invoke(this.getClass().newInstance(), param, serverConfig.getUrl());
+    }
+
+    /**
+     * 读取配置文件
+     */
+    public JSONObject ueditorconfig(List<Object> param, String fileSuffixUrl) throws Exception
+    {
+        ClassPathResource classPathResource = new ClassPathResource("ueditor-config.json");
+        String jsonString = new BufferedReader(new InputStreamReader(classPathResource.getInputStream())).lines().parallel().collect(Collectors.joining(System.lineSeparator()));
+        JSONObject json = JSON.parseObject(jsonString, JSONObject.class);
+        return json;
+    }
+
+    /**
+     * 上传图片
+     */
+    public JSONObject ueditoruploadimage(List<Object> param, String fileSuffixUrl) throws Exception
+    {
+        JSONObject json = new JSONObject();
+        json.put("state", "SUCCESS");
+        json.put("url", ueditorcore(param, IMGE_PATH, false, fileSuffixUrl));
+        return json;
+    }
+
+    /**
+     * 上传视频
+     */
+    public JSONObject ueditoruploadvideo(List<Object> param, String fileSuffixUrl) throws Exception
+    {
+        JSONObject json = new JSONObject();
+        json.put("state", "SUCCESS");
+        json.put("url", ueditorcore(param, VIDEO_PATH, false, fileSuffixUrl));
+        return json;
+    }
+
+    /**
+     * 上传附件
+     */
+    public JSONObject ueditoruploadfile(List<Object> param, String fileSuffixUrl) throws Exception
+    {
+        JSONObject json = new JSONObject();
+        json.put("state", "SUCCESS");
+        json.put("url", ueditorcore(param, FILE_PATH, true, fileSuffixUrl));
+        return json;
+    }
+
+    public String ueditorcore(List<Object> param, String path, boolean isFileName, String fileSuffixUrl)
+            throws Exception
+    {
+        MultipartFile upfile = (MultipartFile) param.get(1);
+        // 上传文件路径
+        String filePath = RuoYiConfig.getUploadPath();
+        String fileName = FileUploadUtils.upload(filePath, upfile);
+        String url = fileSuffixUrl + fileName;
+        return url;
     }
 }

+ 1 - 1
ruoyi-admin/src/main/resources/application.yml

@@ -72,4 +72,4 @@ xss:
   # 排除链接(多个用逗号分隔)
   excludes: /system/notice
   # 匹配链接
-  urlPatterns: /system/*,/monitor/*,/tool/*
+  urlPatterns: /system/*,/monitor/*,/tool/*

+ 94 - 0
ruoyi-admin/src/main/resources/ueditor-config.json

@@ -0,0 +1,94 @@
+/* 前后端通信相关的配置,注释只允许使用多行方式 */
+{
+  /* 上传图片配置项 */
+  "imageActionName": "uploadimage", /* 执行上传图片的action名称 */
+  "imageFieldName": "upfile", /* 提交的图片表单名称 */
+  "imageMaxSize": 2048000, /* 上传大小限制,单位B */
+  "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
+  "imageCompressEnable": true, /* 是否压缩图片,默认是true */
+  "imageCompressBorder": 1600, /* 图片压缩最长边限制 */
+  "imageInsertAlign": "none", /* 插入的图片浮动方式 */
+  "imageUrlPrefix": "", /* 图片访问路径前缀 */
+  "imagePathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
+  /* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */
+  /* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */
+  /* {time} 会替换成时间戳 */
+  /* {yyyy} 会替换成四位年份 */
+  /* {yy} 会替换成两位年份 */
+  /* {mm} 会替换成两位月份 */
+  /* {dd} 会替换成两位日期 */
+  /* {hh} 会替换成两位小时 */
+  /* {ii} 会替换成两位分钟 */
+  /* {ss} 会替换成两位秒 */
+  /* 非法字符 \ : * ? " < > | */
+  /* 具请体看线上文档: fex.baidu.com/ueditor/#use-format_upload_filename */
+
+  /* 涂鸦图片上传配置项 */
+  "scrawlActionName": "uploadscrawl", /* 执行上传涂鸦的action名称 */
+  "scrawlFieldName": "upfile", /* 提交的图片表单名称 */
+  "scrawlPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
+  "scrawlMaxSize": 2048000, /* 上传大小限制,单位B */
+  "scrawlUrlPrefix": "", /* 图片访问路径前缀 */
+  "scrawlInsertAlign": "none",
+
+  /* 截图工具上传 */
+  "snapscreenActionName": "uploadimage", /* 执行上传截图的action名称 */
+  "snapscreenPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
+  "snapscreenUrlPrefix": "", /* 图片访问路径前缀 */
+  "snapscreenInsertAlign": "none", /* 插入的图片浮动方式 */
+
+  /* 抓取远程图片配置 */
+  "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"],
+  "catcherActionName": "catchimage", /* 执行抓取远程图片的action名称 */
+  "catcherFieldName": "source", /* 提交的图片列表表单名称 */
+  "catcherPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
+  "catcherUrlPrefix": "", /* 图片访问路径前缀 */
+  "catcherMaxSize": 2048000, /* 上传大小限制,单位B */
+  "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 抓取图片格式显示 */
+
+  /* 上传视频配置 */
+  "videoActionName": "uploadvideo", /* 执行上传视频的action名称 */
+  "videoFieldName": "upfile", /* 提交的视频表单名称 */
+  "videoPathFormat": "/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
+  "videoUrlPrefix": "", /* 视频访问路径前缀 */
+  "videoMaxSize": 102400000, /* 上传大小限制,单位B,默认100MB */
+  "videoAllowFiles": [
+    ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
+    ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], /* 上传视频格式显示 */
+
+  /* 上传文件配置 */
+  "fileActionName": "uploadfile", /* controller里,执行上传视频的action名称 */
+  "fileFieldName": "upfile", /* 提交的文件表单名称 */
+  "filePathFormat": "/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
+  "fileUrlPrefix": "", /* 文件访问路径前缀 */
+  "fileMaxSize": 51200000, /* 上传大小限制,单位B,默认50MB */
+  "fileAllowFiles": [
+    ".png", ".jpg", ".jpeg", ".gif", ".bmp",
+    ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
+    ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
+    ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
+    ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
+  ], /* 上传文件格式显示 */
+
+  /* 列出指定目录下的图片 */
+  "imageManagerActionName": "listimage", /* 执行图片管理的action名称 */
+  "imageManagerListPath": "/ueditor/jsp/upload/image/", /* 指定要列出图片的目录 */
+  "imageManagerListSize": 20, /* 每次列出文件数量 */
+  "imageManagerUrlPrefix": "", /* 图片访问路径前缀 */
+  "imageManagerInsertAlign": "none", /* 插入的图片浮动方式 */
+  "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 列出的文件类型 */
+
+  /* 列出指定目录下的文件 */
+  "fileManagerActionName": "listfile", /* 执行文件管理的action名称 */
+  "fileManagerListPath": "/ueditor/jsp/upload/file/", /* 指定要列出文件的目录 */
+  "fileManagerUrlPrefix": "", /* 文件访问路径前缀 */
+  "fileManagerListSize": 20, /* 每次列出文件数量 */
+  "fileManagerAllowFiles": [
+    ".png", ".jpg", ".jpeg", ".gif", ".bmp",
+    ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
+    ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
+    ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
+    ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
+  ] /* 列出的文件类型 */
+
+}

+ 2 - 0
ruoyi-common/pom.xml

@@ -137,6 +137,8 @@
             <artifactId>ueditor</artifactId>
             <version>1.4.3.3</version>
         </dependency>
+
+
     </dependencies>
 
 </project>

+ 1 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java

@@ -112,6 +112,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
                 .authorizeRequests()
                 // 对于登录login 注册register 验证码captchaImage 允许匿名访问
                 .antMatchers("/login", "/register", "/captchaImage").permitAll()
+                .antMatchers("/ueditor/**").permitAll()
                 .antMatchers("/investigate/**","/investigate/disposition/**").permitAll()
                 .antMatchers("/login", "/register", "/captchaImage","/common/filetext").permitAll()
                 // 静态资源,可匿名访问

+ 0 - 122
ruoyi-system/src/main/java/com/ruoyi/system/domain/Ueditor.java

@@ -1,122 +0,0 @@
-package com.ruoyi.system.domain;
-
-/**
- * @Author: tjf
- * @Date: 2023/10/13 11:24
- * @Describe:
- */
-
-public class Ueditor {
-        //前端传过来的参数,用于区分是什么请求
-        public final static String ACTION_CONFIG = "config";
-        public final static String ACTION_UPLOADIMAGE = "uploadimage";
-        public final static String ACTION_UPLOADVIDEO = "uploadvideo";
-        public final static String ACTION_UPLOADFILE = "uploadfile";
-        //后端返给前端的参数,需要时特定格式
-        public final static String ACTION_SUCCESS = "SUCCESS";
-        public final static String ACTION_FAIL = "FAIL";
-        private  String state;
-        private  String url;
-        private  String title;
-        private  String original;
-
-        //关键,假装自己是jsp的东西
-        public final static String UEDITOR_CONFIG = "{\n" +
-                "    \"imageActionName\": \"uploadimage\",\n" +
-                "    \"imageFieldName\": \"upfile\", \n" +
-                "    \"imageMaxSize\": 2048000, \n" +
-                "    \"imageAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"],\n" +
-                "    \"imageCompressEnable\": true, \n" +
-                "    \"imageCompressBorder\": 1600, \n" +
-                "    \"imageInsertAlign\": \"none\", \n" +
-                "    \"imageUrlPrefix\": \"\", \n" +
-                "    \"imagePathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\n" +
-                "    \"scrawlActionName\": \"uploadscrawl\",\n" +
-                "    \"scrawlFieldName\": \"upfile\", \n" +
-                "    \"scrawlPathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\n" +
-                "    \"scrawlMaxSize\": 2048000,\n" +
-                "    \"scrawlUrlPrefix\": \"\",\n" +
-                "    \"scrawlInsertAlign\": \"none\",\n" +
-                "    \"snapscreenActionName\": \"uploadimage\", \n" +
-                "    \"snapscreenPathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\n" +
-                "    \"snapscreenUrlPrefix\": \"\", \n" +
-                "    \"snapscreenInsertAlign\": \"none\",\n" +
-                "    \"catcherLocalDomain\": [\"127.0.0.1\", \"localhost\", \"img.baidu.com\"],\n" +
-                "    \"catcherActionName\": \"catchimage\", \n" +
-                "    \"catcherFieldName\": \"source\",\n" +
-                "    \"catcherPathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\",\n" +
-                "    \"catcherUrlPrefix\": \"\", \n" +
-                "    \"catcherMaxSize\": 2048000, \n" +
-                "    \"catcherAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"], \n" +
-                "    \"videoActionName\": \"uploadvideo\",\n" +
-                "    \"videoFieldName\": \"upfile\",\n" +
-                "    \"videoPathFormat\": \"/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}\",\n" +
-                "    \"videoUrlPrefix\": \"\",\n" +
-                "    \"videoMaxSize\": 102400000, \n" +
-                "    \"videoAllowFiles\": [\n" +
-                "        \".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\",\n" +
-                "        \".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\"], \n" +
-                "    \"fileActionName\": \"uploadfile\", \n" +
-                "    \"fileFieldName\": \"upfile\",\n" +
-                "    \"filePathFormat\": \"/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}\", \n" +
-                "    \"fileUrlPrefix\": \"\",\n" +
-                "    \"fileMaxSize\": 51200000, \n" +
-                "    \"fileAllowFiles\": [\n" +
-                "        \".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\",\n" +
-                "        \".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\",\n" +
-                "        \".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\",\n" +
-                "        \".rar\", \".zip\", \".tar\", \".gz\", \".7z\", \".bz2\", \".cab\", \".iso\",\n" +
-                "        \".doc\", \".docx\", \".xls\", \".xlsx\", \".ppt\", \".pptx\", \".pdf\", \".txt\", \".md\", \".xml\"\n" +
-                "    ],\n" +
-                "    \"imageManagerActionName\": \"listimage\",\n" +
-                "    \"imageManagerListPath\": \"/ueditor/jsp/upload/image/\",\n" +
-                "    \"imageManagerListSize\": 20, \n" +
-                "    \"imageManagerUrlPrefix\": \"\", \n" +
-                "    \"imageManagerInsertAlign\": \"none\", \n" +
-                "    \"imageManagerAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"],\n" +
-                "    \"fileManagerActionName\": \"listfile\", \n" +
-                "    \"fileManagerListPath\": \"/ueditor/jsp/upload/file/\",\n" +
-                "    \"fileManagerUrlPrefix\": \"\",\n" +
-                "    \"fileManagerListSize\": 20,\n" +
-                "    \"fileManagerAllowFiles\": [\n" +
-                "        \".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\",\n" +
-                "        \".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\",\n" +
-                "        \".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\",\n" +
-                "        \".rar\", \".zip\", \".tar\", \".gz\", \".7z\", \".bz2\", \".cab\", \".iso\",\n" +
-                "        \".doc\", \".docx\", \".xls\", \".xlsx\", \".ppt\", \".pptx\", \".pdf\", \".txt\", \".md\", \".xml\"\n" +
-                "    ]\n" +
-                "\n" +
-                "}";
-
-    public String getState() {
-        return state;
-    }
-
-    public void setState(String state) {
-        this.state = state;
-    }
-
-    public String getUrl() {
-        return url;
-    }
-
-    public void setUrl(String url) {
-        this.url = url;
-    }
-
-    public String getTitle() {
-        return title;
-    }
-
-    public void setTitle(String title) {
-        this.title = title;
-    }
-
-    public String getOriginal() {
-        return original;
-    }
-
-    public void setOriginal(String original) {
-        this.original = original;
-    }
-}

+ 0 - 36
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/UploadService.java

@@ -1,36 +0,0 @@
-package com.ruoyi.system.service.impl;
-
-import com.ruoyi.common.config.RuoYiConfig;
-import com.ruoyi.system.domain.Ueditor;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-import org.springframework.web.multipart.MultipartFile;
-import java.io.IOException;
-
-/**
- * @Author: tjf
- * @Date: 2023/10/13 11:26
- * @Describe:
- */
-@Service
-public class UploadService {
-
-
-    public Ueditor uploadPicForUeditor(MultipartFile file) {
-        Ueditor ueditor = new Ueditor();
-        ueditor.setState(Ueditor.ACTION_SUCCESS);
-        try {
-            //上传图片服务器后返回的地址
-            String s = RuoYiConfig.getUploadPath();
-            ueditor.setTitle(file.getName());
-            ueditor.setOriginal(file.getOriginalFilename());
-            ueditor.setUrl(s);
-            return ueditor;
-        } catch (Exception e) {
-            ueditor.setState(Ueditor.ACTION_FAIL);
-            e.printStackTrace();
-        }
-        return ueditor;
-    }
-
-}