From 5e61db3a548a757e0628a7742c30996ea06461f2 Mon Sep 17 00:00:00 2001 From: qyx <565485304@qq.com> Date: Thu, 5 Aug 2021 17:34:30 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=9B=9E=E9=80=80=E6=9B=B4=E6=94=B9](master):?= =?UTF-8?q?=20=E5=9B=9E=E9=80=80=E5=88=B0=E6=9C=80=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 回退错误代码 --- .../service/mapstruct/LogErrorMapperImpl.java | 2 +- .../service/mapstruct/LogSmallMapperImpl.java | 2 +- .../modules/upload/domain/UploadFile.java | 62 +++-- .../upload/rest/UploadFileController.java | 243 ++++++++++++++++-- .../upload/service/UploadFileService.java | 101 ++++---- .../upload/service/dto/UploadFileDto.java | 49 ++-- .../service/dto/UploadFileQueryCriteria.java | 54 ++-- .../service/impl/UploadFileServiceImpl.java | 213 ++++++++++++--- .../rest/TbUploadFileNewController.java | 4 +- .../uploadnew/task/TransFormDataTask.java | 1 + .../src/main/resources/config/application.yml | 2 +- eladmin-system/src/main/resources/logback.xml | 4 +- .../me/zhengjie/SpringApplicationTest.java | 4 +- 13 files changed, 555 insertions(+), 186 deletions(-) diff --git a/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogErrorMapperImpl.java b/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogErrorMapperImpl.java index 9cf588d..ccb9899 100644 --- a/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogErrorMapperImpl.java +++ b/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogErrorMapperImpl.java @@ -9,7 +9,7 @@ import org.springframework.stereotype.Component; @Generated( value = "org.mapstruct.ap.MappingProcessor", - date = "2021-08-05T14:00:16+0800", + date = "2021-08-05T16:32:05+0800", comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)" ) @Component diff --git a/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogSmallMapperImpl.java b/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogSmallMapperImpl.java index 4e9f242..6292435 100644 --- a/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogSmallMapperImpl.java +++ b/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogSmallMapperImpl.java @@ -9,7 +9,7 @@ import org.springframework.stereotype.Component; @Generated( value = "org.mapstruct.ap.MappingProcessor", - date = "2021-08-05T14:00:16+0800", + date = "2021-08-05T16:32:05+0800", comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)" ) @Component diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/domain/UploadFile.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/domain/UploadFile.java index 881ec2c..c61a07c 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/domain/UploadFile.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/domain/UploadFile.java @@ -1,40 +1,41 @@ /* -* Copyright 2019-2020 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.zhengjie.modules.upload.domain; -import lombok.Data; import cn.hutool.core.bean.BeanUtil; -import io.swagger.annotations.ApiModelProperty; import cn.hutool.core.bean.copier.CopyOptions; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + import javax.persistence.*; -import javax.validation.constraints.*; -import java.sql.Timestamp; import java.io.Serializable; +import java.sql.Timestamp; /** -* @website https://el-admin.vip -* @description / -* @author x -* @date 2021-08-05 -**/ + * @website https://el-admin.vip + * @description / + * @author x + * @date 2021-01-03 + **/ @Entity @Data @Table(name="tb_upload_file") public class UploadFile implements Serializable { + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") @@ -45,9 +46,16 @@ public class UploadFile implements Serializable { @ApiModelProperty(value = "上传日期") private Timestamp uploadTime; - @Column(name = "opration") + /** + * ps:支持一次上传多个文件,采用一个名称来进行标识 + */ + @Column(name = "upload_file_task_name") + @ApiModelProperty(value = "上传任务名称") + private String uploadFileTaskName; + + @Column(name = "operation") @ApiModelProperty(value = "操作人") - private String opration; + private String operation; @Column(name = "file_count") @ApiModelProperty(value = "文件解析总数") @@ -61,6 +69,10 @@ public class UploadFile implements Serializable { @ApiModelProperty(value = "上传状态") private Integer uploadTag; + @Column(name = "local_save_path") + @ApiModelProperty(value = "文件上传保存路径") + private String localSavePath; + public void copy(UploadFile source){ BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true)); } 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 94fc0b5..8d1e3be 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 @@ -1,48 +1,93 @@ /* -* Copyright 2019-2020 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 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 -**/ -@RestController + * @website https://el-admin.vip + * @author x + * @date 2021-01-03 + **/ +//@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 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 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 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 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/upload/service/UploadFileService.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/UploadFileService.java index c6b6b50..5b08f3d 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/UploadFileService.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/UploadFileService.java @@ -1,50 +1,52 @@ /* -* Copyright 2019-2020 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.zhengjie.modules.upload.service; import me.zhengjie.modules.upload.domain.UploadFile; import me.zhengjie.modules.upload.service.dto.UploadFileDto; import me.zhengjie.modules.upload.service.dto.UploadFileQueryCriteria; import org.springframework.data.domain.Pageable; -import java.util.Map; -import java.util.List; -import java.io.IOException; +import org.springframework.web.multipart.MultipartFile; + import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.List; +import java.util.Map; /** -* @website https://el-admin.vip -* @description 服务接口 -* @author x -* @date 2021-08-05 -**/ + * @website https://el-admin.vip + * @description 服务接口 + * @author x + * @date 2021-01-03 + **/ public interface UploadFileService { /** - * 查询数据分页 - * @param criteria 条件 - * @param pageable 分页参数 - * @return Map - */ + * 查询数据分页 + * @param criteria 条件 + * @param pageable 分页参数 + * @return Map + */ Map queryAll(UploadFileQueryCriteria criteria, Pageable pageable); /** - * 查询所有数据不分页 - * @param criteria 条件参数 - * @return List - */ + * 查询所有数据不分页 + * @param criteria 条件参数 + * @return List + */ List queryAll(UploadFileQueryCriteria criteria); /** @@ -55,29 +57,36 @@ public interface UploadFileService { UploadFileDto findById(Long id); /** - * 创建 - * @param resources / - * @return UploadFileDto - */ + * 创建 + * @param resources / + * @return UploadFileDto + */ UploadFileDto create(UploadFile resources); /** - * 编辑 - * @param resources / - */ + * 编辑 + * @param resources / + */ void update(UploadFile resources); /** - * 多选删除 - * @param ids / - */ + * 多选删除 + * @param ids / + */ void deleteAll(Long[] ids); /** - * 导出数据 - * @param all 待导出的数据 - * @param response / - * @throws IOException / - */ + * 导出数据 + * @param all 待导出的数据 + * @param response / + * @throws IOException / + */ void download(List all, HttpServletResponse response) throws IOException; + + /** + * 加密并生成存储文件 + * + * @param files + */ + UploadFileDto encryptDataAndSaveToFile(MultipartFile[] files, String taskName); } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileDto.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileDto.java index e5b7c06..ac27f81 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileDto.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileDto.java @@ -1,30 +1,31 @@ /* -* Copyright 2019-2020 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.zhengjie.modules.upload.service.dto; import lombok.Data; -import java.sql.Timestamp; + import java.io.Serializable; +import java.sql.Timestamp; /** -* @website https://el-admin.vip -* @description / -* @author x -* @date 2021-08-05 -**/ + * @website https://el-admin.vip + * @description / + * @author x + * @date 2021-01-03 + **/ @Data public class UploadFileDto implements Serializable { @@ -35,7 +36,7 @@ public class UploadFileDto implements Serializable { private Timestamp uploadTime; /** 操作人 */ - private String opration; + private String operation; /** 文件解析总数 */ private Long fileCount; @@ -45,4 +46,10 @@ public class UploadFileDto implements Serializable { /** 上传状态 */ private Integer uploadTag; + + /** 上传保存路径 */ + private String localSavePath; + + /** 上传的文件名称 **/ + private String uploadFileTaskName; } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileQueryCriteria.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileQueryCriteria.java index 31a77c1..3317b05 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileQueryCriteria.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileQueryCriteria.java @@ -1,44 +1,50 @@ /* -* Copyright 2019-2020 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.zhengjie.modules.upload.service.dto; import lombok.Data; +import me.zhengjie.annotation.Query; + import java.sql.Timestamp; import java.util.List; -import me.zhengjie.annotation.Query; /** -* @website https://el-admin.vip -* @author x -* @date 2021-08-05 -**/ + * @website https://el-admin.vip + * @author x + * @date 2021-01-03 + **/ @Data public class UploadFileQueryCriteria{ /** 精确 */ - @Query + @Query(type = Query.Type.EQUAL) private Long id; - /** 模糊 */ - @Query(type = Query.Type.INNER_LIKE) - private String opration; + /** 精确 */ + @Query(type = Query.Type.EQUAL) + private String operation; /** 精确 */ - @Query + @Query(type = Query.Type.EQUAL) private Integer uploadTag; + + /** 精确 */ + @Query(type = Query.Type.EQUAL) + private String uploadFileTaskName; + /** BETWEEN */ @Query(type = Query.Type.BETWEEN) private List uploadTime; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/impl/UploadFileServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/impl/UploadFileServiceImpl.java index 50adeba..a2f8e21 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/impl/UploadFileServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/impl/UploadFileServiceImpl.java @@ -1,71 +1,111 @@ /* -* Copyright 2019-2020 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.zhengjie.modules.upload.service.impl; -import me.zhengjie.modules.upload.domain.UploadFile; -import me.zhengjie.utils.ValidationUtil; -import me.zhengjie.utils.FileUtil; +import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.RandomUtil; +import cn.hutool.system.OsInfo; +import cn.hutool.system.SystemUtil; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import me.zhengjie.modules.upload.consts.SysConst; +import me.zhengjie.modules.upload.domain.UploadFile; import me.zhengjie.modules.upload.repository.UploadFileRepository; 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.service.mapstruct.UploadFileMapper; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; +import me.zhengjie.utils.*; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import me.zhengjie.utils.PageUtil; -import me.zhengjie.utils.QueryHelp; -import java.util.List; -import java.util.Map; -import java.io.IOException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + import javax.servlet.http.HttpServletResponse; -import java.util.ArrayList; -import java.util.LinkedHashMap; +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.util.*; + +import static me.zhengjie.modules.upload.consts.SysConst.TEST_USER_NAME; +import static me.zhengjie.modules.upload.consts.UploadFileConst.*; + /** -* @website https://el-admin.vip -* @description 服务实现 -* @author x -* @date 2021-08-05 -**/ + * @author x + * @website https://el-admin.vip + * @description 服务实现 + * @date 2021-01-03 + **/ @Service @RequiredArgsConstructor +@Slf4j public class UploadFileServiceImpl implements UploadFileService { + /** + * 远程服务器地址 + */ + @Value("${remote.link.address}") + private String remoteFileServerAddress; + + /** + * 远程上传临时存放文件地址 - linux环境 + */ + @Value("${remote.link.file-base-path-linux}") + private String remoteLinkFileBasePathLinux; + + /** + * 远程上传临时存放文件地址 - linux环境 + */ + @Value("${remote.link.file-base-path-windows}") + private String remoteLinkFileBasePathWindows; + + /** + * 远程上传临时存放文件地址 - linux环境 + */ + @Value("${remote.link.file-base-path-mac}") + private String remoteLinkFileBasePathMac; + + private final UploadFileRepository uploadFileRepository; private final UploadFileMapper uploadFileMapper; + @Override - public Map queryAll(UploadFileQueryCriteria criteria, Pageable pageable){ - Page page = uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); + public Map queryAll(UploadFileQueryCriteria criteria, Pageable pageable) { + + Page page = uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable); return PageUtil.toPage(page.map(uploadFileMapper::toDto)); } + @Override - public List queryAll(UploadFileQueryCriteria criteria){ - return uploadFileMapper.toDto(uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); + public List queryAll(UploadFileQueryCriteria criteria) { + return uploadFileMapper.toDto(uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder))); } @Override @Transactional public UploadFileDto findById(Long id) { UploadFile uploadFile = uploadFileRepository.findById(id).orElseGet(UploadFile::new); - ValidationUtil.isNull(uploadFile.getId(),"UploadFile","id",id); + ValidationUtil.isNull(uploadFile.getId(), "UploadFile", "id", id); return uploadFileMapper.toDto(uploadFile); } @@ -79,7 +119,7 @@ public class UploadFileServiceImpl implements UploadFileService { @Transactional(rollbackFor = Exception.class) public void update(UploadFile resources) { UploadFile uploadFile = uploadFileRepository.findById(resources.getId()).orElseGet(UploadFile::new); - ValidationUtil.isNull( uploadFile.getId(),"UploadFile","id",resources.getId()); + ValidationUtil.isNull(uploadFile.getId(), "UploadFile", "id", resources.getId()); uploadFile.copy(resources); uploadFileRepository.save(uploadFile); } @@ -95,9 +135,9 @@ public class UploadFileServiceImpl implements UploadFileService { public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (UploadFileDto uploadFile : all) { - Map map = new LinkedHashMap<>(); + Map map = new LinkedHashMap<>(); map.put("上传日期", uploadFile.getUploadTime()); - map.put("操作人", uploadFile.getOpration()); + map.put("操作人", uploadFile.getOperation()); map.put("文件解析总数", uploadFile.getFileCount()); map.put("上传成功总数", uploadFile.getFileTransSuccessCount()); map.put("上传状态", uploadFile.getUploadTag()); @@ -105,4 +145,99 @@ public class UploadFileServiceImpl implements UploadFileService { } FileUtil.downloadExcel(list, response); } + + @Override + public UploadFileDto encryptDataAndSaveToFile(MultipartFile[] files, String taskName) { + // 1. 文件存储到本地 + long count = 0; // 统计总数 + String baseStr = ""; // 生成通用随机文件夹存放每次的文件 + StringBuilder pathBuilder = new StringBuilder(); + + for (MultipartFile file : files) { + // 获取原来的文件后缀 + String originalFilename = file.getOriginalFilename(); + if (StringUtils.isBlank(originalFilename)) { + log.error("===================== [input file name is empty, please check ]==================="); + } + + String extName = FileUtil.extName(originalFilename); + baseStr += FileUtil.mainName(originalFilename); + String eachFilePath = buildFileWritePath(baseStr, extName); + try { + // 把文件保存到本地路径 + file.transferTo(Paths.get(eachFilePath)); + baseStr = ""; + } catch (IOException e) { + log.error("============== [transferTo file fail, path is {} ] ==============", eachFilePath,e); + } + // 统计行数 + List tempList = FileUtil.readLines(eachFilePath, "UTF-8"); + if (CollectionUtil.isNotEmpty(tempList)){ + count += tempList.size(); + } + // 保存所有的临时存放路径 + pathBuilder.append(eachFilePath); + pathBuilder.append(FILE_PATH_SPLIT); + + } + + // 2. 更新上传记录为正在上传,解析了有多少条 + UploadFile uploadFile = new UploadFile(); + uploadFile.setUploadFileTaskName(taskName); + uploadFile.setUploadTime(new Timestamp(System.currentTimeMillis())); + String currentUsername; + if (SysConst.sysDebug){ + uploadFile.setOperation(TEST_USER_NAME);//fixme 这边测试环境补充一下需要的操作人 + }else { + currentUsername = SecurityUtils.getCurrentUsername(); + uploadFile.setOperation(currentUsername); + } + + uploadFile.setFileCount(count); + uploadFile.setFileTransSuccessCount(0L); + uploadFile.setUploadTag(DOING_TAG); + String tempFilesPath = pathBuilder.toString().substring(0, pathBuilder.length() - 1); + if (StringUtils.isNotBlank(tempFilesPath)){ + uploadFile.setLocalSavePath(tempFilesPath); // 去掉最后的那个通配符 FILE_PATH_SPLIT + } + + UploadFileDto uploadFileDto = create(uploadFile); + + return uploadFileDto; + } + + + /** + * 构建文件上传保存路径 + */ + private String buildFileWritePath(String baseStr, String extName) { + // 获取环境配置信息 + OsInfo osInfo = SystemUtil.getOsInfo(); + // 定义的时间格式 + String timeFormate = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); + + String dirPath; + // 生成一个随机文件夹目录,方便整理和打包 + + String filePath = RandomUtil.randomString(6) + FILE_NAME_SPLIT + baseStr + FILE_SPLIT + extName; + + if (osInfo.isWindows()) { + dirPath = remoteLinkFileBasePathWindows + timeFormate + File.separator; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath + filePath; + } else if (osInfo.isLinux()) { + dirPath = remoteLinkFileBasePathLinux + timeFormate + File.separator; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath + filePath; + } else if (osInfo.isMac()) { + dirPath = remoteLinkFileBasePathMac + timeFormate + File.separator; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath + filePath; + } else { + return ""; + } + } } \ No newline at end of file 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 5ed1820..ec18c26 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 @@ -137,9 +137,9 @@ public class TbUploadFileNewController { */ @Log("新的成单数据及录音文件导入接口") @ApiOperation("新的成单数据及录音文件导入接口") -// @AnonymousAccess + @AnonymousAccess @PostMapping("/newFileUpload") - @PreAuthorize("@el.check('tbUploadFileNew:list')") // 这个换下相关按钮的权限控制 +// @PreAuthorize("@el.check('tbUploadFileNew:list')") // 这个换下相关按钮的权限控制 // @ResponseBody public ResponseEntity newFileUpload(@RequestParam("file") MultipartFile[] files, @RequestParam(value = "taskName") String taskName) { diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java index 5a500e4..ce4f9da 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java @@ -27,5 +27,6 @@ public class TransFormDataTask { } private void runTask(List formdataDtos) { + } } diff --git a/eladmin-system/src/main/resources/config/application.yml b/eladmin-system/src/main/resources/config/application.yml index 8749c6a..6a3d01f 100644 --- a/eladmin-system/src/main/resources/config/application.yml +++ b/eladmin-system/src/main/resources/config/application.yml @@ -5,7 +5,7 @@ spring: freemarker: check-template-location: false profiles: - active: prod + active: dev jackson: time-zone: GMT+8 data: diff --git a/eladmin-system/src/main/resources/logback.xml b/eladmin-system/src/main/resources/logback.xml index 4fd9306..40d230d 100644 --- a/eladmin-system/src/main/resources/logback.xml +++ b/eladmin-system/src/main/resources/logback.xml @@ -77,7 +77,7 @@ - - + + \ No newline at end of file diff --git a/eladmin-system/src/test/java/me/zhengjie/SpringApplicationTest.java b/eladmin-system/src/test/java/me/zhengjie/SpringApplicationTest.java index 55d5e70..9b31b2c 100644 --- a/eladmin-system/src/test/java/me/zhengjie/SpringApplicationTest.java +++ b/eladmin-system/src/test/java/me/zhengjie/SpringApplicationTest.java @@ -54,7 +54,7 @@ public class SpringApplicationTest { UploadFileDto uploadFileDto = new UploadFileDto(); uploadFileDto.setUploadTime(new Timestamp(new java.util.Date().getTime())); - uploadFileDto.setOperation("测试用户1"); +// uploadFileDto.setOperation("测试用户1"); uploadFileDto.setFileCount(1000000L); uploadFileDto.setFileTransSuccessCount(999999L); uploadFileDto.setUploadFileTaskName("测试任务1"); @@ -73,7 +73,7 @@ public class SpringApplicationTest { Pageable pageable = PageRequest.of(1, 10); UploadFileQueryCriteria uploadFileQueryCriteria = new UploadFileQueryCriteria(); - uploadFileQueryCriteria.setOperation("xiaoxing"); +// uploadFileQueryCriteria.setOperation("xiaoxing"); Page page = uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, uploadFileQueryCriteria, criteriaBuilder), pageable);