|
@@ -1,9 +1,11 @@
|
|
|
-package com.ruoyi.file.service;
|
|
|
+package com.ruoyi.file.service.impl;
|
|
|
|
|
|
-import com.ruoyi.file.model.FileUpdateInfo;
|
|
|
-import com.ruoyi.file.repository.FileUpdateInfoRepository;
|
|
|
+import com.ruoyi.file.domain.FileUpdateInfo;
|
|
|
+import com.ruoyi.file.mapper.FileUpdateInfoMapper;
|
|
|
+import com.ruoyi.file.service.FileUpdateInfoService;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.*;
|
|
@@ -14,70 +16,66 @@ import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Service
|
|
|
-public class FileUpdateService {
|
|
|
+public class FileUpdateInfoServiceImpl implements FileUpdateInfoService {
|
|
|
|
|
|
@Autowired
|
|
|
- private FileUpdateInfoRepository fileUpdateInfoRepository;
|
|
|
+ private FileUpdateInfoMapper fileUpdateInfoMapper;
|
|
|
|
|
|
- public void addFileToDatabase(FileUpdateRequest request) throws Exception {
|
|
|
- String fileName = request.getFileName();
|
|
|
- String filePath = request.getFilePath();
|
|
|
+ @Value("${file.remote-url}")
|
|
|
+ private String remoteUrlBase;
|
|
|
|
|
|
+ @Override
|
|
|
+ public void addFileToDatabase(FileUpdateInfo fileUpdateInfo) throws Exception {
|
|
|
+ String filePath = fileUpdateInfo.getFilePath();
|
|
|
File file = new File(filePath);
|
|
|
if (!file.exists()) {
|
|
|
throw new FileNotFoundException("文件不存在:" + filePath);
|
|
|
}
|
|
|
|
|
|
String fileMd5 = DigestUtils.md5Hex(new FileInputStream(file));
|
|
|
- FileUpdateInfo fileUpdateInfo = new FileUpdateInfo();
|
|
|
- fileUpdateInfo.setFileName(fileName);
|
|
|
- fileUpdateInfo.setFilePath(filePath);
|
|
|
fileUpdateInfo.setFileMd5(fileMd5);
|
|
|
fileUpdateInfo.setUpdateTime(new Date());
|
|
|
fileUpdateInfo.setUpdateStatus("待检查文件版本");
|
|
|
- fileUpdateInfoRepository.save(fileUpdateInfo);
|
|
|
+ fileUpdateInfoMapper.insert(fileUpdateInfo);
|
|
|
}
|
|
|
|
|
|
- // 检查文件是否需要更新
|
|
|
+ @Override
|
|
|
public boolean checkFileUpdate(Long id) throws Exception {
|
|
|
- FileUpdateInfo fileUpdateInfo = fileUpdateInfoRepository.findById(id).orElse(null);
|
|
|
+ FileUpdateInfo fileUpdateInfo = fileUpdateInfoMapper.selectById(id);
|
|
|
if (fileUpdateInfo == null) {
|
|
|
throw new FileNotFoundException("文件信息未找到:id=" + id);
|
|
|
}
|
|
|
|
|
|
String localFilePath = fileUpdateInfo.getFilePath();
|
|
|
String localFileMd5 = fileUpdateInfo.getFileMd5();
|
|
|
- String fileName = fileUpdateInfo.getFileName(); // 获取文件名
|
|
|
- String remoteFileMd5 = getRemoteFileMd5(fileName); // 获取远程文件的 MD5 值
|
|
|
+ String fileName = fileUpdateInfo.getFileName();
|
|
|
+ String remoteFileMd5 = getRemoteFileMd5(fileName);
|
|
|
|
|
|
boolean isUpdateRequired = !localFileMd5.equals(remoteFileMd5);
|
|
|
- fileUpdateInfo.setRemoteMd5(remoteFileMd5); // 更新远程文件的 MD5 值
|
|
|
+ fileUpdateInfo.setRemoteMd5(remoteFileMd5);
|
|
|
fileUpdateInfo.setUpdateTime(new Date());
|
|
|
fileUpdateInfo.setUpdateStatus(isUpdateRequired ? "待更新" : "无需更新");
|
|
|
- fileUpdateInfoRepository.save(fileUpdateInfo);
|
|
|
+ fileUpdateInfoMapper.updateById(fileUpdateInfo);
|
|
|
|
|
|
return isUpdateRequired;
|
|
|
}
|
|
|
|
|
|
- // 执行文件更新
|
|
|
+ @Override
|
|
|
public void performFileUpdate(Long id) throws Exception {
|
|
|
- FileUpdateInfo fileUpdateInfo = fileUpdateInfoRepository.findById(id).orElse(null);
|
|
|
+ FileUpdateInfo fileUpdateInfo = fileUpdateInfoMapper.selectById(id);
|
|
|
if (fileUpdateInfo == null) {
|
|
|
throw new FileNotFoundException("文件信息未找到:id=" + id);
|
|
|
}
|
|
|
|
|
|
String localFilePath = fileUpdateInfo.getFilePath();
|
|
|
- String fileName = fileUpdateInfo.getFileName(); // 获取文件名
|
|
|
- String remoteUrl = "https://file.usky.cn/uskycloud/" + fileName; // 替换为实际的远程文件 URL
|
|
|
+ String fileName = fileUpdateInfo.getFileName();
|
|
|
+ String remoteUrl = remoteUrlBase + fileName;
|
|
|
|
|
|
try {
|
|
|
- // 检查并创建备份目录
|
|
|
String backupDirPath = localFilePath + "-backup";
|
|
|
File backupDir = new File(backupDirPath);
|
|
|
if (!backupDir.exists()) {
|
|
@@ -87,7 +85,6 @@ public class FileUpdateService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 为旧文件添加时间戳并移动到备份目录
|
|
|
File originalFile = new File(localFilePath);
|
|
|
if (originalFile.exists()) {
|
|
|
String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
|
@@ -96,15 +93,13 @@ public class FileUpdateService {
|
|
|
System.out.println("旧文件已备份到: " + backupFilePath);
|
|
|
}
|
|
|
|
|
|
- // 下载远程文件
|
|
|
downloadFileFromRemote(remoteUrl, localFilePath);
|
|
|
|
|
|
- // 更新数据库中的文件信息
|
|
|
String newFileMd5 = DigestUtils.md5Hex(new FileInputStream(localFilePath));
|
|
|
fileUpdateInfo.setFileMd5(newFileMd5);
|
|
|
fileUpdateInfo.setUpdateTime(new Date());
|
|
|
fileUpdateInfo.setUpdateStatus("已更新");
|
|
|
- fileUpdateInfoRepository.save(fileUpdateInfo);
|
|
|
+ fileUpdateInfoMapper.updateById(fileUpdateInfo);
|
|
|
} catch (FileNotFoundException e) {
|
|
|
throw new Exception("文件未找到: " + localFilePath + ",原因: " + e.getMessage(), e);
|
|
|
} catch (IOException e) {
|
|
@@ -126,9 +121,8 @@ public class FileUpdateService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private String getRemoteFileMd5(String fileName) throws Exception {
|
|
|
- String remoteUrl = "https://file.usky.cn/uskycloud/" + fileName;
|
|
|
+ String remoteUrl = remoteUrlBase + fileName;
|
|
|
System.out.println("远程文件 URL: " + remoteUrl);
|
|
|
URL url = new URL(remoteUrl);
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
@@ -137,4 +131,27 @@ public class FileUpdateService {
|
|
|
return DigestUtils.md5Hex(in);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<FileUpdateInfo> getAllFiles() {
|
|
|
+ return fileUpdateInfoMapper.selectList(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<FileUpdateInfo> getFilesByFileNameContaining(String fileName) {
|
|
|
+ return fileUpdateInfoMapper.selectList(
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<FileUpdateInfo>()
|
|
|
+ .like("file_name", fileName)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public FileUpdateInfo getFileById(Long id) {
|
|
|
+ return fileUpdateInfoMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteFileById(Long id) {
|
|
|
+ fileUpdateInfoMapper.deleteById(id);
|
|
|
+ }
|
|
|
}
|