|
|
|
@ -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<String,Object> queryAll(UploadFileQueryCriteria criteria, Pageable pageable){
|
|
|
|
|
Page<UploadFile> page = uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
|
|
|
|
|
public Map<String, Object> queryAll(UploadFileQueryCriteria criteria, Pageable pageable) {
|
|
|
|
|
|
|
|
|
|
Page<UploadFile> page = uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
|
|
|
|
|
return PageUtil.toPage(page.map(uploadFileMapper::toDto));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<UploadFileDto> queryAll(UploadFileQueryCriteria criteria){
|
|
|
|
|
return uploadFileMapper.toDto(uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
|
|
|
|
|
public List<UploadFileDto> 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<UploadFileDto> all, HttpServletResponse response) throws IOException {
|
|
|
|
|
List<Map<String, Object>> list = new ArrayList<>();
|
|
|
|
|
for (UploadFileDto uploadFile : all) {
|
|
|
|
|
Map<String,Object> map = new LinkedHashMap<>();
|
|
|
|
|
Map<String, Object> 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<String> 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 "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|