新增删除备份文件,下载备份文件的接口。
This commit is contained in:
parent
1d37a302be
commit
d58d783979
|
|
@ -10,6 +10,8 @@ import com.electromagnetic.industry.software.common.pojo.BackupFileResLog;
|
|||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
|
@ -29,10 +31,10 @@ public class FileController {
|
|||
private BackupPro backupPro;
|
||||
|
||||
@RequestMapping("/upload")
|
||||
public ElectromagneticResult<?> upload(@RequestParam("file") MultipartFile file) {
|
||||
public ElectromagneticResult<?> upload(@RequestParam("file") MultipartFile file, @RequestParam("id") String id) {
|
||||
BackupFileResLog backupFileResLog = BackupFileResLog.builder().backupStartTime(new Date()).fileName(file.getOriginalFilename()).backupSuccess(true).build();
|
||||
try {
|
||||
fileService.upload(file);
|
||||
fileService.upload(file, id);
|
||||
} catch (Exception e) {
|
||||
String details = ExceptionUtil.stacktraceToString(e);
|
||||
backupFileResLog.setBackupSuccess(false);
|
||||
|
|
@ -50,4 +52,20 @@ public class FileController {
|
|||
FileUtil.appendUtf8String(info.toString(), backupPro.getLogPath());
|
||||
return ElectromagneticResultUtil.success(JSONUtil.toJsonStr(backupFileResLog, jsonConfig));
|
||||
}
|
||||
|
||||
@RequestMapping("/remove")
|
||||
public ElectromagneticResult<?> remove(@RequestParam("id") String id) {
|
||||
try {
|
||||
fileService.remove(id);
|
||||
} catch (Exception e) {
|
||||
log.error("删除文件失败, id-->{},原因-->{}",id, e.getMessage(), e);
|
||||
ElectromagneticResultUtil.fail("-1", e.getMessage());
|
||||
}
|
||||
return ElectromagneticResultUtil.success(true);
|
||||
}
|
||||
|
||||
@RequestMapping("/download")
|
||||
public ResponseEntity<InputStreamResource> download(@RequestParam("id") String id) throws Exception {
|
||||
return fileService.download(id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,17 @@
|
|||
package com.electromagnetic.industry.software.backup.service;
|
||||
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public interface FileService {
|
||||
|
||||
void upload(MultipartFile file) throws IOException;
|
||||
void upload(MultipartFile file, String id) throws IOException;
|
||||
|
||||
void remove(String id);
|
||||
|
||||
ResponseEntity<InputStreamResource> download(String id) throws Exception;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,10 @@ package com.electromagnetic.industry.software.backup.serviceimp;
|
|||
import cn.hutool.core.io.FileUtil;
|
||||
import com.electromagnetic.industry.software.backup.pojo.BackupPro;
|
||||
import com.electromagnetic.industry.software.backup.service.FileService;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
|
|
@ -17,12 +21,33 @@ public class FileServiceImpl implements FileService {
|
|||
private BackupPro backupPro;
|
||||
|
||||
@Override
|
||||
public void upload(MultipartFile file) throws IOException {
|
||||
String saveFolder = backupPro.getSaveFolder();
|
||||
String fileName = file.getOriginalFilename();
|
||||
String destPath = saveFolder + File.separator + File.separator + fileName;
|
||||
public void upload(MultipartFile file, String id) throws IOException {
|
||||
String destPath = getFileSysPathById(id);
|
||||
if (!FileUtil.exist(destPath)) {
|
||||
FileUtil.writeFromStream(file.getInputStream(), destPath);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(String id) {
|
||||
String destPath = getFileSysPathById(id);
|
||||
FileUtil.del(destPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<InputStreamResource> download(String id) throws Exception {
|
||||
String destPath = getFileSysPathById(id);
|
||||
FileSystemResource fileSystemResource = new FileSystemResource(destPath);
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.contentLength(fileSystemResource.contentLength())
|
||||
.contentType(MediaType.parseMediaType("application/octet-stream"))
|
||||
.body(new InputStreamResource(fileSystemResource.getInputStream()));
|
||||
}
|
||||
|
||||
private String getFileSysPathById(String id) {
|
||||
String saveFolder = backupPro.getSaveFolder();
|
||||
return saveFolder + File.separator + id;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,27 +2,21 @@ package com.electromagnetic.industry.software.manage.service.serviceimpl;
|
|||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.text.StrFormatter;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.electromagnetic.industry.software.common.enums.*;
|
||||
import com.electromagnetic.industry.software.common.exception.BizException;
|
||||
import com.electromagnetic.industry.software.common.pojo.BackupFileResLog;
|
||||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||
import com.electromagnetic.industry.software.common.util.EleCommonUtil;
|
||||
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
||||
import com.electromagnetic.industry.software.common.util.IdWorker;
|
||||
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
|
||||
import com.electromagnetic.industry.software.manage.config.ElePropertyConfig;
|
||||
import com.electromagnetic.industry.software.manage.mapper.EdFileInfoMapper;
|
||||
import com.electromagnetic.industry.software.manage.mapper.FileBackupLogMapper;
|
||||
import com.electromagnetic.industry.software.manage.mapper.UserMapper;
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.EdFileInfo;
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.FileBackupLog;
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.User;
|
||||
import com.electromagnetic.industry.software.manage.pojo.req.RecycleFileQueryDTO;
|
||||
import com.electromagnetic.industry.software.manage.pojo.resp.FileRecycleQueryVO;
|
||||
|
|
@ -36,8 +30,6 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
|
@ -75,7 +67,6 @@ public class FileRecycleServiceImpl implements FileRecycleService {
|
|||
.orderByAsc(ObjUtil.equals(pars.getCreatedTime(), 0), EdFileInfo::getCreatedTime)
|
||||
.orderByDesc(ObjUtil.equals(pars.getCreatedTime(), 1), EdFileInfo::getCreatedTime)
|
||||
.orderByDesc(ObjUtil.isAllEmpty(pars.getCreatedTime(), pars.getVersionSort(), pars.getFileSizeSort(), pars.getFileNameSort(), pars.getUpdatedTime(), pars.getFileTypeSort()), EdFileInfo::getCreatedTime)
|
||||
.orderByDesc(ObjUtil.isAllEmpty(pars.getCreatedTime(), pars.getVersionSort(), pars.getFileSizeSort(), pars.getFileNameSort(), pars.getUpdatedTime(), pars.getFileTypeSort()), EdFileInfo::getCreatedTime)
|
||||
.orderByAsc(ObjUtil.equals(pars.getVersionSort(), 0), EdFileInfo::getFileVersion)
|
||||
.orderByDesc(ObjUtil.equals(pars.getVersionSort(), 1), EdFileInfo::getFileVersion)
|
||||
|
||||
|
|
@ -117,7 +108,6 @@ public class FileRecycleServiceImpl implements FileRecycleService {
|
|||
resetRes(records);
|
||||
UserThreadLocal.setSuccessInfo("", "", "查询所有删除文件成功");
|
||||
return ElectromagneticResultUtil.success(new RespPageVO<>(total, records));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -128,71 +118,73 @@ public class FileRecycleServiceImpl implements FileRecycleService {
|
|||
List<EdFileInfo> edFileInfos = this.edFileInfoMapper.selectList(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||
.select(EdFileInfo.class, file -> !StrUtil.equals(file.getColumn(), "file_content"))
|
||||
.eq(EdFileInfo::getFileId, fileId));
|
||||
List<String> fileSysPaths = new ArrayList<>();
|
||||
// List<String> fileSysPaths = new ArrayList<>();
|
||||
for (EdFileInfo edFileInfo : edFileInfos) {
|
||||
String fileSysPath = commonService.getFileSysPath(edFileInfo.getFilePath(), edFileInfo.getDataOwn());
|
||||
FileBackupLog fileBackupLog = fileBackupLogMapper.selectOne(Wrappers.lambdaQuery(FileBackupLog.class).eq(FileBackupLog::getFileId, edFileInfo.getId()));
|
||||
// 移动到tmp目录,七天后删除
|
||||
fileSystemService.moveFile(fileSysPath, elePropertyConfig.getEleTmpPath() + File.separator + new File(fileSysPath).getName());
|
||||
// FileBackupLog fileBackupLog = fileBackupLogMapper.selectOne(Wrappers.lambdaQuery(FileBackupLog.class).eq(FileBackupLog::getFileId, edFileInfo.getId()));
|
||||
//
|
||||
// String saveFileName = edFileInfo.getFileName() + "." + edFileInfo.getFileType() + "." + edFileInfo.getFileCode();
|
||||
|
||||
String saveFileName = edFileInfo.getFileName() + "." + edFileInfo.getFileType() + "." + edFileInfo.getFileCode();
|
||||
|
||||
// 表示从没有备份过该文件
|
||||
if (fileBackupLog == null) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
BackupFileResLog backup = backupTask.backup(fileSysPath);
|
||||
long endTime = System.currentTimeMillis();
|
||||
|
||||
FileBackupLog backupLog = new FileBackupLog()
|
||||
.setId(IdWorker.getSnowFlakeIdString())
|
||||
.setFileId(edFileInfo.getId())
|
||||
.setStartTime(startTime)
|
||||
.setEndTime(endTime)
|
||||
.setDuration(endTime - startTime)
|
||||
.setFileTime(FileUtil.lastModifiedTime(fileSysPath).getTime())
|
||||
.setCreateTime(new Date())
|
||||
.setFileName(saveFileName)
|
||||
.setFileCode(commonService.getFileCode(fileSysPath))
|
||||
.setFileCreateTime(edFileInfo.getCreatedTime())
|
||||
.setSource(FileBackupSource.REMOVE.code);
|
||||
|
||||
if (backup.getBackupSuccess()) {
|
||||
backupLog.setBackupSuccess(true);
|
||||
fileBackupLogMapper.insert(backupLog);
|
||||
} else {
|
||||
backupLog.setBackupSuccess(false);
|
||||
backupLog.setFailInfoDetail(backup.getFailInfoDetail());
|
||||
fileBackupLogMapper.insert(backupLog);
|
||||
throw new BizException(StrFormatter.format("删除文件 {} 失败,原因 备份该文件出现错误,联系管理员查看日志", saveFileName));
|
||||
}
|
||||
|
||||
} else {
|
||||
long startTime = System.currentTimeMillis();
|
||||
BackupFileResLog backup = backupTask.backup(fileSysPath);
|
||||
long endTime = System.currentTimeMillis();
|
||||
fileBackupLog.setStartTime(startTime)
|
||||
.setEndTime(endTime)
|
||||
.setDuration(endTime - startTime)
|
||||
.setFileTime(FileUtil.lastModifiedTime(fileSysPath).getTime())
|
||||
.setCreateTime(new Date())
|
||||
.setFileCode(commonService.getFileCode(fileSysPath))
|
||||
.setFileCreateTime(edFileInfo.getCreatedTime())
|
||||
.setFileName(saveFileName);
|
||||
if (backup.getBackupSuccess()) {
|
||||
fileBackupLog.setBackupSuccess(true);
|
||||
fileBackupLogMapper.update(fileBackupLog, null);
|
||||
} else {
|
||||
fileBackupLog.setBackupSuccess(false);
|
||||
fileBackupLog.setFailInfoDetail(backup.getFailInfoDetail());
|
||||
fileBackupLogMapper.update(fileBackupLog, null);
|
||||
throw new BizException(StrFormatter.format("删除文件 {} 失败,原因 备份该文件出现错误,联系管理员查看日志", saveFileName));
|
||||
}
|
||||
}
|
||||
fileSysPaths.add(fileSysPath);
|
||||
// // 表示从没有备份过该文件
|
||||
// if (fileBackupLog == null) {
|
||||
// long startTime = System.currentTimeMillis();
|
||||
// BackupFileResLog backup = backupTask.backup(fileSysPath, edFileInfo.getId());
|
||||
// long endTime = System.currentTimeMillis();
|
||||
//
|
||||
// FileBackupLog backupLog = new FileBackupLog()
|
||||
// .setId(IdWorker.getSnowFlakeIdString())
|
||||
// .setFileId(edFileInfo.getId())
|
||||
// .setStartTime(startTime)
|
||||
// .setEndTime(endTime)
|
||||
// .setDuration(endTime - startTime)
|
||||
// .setFileTime(FileUtil.lastModifiedTime(fileSysPath).getTime())
|
||||
// .setCreateTime(new Date())
|
||||
// .setFileName(saveFileName)
|
||||
// .setFileCode(commonService.getFileCode(fileSysPath))
|
||||
// .setFileCreateTime(edFileInfo.getCreatedTime())
|
||||
// .setSource(FileBackupSource.REMOVE.code);
|
||||
//
|
||||
// if (backup.getBackupSuccess()) {
|
||||
// backupLog.setBackupSuccess(true);
|
||||
// fileBackupLogMapper.insert(backupLog);
|
||||
// } else {
|
||||
// backupLog.setBackupSuccess(false);
|
||||
// backupLog.setFailInfoDetail(backup.getFailInfoDetail());
|
||||
// fileBackupLogMapper.insert(backupLog);
|
||||
// throw new BizException(StrFormatter.format("删除文件 {} 失败,原因 备份该文件出现错误,联系管理员查看日志", saveFileName));
|
||||
// }
|
||||
//
|
||||
// } else {
|
||||
// long startTime = System.currentTimeMillis();
|
||||
// BackupFileResLog backup = backupTask.backup(fileSysPath, edFileInfo.getId());
|
||||
// long endTime = System.currentTimeMillis();
|
||||
// fileBackupLog.setStartTime(startTime)
|
||||
// .setEndTime(endTime)
|
||||
// .setDuration(endTime - startTime)
|
||||
// .setFileTime(FileUtil.lastModifiedTime(fileSysPath).getTime())
|
||||
// .setCreateTime(new Date())
|
||||
// .setFileCode(commonService.getFileCode(fileSysPath))
|
||||
// .setFileCreateTime(edFileInfo.getCreatedTime())
|
||||
// .setFileName(saveFileName);
|
||||
// if (backup.getBackupSuccess()) {
|
||||
// fileBackupLog.setBackupSuccess(true);
|
||||
// fileBackupLogMapper.update(fileBackupLog, null);
|
||||
// } else {
|
||||
// fileBackupLog.setBackupSuccess(false);
|
||||
// fileBackupLog.setFailInfoDetail(backup.getFailInfoDetail());
|
||||
// fileBackupLogMapper.update(fileBackupLog, null);
|
||||
// throw new BizException(StrFormatter.format("删除文件 {} 失败,原因 备份该文件出现错误,联系管理员查看日志", saveFileName));
|
||||
// }
|
||||
// }
|
||||
// fileSysPaths.add(fileSysPath);
|
||||
}
|
||||
|
||||
// 移动到tmp目录,七天后删除
|
||||
for (String fileSysPath : fileSysPaths) {
|
||||
fileSystemService.moveFile(fileSysPath, elePropertyConfig.getEleTmpPath() + File.separator + new File(fileSysPath).getName());
|
||||
}
|
||||
// for (String fileSysPath : fileSysPaths) {
|
||||
//
|
||||
// }
|
||||
|
||||
// 更新MySQL数据库
|
||||
this.edFileInfoMapper.update(new EdFileInfo(), Wrappers.lambdaUpdate(EdFileInfo.class)
|
||||
|
|
|
|||
|
|
@ -44,6 +44,8 @@ public class UserAccessLogServiceImpl extends ServiceImpl<UserAccessLogMapper, U
|
|||
LambdaQueryWrapper<UserAccessLog> queryWrapper = Wrappers.lambdaQuery(UserAccessLog.class);
|
||||
if (!adminQuery) {
|
||||
queryWrapper.eq(UserAccessLog::getDataId, pars.getDataId());
|
||||
} else {
|
||||
queryWrapper.ne(UserAccessLog::getOperationModule, UserOperationModuleEnum.TMP.key);
|
||||
}
|
||||
|
||||
if (StrUtil.isNotEmpty(pars.getKeyWord())) {
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ public class BackupTask {
|
|||
.eq(EdFileInfo::getId, id));
|
||||
String sysFilePath = commonService.getFileSysPath(fileInfo.getFilePath(), fileInfo.getDataOwn());
|
||||
long startTime = System.currentTimeMillis();
|
||||
BackupFileResLog resLog = backup(sysFilePath);
|
||||
BackupFileResLog resLog = backup(sysFilePath, id);
|
||||
long endTime = System.currentTimeMillis();
|
||||
fileBackupLogMapper.update(null, Wrappers.<FileBackupLog>lambdaUpdate()
|
||||
.eq(FileBackupLog::getFileId, id)
|
||||
|
|
@ -95,7 +95,7 @@ public class BackupTask {
|
|||
}
|
||||
String fileSysPath = commonService.getFileSysPath(edFileInfo.getFilePath(), edFileInfo.getDataOwn());
|
||||
long startTime = System.currentTimeMillis();
|
||||
BackupFileResLog resLog = backup(fileSysPath);
|
||||
BackupFileResLog resLog = backup(fileSysPath, edFileInfo.getId());
|
||||
long endTime = System.currentTimeMillis();
|
||||
FileBackupLog backupLog = new FileBackupLog()
|
||||
.setId(IdWorker.getSnowFlakeIdString())
|
||||
|
|
@ -163,9 +163,10 @@ public class BackupTask {
|
|||
}
|
||||
}
|
||||
|
||||
public BackupFileResLog backup(String filePath) {
|
||||
public BackupFileResLog backup(String filePath, String id) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("file", new File(filePath));
|
||||
map.put("id", id);
|
||||
String url = StrFormatter.format("http://{}:{}/data/file/backup/upload", elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort());
|
||||
String res = HttpUtil.post(url, map);
|
||||
ElectromagneticResult<?> resObj = JSONUtil.toBean(res, ElectromagneticResult.class);
|
||||
|
|
|
|||
|
|
@ -40,3 +40,5 @@ prj.folder.max.length=6
|
|||
tmp.file.store.days=7
|
||||
backup.remote.host=127.0.0.1
|
||||
backup.remote.port=1111
|
||||
|
||||
backup.mysql.path=/workspace/mysqlbak/test
|
||||
Loading…
Reference in New Issue