EditorFileVerifyAop.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2012-present 铭软科技(mingsoft.net)
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. * <p>
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. * <p>
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. package net.mingsoft.cms.aop;
  22. import cn.hutool.core.util.ObjectUtil;
  23. import cn.hutool.core.util.StrUtil;
  24. import net.mingsoft.base.entity.ResultData;
  25. import net.mingsoft.basic.aop.FileVerifyAop;
  26. import net.mingsoft.basic.bean.UploadConfigBean;
  27. import net.mingsoft.basic.util.BasicUtil;
  28. import net.mingsoft.basic.util.FileUtil;
  29. import net.mingsoft.basic.util.SpringUtil;
  30. import net.mingsoft.cms.bean.EditorStateBean;
  31. import org.apache.commons.codec.binary.Base64;
  32. import org.apache.commons.lang3.StringUtils;
  33. import org.aspectj.lang.ProceedingJoinPoint;
  34. import org.aspectj.lang.annotation.Around;
  35. import org.aspectj.lang.annotation.Aspect;
  36. import org.aspectj.lang.annotation.Pointcut;
  37. import org.springframework.stereotype.Component;
  38. import org.springframework.web.multipart.MultipartFile;
  39. import java.util.ArrayList;
  40. import java.util.List;
  41. /**
  42. * @author 铭软开发团队
  43. * @ClassName: EditorFileVerifyAop
  44. * @Description: 检测编辑器上传的文件是否合法
  45. * @date 2025年6月12日15:14:17
  46. */
  47. @Component
  48. @Aspect
  49. public class EditorFileVerifyAop extends FileVerifyAop {
  50. /**
  51. * 切入点
  52. */
  53. @Pointcut()
  54. public void uploadPointCut(){}
  55. /**
  56. * 后台上传文件的时候,将验证zip里的文件
  57. * @param joinPoint
  58. * @return
  59. * @throws Throwable
  60. */
  61. @Around("execution(* net.mingsoft.cms.action.EditorAction.editor(..)) ")
  62. public Object uploadAop(ProceedingJoinPoint joinPoint) throws Throwable {
  63. // 1. 获取请求操作
  64. String action = BasicUtil.getString("action");
  65. // 获取配置操作直接返回
  66. if (StrUtil.isBlank(action) || "config".equals(action)) {
  67. return joinPoint.proceed();
  68. }
  69. List<MultipartFile> files = this.getFiles(joinPoint);
  70. UploadConfigBean bean = new UploadConfigBean();
  71. // 会有抓取批量操作
  72. for (MultipartFile multipartFile : files) {
  73. bean = new UploadConfigBean();
  74. bean.setFile(multipartFile);
  75. ResultData resultData = prepareUpload(bean, false);
  76. if (!resultData.isSuccess()) {
  77. return new EditorStateBean(false, resultData.getMsg()).toString();
  78. }
  79. }
  80. return joinPoint.proceed();
  81. }
  82. /**
  83. * web上传文件的时候,将验证zip里的文件
  84. * @param joinPoint
  85. * @return
  86. * @throws Throwable
  87. */
  88. @Around("execution(* net.mingsoft.cms.action.web.EditorAction.editor(..))")
  89. public Object webUploadAop(ProceedingJoinPoint joinPoint) throws Throwable {
  90. // 1. 获取请求操作
  91. String action = BasicUtil.getString("action");
  92. // 获取配置操作直接返回
  93. if (StrUtil.isBlank(action) || "config".equals(action)) {
  94. return joinPoint.proceed();
  95. }
  96. List<MultipartFile> files = this.getFiles(joinPoint);
  97. UploadConfigBean bean = new UploadConfigBean();
  98. // 会有抓取批量操作
  99. for (MultipartFile multipartFile : files) {
  100. bean = new UploadConfigBean();
  101. bean.setFile(multipartFile);
  102. ResultData resultData = prepareUpload(bean, true);
  103. if (!resultData.isSuccess()) {
  104. return new EditorStateBean(false, resultData.getMsg()).toString();
  105. }
  106. }
  107. return joinPoint.proceed();
  108. }
  109. /**
  110. * 获取上传的文件数组,可能存在多个
  111. * @param pjp
  112. * @return
  113. * @throws Exception
  114. */
  115. private List<MultipartFile> getFiles(ProceedingJoinPoint pjp) throws Exception{
  116. // 1. 获取请求操作
  117. String action = BasicUtil.getString("action");
  118. List<MultipartFile> files = new ArrayList<>();
  119. MultipartFile file = null;
  120. // 判断当前编辑器什么操作
  121. switch (action) {
  122. case "uploadscrawl":
  123. // 上传涂鸦文件
  124. String base64 = SpringUtil.getRequest().getParameter("upfile");
  125. byte[] bytes = Base64.decodeBase64(base64);
  126. // 尝试从流中获取后缀地址
  127. file = FileUtil.bytesToMultipartFile(bytes, "png");
  128. if (ObjectUtil.isNull(file)) {
  129. files.add(file);
  130. }
  131. break;
  132. case "uploadimage":
  133. case "uploadvideo":
  134. case "uploadfile":
  135. // 上传文件
  136. Object[] args = pjp.getArgs();
  137. file = null;
  138. // TODO: 2025/6/12 通过getType无法获取file文件信息,所以改成这个方法获取文件信息
  139. for (Object arg : args) {
  140. if (arg instanceof MultipartFile) {
  141. file = (MultipartFile) arg;
  142. }
  143. }
  144. files.add(file);
  145. break;
  146. case "catchimage":
  147. // 抓取网络图片到本地
  148. // 获取图片地址
  149. String[] remotes = SpringUtil.getRequest().getParameterValues("source[]");
  150. for (String remote : remotes) {
  151. if (StringUtils.isBlank(remote)) {
  152. continue;
  153. }
  154. file = FileUtil.remoteUrlToMultipartFile(remote, "png");
  155. if (file == null) {
  156. continue;
  157. }
  158. files.add(file);
  159. }
  160. break;
  161. default:
  162. break;
  163. }
  164. return files;
  165. }
  166. }