文件备份的适配,屏蔽文件删除相关的功能。

This commit is contained in:
chenxudong 2025-04-16 12:06:19 +08:00
parent 2c7b0eea0a
commit cb4cfe1618
5 changed files with 41 additions and 41 deletions

View File

@ -31,10 +31,10 @@ public class FileController {
private BackupPro backupPro;
@RequestMapping("/upload")
public ElectromagneticResult<?> upload(@RequestParam("file") MultipartFile file, @RequestParam("id") String id) {
public ElectromagneticResult<?> upload(@RequestParam("file") MultipartFile file) {
BackupFileResLog backupFileResLog = BackupFileResLog.builder().backupStartTime(new Date()).fileName(file.getOriginalFilename()).backupSuccess(true).build();
try {
fileService.upload(file, id);
fileService.upload(file);
} catch (Exception e) {
String details = ExceptionUtil.stacktraceToString(e);
backupFileResLog.setBackupSuccess(false);
@ -53,20 +53,20 @@ public class FileController {
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("/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);
public ResponseEntity<InputStreamResource> download(@RequestParam("id") String id, @RequestParam("uuid") String uuid) throws Exception {
return fileService.download(id, uuid);
}
@RequestMapping("/backupSql")

View File

@ -8,11 +8,11 @@ import java.io.IOException;
public interface FileService {
void upload(MultipartFile file, String id) throws IOException;
void upload(MultipartFile file) throws IOException;
void remove(String id);
// void remove(String id);
ResponseEntity<InputStreamResource> download(String id) throws Exception;
ResponseEntity<InputStreamResource> download(String id, String uuid) throws Exception;
void backupSql(MultipartFile file) throws Exception;
}

View File

@ -26,22 +26,23 @@ public class FileServiceImpl implements FileService {
private BackupPro backupPro;
@Override
public void upload(MultipartFile file, String id) throws IOException {
String destPath = getFileSysPathById(id);
public void upload(MultipartFile file) throws IOException {
String name = FileUtil.mainName(file.getOriginalFilename());
String destPath = getFileSysPathById(name);
if (!FileUtil.exist(destPath)) {
FileUtil.writeFromStream(file.getInputStream(), destPath);
}
}
@Override
public void remove(String id) {
String destPath = getFileSysPathById(id);
FileUtil.del(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);
public ResponseEntity<InputStreamResource> download(String id, String uuid) throws Exception {
String destPath = getFileSysPathById(id + "_" + uuid);
FileSystemResource fileSystemResource = new FileSystemResource(destPath);
return ResponseEntity
.ok()
@ -63,9 +64,9 @@ public class FileServiceImpl implements FileService {
FileUtil.del(destPath);
}
private String getFileSysPathById(String id) {
private String getFileSysPathById(String fileName) {
String saveFolder = backupPro.getSaveFolder();
return saveFolder + File.separator + "prj_files" + File.separator + id;
return saveFolder + File.separator + "prj_files" + File.separator + fileName;
}
}

View File

@ -95,7 +95,7 @@ public class FileBackLogServiceImpl extends ServiceImpl<FileBackupLogMapper, Fil
destPath = destPath.replace("//", "/");
log.info("back up file path is {}", destPath);
if (!FileUtil.exist(destPath)) {
byte[] bytes = backupHandler.downloadFile(edFileInfo.getId());
byte[] bytes = backupHandler.downloadFile(edFileInfo.getId(), edFileInfo.getUuid());
FileUtil.writeBytes(bytes, destPath);
String dbPath = commonService.getDbPath(edFileInfo.getFilePath());
UserThreadLocal.setSuccessInfo(edFileInfo.getParentId(), edFileInfo.getId(), "{} 恢复成功,路径为 {}", edFileInfo.getFileName() + "." + edFileInfo.getFileType(), dbPath);

View File

@ -24,7 +24,6 @@ public class BackupHandler {
public BackupFileResLog backupFiles(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());
log.info("back up url is {}", url);
String res = HttpUtil.post(url, map);
@ -43,18 +42,18 @@ public class BackupHandler {
return JSONUtil.toBean(data, BackupFileResLog.class);
}
public BackupFileResLog deleteFile(String id) {
Map<String, Object> map = new HashMap<>();
map.put("id", id);
String url = StrFormatter.format("http://{}:{}/data/file/backup/remove", elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort());
String res = HttpUtil.get(url, map);
ElectromagneticResult<?> resObj = JSONUtil.toBean(res, ElectromagneticResult.class);
String data = JSONUtil.toJsonStr(resObj.getData());
return JSONUtil.toBean(data, BackupFileResLog.class);
}
// public BackupFileResLog deleteFile(String id) {
// Map<String, Object> map = new HashMap<>();
// map.put("id", id);
// String url = StrFormatter.format("http://{}:{}/data/file/backup/remove", elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort());
// String res = HttpUtil.get(url, map);
// ElectromagneticResult<?> resObj = JSONUtil.toBean(res, ElectromagneticResult.class);
// String data = JSONUtil.toJsonStr(resObj.getData());
// return JSONUtil.toBean(data, BackupFileResLog.class);
// }
public byte[] downloadFile(String id) {
String url = StrFormatter.format("http://{}:{}/data/file/backup/download?id={}", elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort(), id);
public byte[] downloadFile(String id, String uuid) {
String url = StrFormatter.format("http://{}:{}/data/file/backup/download?id={}&&uuid=", elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort(), id, uuid);
return HttpUtil.downloadBytes(url);
}
}