对话流实现 文件上传
This commit is contained in:
@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import java.io.File;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.xyzh.api.file.FileService;
|
||||
import org.xyzh.common.core.domain.ResultDomain;
|
||||
@@ -371,5 +372,120 @@ public class FileServiceImpl implements FileService {
|
||||
return resultDomain;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<String> saveTempFile(MultipartFile file, String module) {
|
||||
ResultDomain<String> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
if (file == null || file.isEmpty()) {
|
||||
resultDomain.fail("文件不能为空");
|
||||
return resultDomain;
|
||||
}
|
||||
|
||||
// 获取存储策略
|
||||
FileStorageStrategy strategy = strategyFactory.getDefaultStrategy();
|
||||
|
||||
// 生成唯一文件名
|
||||
String originalFileName = file.getOriginalFilename();
|
||||
String fileExtension = "";
|
||||
if (originalFileName != null && originalFileName.contains(".")) {
|
||||
fileExtension = originalFileName.substring(originalFileName.lastIndexOf("."));
|
||||
}
|
||||
String fileName = UUID.randomUUID().toString().replace("-", "") + fileExtension;
|
||||
|
||||
// 上传到临时目录
|
||||
String tempModule = "temp/" + module;
|
||||
String relativePath = strategy.upload(file, fileName, tempModule);
|
||||
|
||||
// 转换为绝对路径(用于后续文件操作)
|
||||
String absolutePath = strategy.getAbsolutePath(relativePath);
|
||||
|
||||
log.info("临时文件保存成功: relativePath={}, absolutePath={}, originalName={}",
|
||||
relativePath, absolutePath, originalFileName);
|
||||
|
||||
resultDomain.success("临时文件保存成功", absolutePath);
|
||||
return resultDomain;
|
||||
} catch (Exception e) {
|
||||
log.error("保存临时文件失败", e);
|
||||
resultDomain.fail("保存临时文件失败: " + e.getMessage());
|
||||
return resultDomain;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultDomain<Boolean> deleteTempFile(String tempFilePath) {
|
||||
ResultDomain<Boolean> resultDomain = new ResultDomain<>();
|
||||
|
||||
try {
|
||||
if (tempFilePath == null || tempFilePath.isEmpty()) {
|
||||
resultDomain.fail("文件路径不能为空");
|
||||
return resultDomain;
|
||||
}
|
||||
|
||||
// 获取存储策略
|
||||
FileStorageStrategy strategy = strategyFactory.getDefaultStrategy();
|
||||
|
||||
// 判断是绝对路径还是相对路径
|
||||
File file = new File(tempFilePath);
|
||||
|
||||
if (file.isAbsolute()) {
|
||||
// 如果是绝对路径,直接删除文件
|
||||
if (file.exists()) {
|
||||
boolean deleted = file.delete();
|
||||
if (deleted) {
|
||||
log.info("临时文件删除成功(绝对路径): filePath={}", tempFilePath);
|
||||
resultDomain.success("临时文件删除成功", true);
|
||||
} else {
|
||||
log.warn("临时文件删除失败(绝对路径): filePath={}", tempFilePath);
|
||||
resultDomain.success("文件删除失败", false);
|
||||
}
|
||||
} else {
|
||||
log.warn("临时文件不存在(绝对路径): filePath={}", tempFilePath);
|
||||
resultDomain.success("文件不存在或已删除", false);
|
||||
}
|
||||
} else {
|
||||
// 如果是相对路径,使用策略删除
|
||||
boolean deleted = strategy.delete(tempFilePath);
|
||||
|
||||
if (deleted) {
|
||||
log.info("临时文件删除成功(相对路径): filePath={}", tempFilePath);
|
||||
resultDomain.success("临时文件删除成功", true);
|
||||
} else {
|
||||
log.warn("临时文件删除失败,可能文件不存在(相对路径): filePath={}", tempFilePath);
|
||||
resultDomain.success("文件不存在或已删除", false);
|
||||
}
|
||||
}
|
||||
|
||||
return resultDomain;
|
||||
} catch (Exception e) {
|
||||
log.error("删除临时文件失败: filePath={}", tempFilePath, e);
|
||||
resultDomain.fail("删除临时文件失败: " + e.getMessage());
|
||||
return resultDomain;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getFileByRelativePath(String relativePath) {
|
||||
if (relativePath == null || relativePath.isEmpty()) {
|
||||
log.warn("相对路径为空,无法获取文件对象");
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
FileStorageStrategy strategy = strategyFactory.getDefaultStrategy();
|
||||
String absolutePath = strategy.getAbsolutePath(relativePath);
|
||||
File file = new File(absolutePath);
|
||||
|
||||
if (!file.exists()) {
|
||||
log.warn("文件不存在: relativePath={}, absolutePath={}", relativePath, absolutePath);
|
||||
}
|
||||
|
||||
return file;
|
||||
} catch (Exception e) {
|
||||
log.error("获取文件对象失败: relativePath={}", relativePath, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -61,5 +61,14 @@ public interface FileStorageStrategy {
|
||||
* @since 2025-10-16
|
||||
*/
|
||||
String getStorageType();
|
||||
|
||||
/**
|
||||
* @description 获取文件的绝对路径(用于临时文件)
|
||||
* @param relativePath 相对路径
|
||||
* @return String 绝对路径
|
||||
* @author AI Assistant
|
||||
* @since 2025-11-06
|
||||
*/
|
||||
String getAbsolutePath(String relativePath);
|
||||
}
|
||||
|
||||
|
||||
@@ -76,5 +76,10 @@ public class LocalFileStorageStrategy implements FileStorageStrategy {
|
||||
public String getStorageType() {
|
||||
return "local";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAbsolutePath(String relativePath) {
|
||||
return basePath + File.separator + relativePath;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -137,5 +137,10 @@ public class MinIOFileStorageStrategy implements FileStorageStrategy {
|
||||
public String getStorageType() {
|
||||
return "minio";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAbsolutePath(String relativePath) {
|
||||
return relativePath;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user