diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/rest/UploadFileController.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/rest/UploadFileController.java index 2c7708f..e29929c 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/rest/UploadFileController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/rest/UploadFileController.java @@ -24,16 +24,23 @@ import me.zhengjie.annotation.AnonymousAccess; import me.zhengjie.annotation.Log; import me.zhengjie.common.http.CommonResponse; import me.zhengjie.common.http.ResponseCode; +import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.upload.consts.SysConst; import me.zhengjie.modules.upload.domain.UploadFile; import me.zhengjie.modules.upload.service.UploadFileService; import me.zhengjie.modules.upload.service.dto.UploadFileDto; import me.zhengjie.modules.upload.service.dto.UploadFileQueryCriteria; import me.zhengjie.modules.upload.task.SaveToFileTask; +import me.zhengjie.modules.uploadnew.redis.RedisParentDao; +import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService; +import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto; +import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewQueryCriteria; +import me.zhengjie.modules.uploadnew.task.SaveToFileNewTask; import me.zhengjie.utils.SecurityUtils; import me.zhengjie.utils.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; @@ -42,6 +49,9 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.ServletRequest; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; @@ -50,6 +60,7 @@ import java.util.Objects; import static me.zhengjie.modules.upload.consts.UploadFileConst.FILE_PATH_SPLIT; import static me.zhengjie.modules.upload.consts.UploadFileConst.WHITE_LIST; +import static me.zhengjie.modules.uploadnew.consts.SysConstNew.*; /** * @website https://el-admin.vip @@ -67,6 +78,13 @@ public class UploadFileController { private final UploadFileService uploadFileService; + @Autowired + private RedisTemplate redisTemplate; + @Autowired + private SaveToFileNewTask saveToFileNewTask; + @Autowired + private TbUploadFileNewService tbUploadFileNewService; + @Autowired private SaveToFileTask saveToFileTask; @@ -175,4 +193,94 @@ public class UploadFileController { return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK); } + + + /** + * 文件上传接口 + * + * @return 返回调用信息 + */ + @Log("上传并加密任务") + @ApiOperation("上传并加密任务") + @AnonymousAccess + @PostMapping("/fileUpload") + @PreAuthorize("@el.check('uploadFile:list')") +// @ResponseBody + public ResponseEntity fileUpload(@RequestParam("file") MultipartFile file, + @RequestParam(value = "taskName") String taskName, + @RequestParam(value = "isEcryptionNew") String isEcryptionNew, + ServletRequest servletRequest) { + // 记录日志 + log.info("TbUploadFileNewController:fileUpload | taskName="+taskName+"===== isEcryptionNew="+isEcryptionNew); + // 校验上传是否有文件 + if (file== null ){ + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_INPUT), HttpStatus.OK); + } + Integer isEcryption = 1; + if (StringUtils.isNotBlank(isEcryptionNew)){ + try { + isEcryption = Integer.parseInt(isEcryptionNew); + } catch (NumberFormatException e) { + log.error("TbUploadFileNewController|fileUpload 转换异常!!!!"); + throw new BadRequestException("转换异常!!!"); + } + } + String name=file.getOriginalFilename(); + String keyName = ""; + String token = ""; + HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; + Cookie[] cookies = httpServletRequest.getCookies(); + if (cookies != null){ + for(Cookie cookie : cookies) { + //获取cookie的名字 + token=cookie.getValue(); + log.info("TbUploadFileNewController:fileUpload | token="+token); + } + } + RedisParentDao redisParentDao = new RedisParentDao(); + redisParentDao.setRedisTemplate(redisTemplate); + keyName = token+TOKEN_NAME+name; + String keyValue = redisParentDao.getValue(keyName); + //防止用户多次提交 + if (keyValue != null){ + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_STAETING), HttpStatus.OK); + } + + redisParentDao.cacheValue(keyName, TOKEN_VALUE, EXP_TIME); + int lastIndexOf = name.lastIndexOf("."); + // 校验文件格式 + String nameStr = name.substring(lastIndexOf); + if (!((nameStr.equals(".xlsx")||nameStr.equals(".xls")) || nameStr.equals(".txt") ||nameStr.equals(".csv"))){ + redisParentDao.remove(keyName); + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_FORMAT), HttpStatus.OK); + } + // 任务名称检验,为必填参数,且不能重复 + if (org.apache.commons.lang3.StringUtils.isNotBlank(taskName)){ + // modify by x bug fix on POSTMAN request + if (org.apache.commons.lang3.StringUtils.contains(taskName, FILE_PATH_SPLIT)){ + taskName = taskName.substring(0, taskName.length()-1); + } + + TbUploadFileNewQueryCriteria criteria = new TbUploadFileNewQueryCriteria(); + criteria.setUploadFileTaskName(taskName); + List uploadFileDtos = tbUploadFileNewService.queryAll(criteria); + if (CollectionUtil.isNotEmpty(uploadFileDtos)){ + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_NAME_IS_EXIST), HttpStatus.OK); + } + }else { + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.EMPTY_ARGUMENT), HttpStatus.OK); + } + // 生成本地文件 + TbUploadFileNewDto tbUploadFileNewDto = tbUploadFileNewService.encryptDataAndSaveToFileNew(file, taskName, isEcryption); + // 如果临时生成失败就修改状态为失败 + if (Objects.isNull(tbUploadFileNewDto)){ + log.error("TbUploadFileNewController:fileUpload | Operation Fail.tbUploadFileNewDto is Null"); + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_FILE_SAVE_FAIL), HttpStatus.OK); + } + // 调用异步任务 + saveToFileNewTask.doRunTask(tbUploadFileNewDto); + redisParentDao.remove(keyName); + return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK); + } + } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/SysConstNew.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/SysConstNew.java index b94d34b..3585eca 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/SysConstNew.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/SysConstNew.java @@ -43,6 +43,18 @@ public class SysConstNew { */ public static String TEMP_FILE_END_STR = "bak"; + /** modeify by wzx 分割token和文件名 + */ + public static String TOKEN_NAME = "==="; + + /** modeify by wzx 分割token和文件名 + */ + public static String TOKEN_VALUE = "==="; + + /** modeify by wzx 分割token和文件名 + */ + public static long EXP_TIME = 180000; + /** * 使用Set方式注入 * diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/redis/RedisParentDao.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/redis/RedisParentDao.java new file mode 100644 index 0000000..0cc6262 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/redis/RedisParentDao.java @@ -0,0 +1,79 @@ +package me.zhengjie.modules.uploadnew.redis; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; + +import static org.apache.logging.log4j.ThreadContext.containsKey; + +@Component +@Slf4j +public class RedisParentDao { + private RedisTemplate redisTemplate; + + public void setRedisTemplate(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + /** + * 缓存value操作 + * @param k + * @param v + * @param time + * @return + */ + public boolean cacheValue(String k, String v, long time) { + try { + ValueOperations valueOps = redisTemplate.opsForValue(); + valueOps.set(k, v); + if (time > 0) redisTemplate.expire(k, time, TimeUnit.SECONDS); + return true; + } catch (Throwable t) { + log.error("TbUploadFileNewController:fileUpload | 缓存失败!!!"+t); + } + return false; + } + + /** + * 判断缓存是否存在 + * @param k + * @return + */ + public boolean containsValueKey(String k) { + return containsKey(k); + } + + /** + * 获取缓存 + * @param k + * @return + */ + public String getValue(String k) { + try { + ValueOperations valueOps = redisTemplate.opsForValue(); + return valueOps.get(k); + } catch (Throwable t) { + log.error("TbUploadFileNewController:fileUpload | 获取缓存失败!!!"); + } + return null; + } + + /** + * 移除缓存 + * @param key + * @return + */ + public boolean remove(String key) { + try { + redisTemplate.delete(key); + return true; + } catch (Throwable t) { + log.error("TbUploadFileNewController:fileUpload | 移除缓存失败!!!"); + } + return false; + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java index fb24ccc..54f2ec8 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java @@ -24,7 +24,9 @@ import me.zhengjie.annotation.AnonymousAccess; import me.zhengjie.annotation.Log; import me.zhengjie.common.http.CommonResponse; import me.zhengjie.common.http.ResponseCode; +import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew; +import me.zhengjie.modules.uploadnew.redis.RedisParentDao; import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService; import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto; import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewQueryCriteria; @@ -49,6 +51,7 @@ import java.util.List; import java.util.Objects; import static me.zhengjie.modules.upload.consts.UploadFileConst.FILE_PATH_SPLIT; +import static me.zhengjie.modules.uploadnew.consts.SysConstNew.*; //import org.springframework.util.StringUtils; @@ -121,8 +124,9 @@ public class TbUploadFileNewController { */ @Log("上传并加密任务") @ApiOperation("上传并加密任务") - @AnonymousAccess +// @AnonymousAccess @PostMapping("/fileUpload") + @PreAuthorize("@el.check('tbUploadFileNew:list')") // @ResponseBody public ResponseEntity fileUpload(@RequestParam("file") MultipartFile file, @RequestParam(value = "taskName") String taskName, @@ -134,11 +138,14 @@ public class TbUploadFileNewController { if (file== null ){ return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_INPUT), HttpStatus.OK); } - // FIXME: 2021/4/23 0023 - Integer isEcryption = Integer.parseInt(isEcryptionNew); - // FIXME: 2021/4/23 0023 + Integer isEcryption = 0; + try { + isEcryption = Integer.parseInt(isEcryptionNew); + } catch (NumberFormatException e) { + log.error("TbUploadFileNewController|fileUpload 转换异常!!!!"); + throw new BadRequestException("转换异常!!!"); + } String name=file.getOriginalFilename(); - String keyName = ""; String token = ""; HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; @@ -146,26 +153,25 @@ public class TbUploadFileNewController { if (cookies != null){ for(Cookie cookie : cookies) { //获取cookie的名字 - String cookieName = cookie.getName(); token=cookie.getValue(); + log.info("TbUploadFileNewController:fileUpload | token="+token); } } - - // FIXME: 2021/4/23 0023 === - keyName = token+"==="+name; - Object keyValue = redisTemplate.opsForValue().get(keyName); + RedisParentDao redisParentDao = new RedisParentDao(); + redisParentDao.setRedisTemplate(redisTemplate); + keyName = token+TOKEN_NAME+name; + String keyValue = redisParentDao.getValue(keyName); //防止用户多次提交 if (keyValue != null){ return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_STAETING), HttpStatus.OK); } - redisTemplate.opsForValue().set(keyName, "value11"); - - + redisParentDao.cacheValue(keyName, TOKEN_VALUE, EXP_TIME); int lastIndexOf = name.lastIndexOf("."); // 校验文件格式 String nameStr = name.substring(lastIndexOf); if (!((nameStr.equals(".xlsx")||nameStr.equals(".xls")) || nameStr.equals(".txt") ||nameStr.equals(".csv"))){ + redisParentDao.remove(keyName); return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_FORMAT), HttpStatus.OK); } // 任务名称检验,为必填参数,且不能重复 @@ -184,9 +190,6 @@ public class TbUploadFileNewController { }else { return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.EMPTY_ARGUMENT), HttpStatus.OK); } - - - // 生成本地文件 TbUploadFileNewDto tbUploadFileNewDto = tbUploadFileNewService.encryptDataAndSaveToFileNew(file, taskName, isEcryption); // 如果临时生成失败就修改状态为失败 @@ -194,13 +197,27 @@ public class TbUploadFileNewController { log.error("TbUploadFileNewController:fileUpload | Operation Fail.tbUploadFileNewDto is Null"); return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_FILE_SAVE_FAIL), HttpStatus.OK); } - // 调用异步任务 saveToFileNewTask.doRunTask(tbUploadFileNewDto); - - redisTemplate.delete(keyName); + redisParentDao.remove(keyName); + return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK); + } + @Log("上传并加密任务") + @ApiOperation("上传并加密任务") + @AnonymousAccess + @PostMapping("/aa") +// @ResponseBody + public ResponseEntity aa(){ + RedisParentDao redisParentDao = new RedisParentDao(); + redisParentDao.setRedisTemplate(redisTemplate); + redisParentDao.cacheValue("111", "2222", 180000); + String value = redisParentDao.getValue("111"); + System.out.println("value = " + value); + redisParentDao.remove("111"); + + String value1 = redisParentDao.getValue("111"); + System.out.println("value = " + value1); return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK); } - } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java index ae92658..2cf6a64 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java @@ -28,6 +28,7 @@ import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService; import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto; import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewQueryCriteria; import me.zhengjie.modules.uploadnew.service.mapstruct.TbUploadFileNewMapper; +import me.zhengjie.modules.uploadnew.util.ExcelUtils; import me.zhengjie.modules.uploadnew.util.TxtUtils; import me.zhengjie.utils.*; import org.springframework.beans.factory.annotation.Value;