|
@@ -30,16 +30,22 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
|
|
|
|
|
|
@Override
|
|
|
public void addFileToDatabase(FileUpdateInfo fileUpdateInfo) throws Exception {
|
|
|
- String filePath = fileUpdateInfo.getFilePath();
|
|
|
+ String filePath = fileUpdateInfo.getFilePath(); // 使用新的 getFilePath 方法
|
|
|
File file = new File(filePath);
|
|
|
- if (!file.exists()) {
|
|
|
- throw new FileNotFoundException("文件不存在:" + filePath);
|
|
|
+
|
|
|
+ if (file.exists()) {
|
|
|
+ // 如果文件存在,计算本地文件的 MD5 值
|
|
|
+ String fileMd5 = DigestUtils.md5Hex(new FileInputStream(file));
|
|
|
+ fileUpdateInfo.setFileMd5(fileMd5);
|
|
|
+ fileUpdateInfo.setUpdateStatus("待检查文件版本");
|
|
|
+ } else {
|
|
|
+ // 如果文件不存在,设置状态为“待更新”
|
|
|
+ fileUpdateInfo.setFileMd5(null);
|
|
|
+ fileUpdateInfo.setUpdateStatus("待更新");
|
|
|
}
|
|
|
|
|
|
- String fileMd5 = DigestUtils.md5Hex(new FileInputStream(file));
|
|
|
- fileUpdateInfo.setFileMd5(fileMd5);
|
|
|
+ fileUpdateInfo.setVersion("v1.0.0"); // 初始化版本号
|
|
|
fileUpdateInfo.setUpdateTime(new Date());
|
|
|
- fileUpdateInfo.setUpdateStatus("待检查文件版本");
|
|
|
fileUpdateInfoMapper.insert(fileUpdateInfo);
|
|
|
}
|
|
|
|
|
@@ -50,18 +56,26 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
|
|
|
throw new FileNotFoundException("文件信息未找到:id=" + id);
|
|
|
}
|
|
|
|
|
|
- String localFilePath = fileUpdateInfo.getFilePath();
|
|
|
- String localFileMd5 = fileUpdateInfo.getFileMd5();
|
|
|
- String fileName = fileUpdateInfo.getFileName();
|
|
|
- String remoteFileMd5 = getRemoteFileMd5(fileName);
|
|
|
+ String localFilePath = fileUpdateInfo.getFilePath(); // 使用新的 getFilePath 方法
|
|
|
+ File localFile = new File(localFilePath);
|
|
|
|
|
|
- boolean isUpdateRequired = !localFileMd5.equals(remoteFileMd5);
|
|
|
- fileUpdateInfo.setRemoteMd5(remoteFileMd5);
|
|
|
- fileUpdateInfo.setUpdateTime(new Date());
|
|
|
- fileUpdateInfo.setUpdateStatus(isUpdateRequired ? "待更新" : "无需更新");
|
|
|
- fileUpdateInfoMapper.updateById(fileUpdateInfo);
|
|
|
+ String remoteFileMd5 = getRemoteFileMd5(fileUpdateInfo.getFileName());
|
|
|
|
|
|
- return isUpdateRequired;
|
|
|
+ if (!localFile.exists()) {
|
|
|
+ // 如果本地文件不存在,设置状态为“待更新”
|
|
|
+ fileUpdateInfo.setRemoteMd5(remoteFileMd5);
|
|
|
+ fileUpdateInfo.setUpdateStatus("待更新");
|
|
|
+ fileUpdateInfoMapper.updateById(fileUpdateInfo);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ // 如果本地文件存在,比较 MD5 值
|
|
|
+ String localFileMd5 = DigestUtils.md5Hex(new FileInputStream(localFile));
|
|
|
+ boolean isUpdateRequired = !localFileMd5.equals(remoteFileMd5);
|
|
|
+ fileUpdateInfo.setRemoteMd5(remoteFileMd5);
|
|
|
+ fileUpdateInfo.setUpdateStatus(isUpdateRequired ? "待更新" : "无需更新");
|
|
|
+ fileUpdateInfoMapper.updateById(fileUpdateInfo);
|
|
|
+ return isUpdateRequired;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -71,12 +85,12 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
|
|
|
throw new FileNotFoundException("文件信息未找到:id=" + id);
|
|
|
}
|
|
|
|
|
|
- String localFilePath = fileUpdateInfo.getFilePath();
|
|
|
+ String localFilePath = fileUpdateInfo.getFilePath(); // 使用新的 getFilePath 方法
|
|
|
String fileName = fileUpdateInfo.getFileName();
|
|
|
String remoteUrl = remoteUrlBase + fileName;
|
|
|
|
|
|
try {
|
|
|
- String backupDirPath = localFilePath + "-backup";
|
|
|
+ String backupDirPath = System.getProperty("user.dir") + File.separator + "backup";
|
|
|
File backupDir = new File(backupDirPath);
|
|
|
if (!backupDir.exists()) {
|
|
|
boolean created = backupDir.mkdirs();
|
|
@@ -99,6 +113,15 @@ public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
|
|
|
fileUpdateInfo.setFileMd5(newFileMd5);
|
|
|
fileUpdateInfo.setUpdateTime(new Date());
|
|
|
fileUpdateInfo.setUpdateStatus("已更新");
|
|
|
+
|
|
|
+ // 递增版本号
|
|
|
+ String currentVersion = fileUpdateInfo.getVersion();
|
|
|
+ String[] versionParts = currentVersion.substring(1).split("\\."); // 去掉 'v' 并拆分版本号
|
|
|
+ int major = Integer.parseInt(versionParts[0]);
|
|
|
+ int minor = Integer.parseInt(versionParts[1]);
|
|
|
+ int patch = Integer.parseInt(versionParts[2]) + 1; // 小版本号加 1
|
|
|
+ fileUpdateInfo.setVersion("v" + major + "." + minor + "." + patch);
|
|
|
+
|
|
|
fileUpdateInfoMapper.updateById(fileUpdateInfo);
|
|
|
} catch (FileNotFoundException e) {
|
|
|
throw new Exception("文件未找到: " + localFilePath + ",原因: " + e.getMessage(), e);
|