|
|
|
@ -15,34 +15,79 @@
|
|
|
|
|
*/
|
|
|
|
|
package me.zhengjie.modules.upload.rest;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
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 lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import io.swagger.annotations.*;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
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;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
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
|
|
|
|
|
* @author x
|
|
|
|
|
* @date 2021-08-05
|
|
|
|
|
* @date 2021-01-03
|
|
|
|
|
**/
|
|
|
|
|
@RestController
|
|
|
|
|
//@RestController
|
|
|
|
|
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
@Controller
|
|
|
|
|
@Api(tags = "上传文件解析发送管理")
|
|
|
|
|
@RequestMapping("/api/uploadFile")
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class UploadFileController {
|
|
|
|
|
|
|
|
|
|
private final UploadFileService uploadFileService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisTemplate redisTemplate;
|
|
|
|
|
@Autowired
|
|
|
|
|
private SaveToFileNewTask saveToFileNewTask;
|
|
|
|
|
@Autowired
|
|
|
|
|
private TbUploadFileNewService tbUploadFileNewService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private SaveToFileTask saveToFileTask;
|
|
|
|
|
|
|
|
|
|
@Log("导出数据")
|
|
|
|
|
@ApiOperation("导出数据")
|
|
|
|
|
@GetMapping(value = "/download")
|
|
|
|
@ -56,9 +101,28 @@ public class UploadFileController {
|
|
|
|
|
@ApiOperation("查询上传文件解析发送")
|
|
|
|
|
@PreAuthorize("@el.check('uploadFile:list')")
|
|
|
|
|
public ResponseEntity<Object> query(UploadFileQueryCriteria criteria, Pageable pageable){
|
|
|
|
|
filterOfWriteList(criteria);
|
|
|
|
|
log.info("======= [ criteria is {} ] =======", criteria.toString());
|
|
|
|
|
return new ResponseEntity<>(uploadFileService.queryAll(criteria,pageable),HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加一个判断,除了白名单账户外,其他的人查询只能返回自己上传的列表
|
|
|
|
|
*
|
|
|
|
|
* @param criteria 查询的参数
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private void filterOfWriteList(UploadFileQueryCriteria criteria) {
|
|
|
|
|
if (!SysConst.sysDebug){
|
|
|
|
|
if (!CollectionUtil.contains(WHITE_LIST, SecurityUtils.getCurrentUsername())){
|
|
|
|
|
criteria.setOperation(SecurityUtils.getCurrentUsername());
|
|
|
|
|
log.info("======= [UploadFileServiceImpl|filterOfWriteList, userName is {} ] =======", SecurityUtils.getCurrentUsername());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping
|
|
|
|
|
@Log("新增上传文件解析发送")
|
|
|
|
|
@ApiOperation("新增上传文件解析发送")
|
|
|
|
@ -84,4 +148,139 @@ public class UploadFileController {
|
|
|
|
|
uploadFileService.deleteAll(ids);
|
|
|
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==== 去重开发 =====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Log("上传并加密任务")
|
|
|
|
|
@ApiOperation("上传并加密任务")
|
|
|
|
|
@PostMapping(value = "/sendTask")
|
|
|
|
|
@PreAuthorize("@el.check('uploadFile:list')")
|
|
|
|
|
// @AnonymousAccess // fixme 需要测试完成后进行去除和使用上面的权限注解
|
|
|
|
|
public ResponseEntity<Object> sendTask(@RequestParam(value = "files", required = false) MultipartFile[] files, @RequestParam(value = "taskName") String taskName ) {
|
|
|
|
|
// 任务名称检验,为必填参数,且不能重复
|
|
|
|
|
if (StringUtils.isNotBlank(taskName)){
|
|
|
|
|
// modify by x bug fix on POSTMAN request
|
|
|
|
|
if (StringUtils.contains(taskName, FILE_PATH_SPLIT)){
|
|
|
|
|
taskName = taskName.substring(0, taskName.length()-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UploadFileQueryCriteria criteria = new UploadFileQueryCriteria();
|
|
|
|
|
criteria.setUploadFileTaskName(taskName);
|
|
|
|
|
List<UploadFileDto> uploadFileDtos = uploadFileService.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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 校验上传是否有文件
|
|
|
|
|
if (files!= null && files.length<= 0){
|
|
|
|
|
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_INPUT), HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
// 生成本地文件
|
|
|
|
|
UploadFileDto uploadFileDto = uploadFileService.encryptDataAndSaveToFile(files, taskName);
|
|
|
|
|
|
|
|
|
|
// 如果临时生成失败就修改状态为失败
|
|
|
|
|
if (Objects.isNull(uploadFileDto)){
|
|
|
|
|
log.error("UploadFileController:sendTask | Operation Fail.UploadFile is Null");
|
|
|
|
|
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_FILE_SAVE_FAIL), HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
// 调用异步任务
|
|
|
|
|
saveToFileTask.doRunTask(uploadFileDto);
|
|
|
|
|
|
|
|
|
|
return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 文件上传接口
|
|
|
|
|
*
|
|
|
|
|
* @return 返回调用信息
|
|
|
|
|
*/
|
|
|
|
|
@Log("上传并加密任务")
|
|
|
|
|
@ApiOperation("上传并加密任务")
|
|
|
|
|
@AnonymousAccess
|
|
|
|
|
@PostMapping("/fileUpload")
|
|
|
|
|
@PreAuthorize("@el.check('uploadFile:list')")
|
|
|
|
|
// @ResponseBody
|
|
|
|
|
public ResponseEntity<Object> 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<TbUploadFileNewDto> 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|