1.修改创建文件夹和上传文件的bug。
This commit is contained in:
parent
143389f52b
commit
9ed6a64a9e
|
|
@ -38,3 +38,7 @@ data.export.cache.dir=export
|
|||
#导入数据时文件的缓存文件夹名称
|
||||
data.import.cache.dir=import
|
||||
file.encode.passwd=adknfhkj87654knd
|
||||
#数据类型中的文件类型为file
|
||||
data.type.file=file
|
||||
#数据类型中的文件夹类型为folder
|
||||
data.type.folder=folder
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ public interface EDDataService {
|
|||
* @param edDataInfo
|
||||
* @return
|
||||
*/
|
||||
Boolean createDataInfo(EDDataInfo edDataInfo);
|
||||
Boolean createDataInfo(EDDataInfo edDataInfo) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* 获取文件信息列表
|
||||
|
|
@ -69,7 +69,7 @@ public interface EDDataService {
|
|||
* @param parames
|
||||
* @return
|
||||
*/
|
||||
Boolean uploadFile(EDDataParams parames);
|
||||
Boolean uploadFile(EDDataParams parames) throws FileNotFoundException;
|
||||
|
||||
/**
|
||||
* 文件下载
|
||||
|
|
|
|||
|
|
@ -71,6 +71,11 @@ public class EDDataServiceImpl implements EDDataService {
|
|||
private String exportCacheDir;
|
||||
@Value("${file.encode.passwd}")
|
||||
private String encodePasswd;
|
||||
@Value("${data.type.file}")
|
||||
private String dataTypeFile;
|
||||
@Value("${data.type.folder}")
|
||||
private String dataTypeFolder;
|
||||
|
||||
|
||||
// 文件夹名称分隔符
|
||||
private static final String FOLDER_NAME_SEPARATOR = "_";
|
||||
|
|
@ -140,15 +145,14 @@ public class EDDataServiceImpl implements EDDataService {
|
|||
* @param edDataInfo
|
||||
* @return
|
||||
*/
|
||||
public Boolean createDataInfo(EDDataInfo edDataInfo)
|
||||
public Boolean createDataInfo(EDDataInfo edDataInfo) throws FileNotFoundException
|
||||
{
|
||||
// 获取上级目录的名称
|
||||
Category categoryParent = new Category();
|
||||
categoryParent.setCategoryId(edDataInfo.getCategoryId());
|
||||
List<Category> categoryParentList = categoryRepository.selectCategories(categoryParent);
|
||||
if(categoryParentList.size() < 1)
|
||||
{
|
||||
return Boolean.FALSE;
|
||||
if(categoryParentList.size() < 1) {
|
||||
throw new FileNotFoundException("上级文件夹不存在");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -165,8 +169,8 @@ public class EDDataServiceImpl implements EDDataService {
|
|||
List<EDDataInfo> childFileInfoList = edDataRepository.getDataInfoList(folderParames);
|
||||
for(EDDataInfo fileInfo : childFileInfoList)
|
||||
{
|
||||
if(fileInfo.getDataName() == edDataInfo.getDataName()){
|
||||
return Boolean.FALSE;
|
||||
if(fileInfo.getDataName().equals(edDataInfo.getDataName())){
|
||||
throw new FileNotFoundException("文件夹已存在");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -243,7 +247,7 @@ public class EDDataServiceImpl implements EDDataService {
|
|||
// 修改文件夹
|
||||
edDataRepository.updateFileInfo(parames);
|
||||
// 修改文件夹中的文件
|
||||
if(edDataInfo.getDataType().equals("folder") && parames.getEffectFlag() != null) {
|
||||
if(edDataInfo.getDataType().equals(dataTypeFolder) && parames.getEffectFlag() != null) {
|
||||
EDDataParams paramesChild = new EDDataParams();
|
||||
paramesChild.setParentId(edDataInfo.getDataId());
|
||||
paramesChild.setEffectFlag(parames.getEffectFlag());
|
||||
|
|
@ -265,12 +269,12 @@ public class EDDataServiceImpl implements EDDataService {
|
|||
List<EDDataInfo> edDataInfoList = edDataRepository.getDataInfoList(parames);
|
||||
parames.setDataId(null);
|
||||
for (EDDataInfo edDataInfo : edDataInfoList) {
|
||||
if(edDataInfo.getDataType().equals("folder"))
|
||||
if(edDataInfo.getDataType().equals(dataTypeFolder))
|
||||
{
|
||||
parames.setParentId(edDataInfo.getDataId());
|
||||
childFileCount += getChildFileCount(parames);
|
||||
}
|
||||
else if(edDataInfo.getDataType().equals("file"))
|
||||
else if(edDataInfo.getDataType().equals(dataTypeFile))
|
||||
{
|
||||
++childFileCount;
|
||||
}
|
||||
|
|
@ -284,7 +288,7 @@ public class EDDataServiceImpl implements EDDataService {
|
|||
* @param parames
|
||||
* @return
|
||||
*/
|
||||
public Boolean uploadFile(EDDataParams parames)
|
||||
public Boolean uploadFile(EDDataParams parames) throws FileNotFoundException
|
||||
{
|
||||
|
||||
// 获取目录编码ID
|
||||
|
|
@ -295,7 +299,7 @@ public class EDDataServiceImpl implements EDDataService {
|
|||
|
||||
// 检查文件是否为空
|
||||
if (fileInput == null || fileInput.isEmpty()) {
|
||||
return Boolean.FALSE;
|
||||
throw new FileNotFoundException("上传的文件为空");
|
||||
}
|
||||
|
||||
// 获取文件名
|
||||
|
|
@ -309,6 +313,17 @@ public class EDDataServiceImpl implements EDDataService {
|
|||
fileName = fileFullName.substring(fileFullName.lastIndexOf("."));
|
||||
}
|
||||
|
||||
// 判断文件名称是否存在
|
||||
EDDataParams folderParames = new EDDataParams();
|
||||
folderParames.setParentId(categoryId);
|
||||
List<EDDataInfo> childFileInfoList = edDataRepository.getDataInfoList(folderParames);
|
||||
for(EDDataInfo fileInfo : childFileInfoList)
|
||||
{
|
||||
if(fileInfo.getDataName().equals(fileFullName)){
|
||||
throw new FileNotFoundException("上传的文件已存在");
|
||||
}
|
||||
}
|
||||
|
||||
String dataStoragePath = getDataStoragePath();
|
||||
if (!FileUtil.exist(dataStoragePath)){
|
||||
FileUtil.mkdir(dataStoragePath);
|
||||
|
|
@ -319,91 +334,88 @@ public class EDDataServiceImpl implements EDDataService {
|
|||
|
||||
// 将文件数据信息写到数据库
|
||||
EDDataInfo edDataInfo = new EDDataInfo();
|
||||
{
|
||||
// 临时数据设置
|
||||
if (Boolean.TRUE) {
|
||||
if (parames.getUserId() == null || parames.getUserId().isEmpty()) parames.setUserId(IdWorker.getSnowFlakeIdString());
|
||||
if (parames.getUserName() == null || parames.getUserName().isEmpty()) parames.setUserName("user");
|
||||
}
|
||||
|
||||
// 创建新文件数据
|
||||
edDataInfo.setCreator(parames.getUserId());
|
||||
edDataInfo.setCreatorName(parames.getUserName());
|
||||
edDataInfo.setModifier(parames.getUserId());
|
||||
edDataInfo.setModifierName(parames.getUserName());
|
||||
edDataInfo.setCategoryId(parames.getParentId());
|
||||
edDataInfo.setDataName(fileFullName);
|
||||
edDataInfo.setNote(parames.getNote());
|
||||
edDataInfo.setFileType(fileType);
|
||||
edDataInfo.setGmtBatchUpload(parames.getGmtBatchUpload());
|
||||
|
||||
edDataInfo.setDataId(IdWorker.getSnowFlakeIdString());
|
||||
edDataInfo.setDataNo(edDataInfo.getDataId());
|
||||
edDataInfo.setDataType("file");
|
||||
edDataInfo.setVersion("1.0.0");
|
||||
edDataInfo.setDataStatus("publish");
|
||||
edDataInfo.setSaveStatus("saving");
|
||||
// 创建新文件数据
|
||||
edDataInfo.setCreator(parames.getUserId());
|
||||
edDataInfo.setCreatorName(parames.getUserName());
|
||||
edDataInfo.setModifier(parames.getUserId());
|
||||
edDataInfo.setModifierName(parames.getUserName());
|
||||
edDataInfo.setCategoryId(parames.getParentId());
|
||||
edDataInfo.setDataName(fileFullName);
|
||||
edDataInfo.setNote(parames.getNote());
|
||||
edDataInfo.setFileType(fileType);
|
||||
edDataInfo.setGmtBatchUpload(parames.getGmtBatchUpload());
|
||||
|
||||
boolean isSuccess = edDataRepository.createDataInfo(edDataInfo);
|
||||
}
|
||||
edDataInfo.setDataId(IdWorker.getSnowFlakeIdString());
|
||||
edDataInfo.setDataNo(edDataInfo.getDataId());
|
||||
edDataInfo.setDataType(dataTypeFile);
|
||||
edDataInfo.setVersion("1.0.0");
|
||||
edDataInfo.setDataStatus("publish");
|
||||
edDataInfo.setSaveStatus("saving");
|
||||
|
||||
boolean isSuccess = edDataRepository.createDataInfo(edDataInfo);
|
||||
log.info("文件开始保存.");
|
||||
|
||||
// 保存文件数据 到 文件存储目录
|
||||
{
|
||||
|
||||
// 文件保存目录路径
|
||||
String fileSavePath = dataStoragePath + File.separator + filePathOfFolder;
|
||||
String newFileName = edDataInfo.getDataId() + FOLDER_NAME_SEPARATOR + fileFullName;
|
||||
if (!FileUtil.exist(fileSavePath)){
|
||||
FileUtil.mkdir(fileSavePath);
|
||||
}
|
||||
|
||||
String dataCachePath = getDataCachePath();
|
||||
String uploadFileCachePath = dataCachePath + uploadCacheDir + FOLDER_NAME_SEPARATOR + getTimeStampString();
|
||||
if (!FileUtil.exist(uploadFileCachePath)){
|
||||
FileUtil.mkdir(uploadFileCachePath);
|
||||
}
|
||||
|
||||
String fileCacheFullPath = uploadFileCachePath + File.separator + newFileName;
|
||||
String fileSaveFullPath = fileSavePath + File.separator + fileFullName;
|
||||
log.info("文件缓存路径为: " + fileCacheFullPath);
|
||||
|
||||
|
||||
// 这里可以添加将文件保存到本地磁盘或其他存储介质的逻辑
|
||||
File saveFile = new File(fileCacheFullPath);
|
||||
|
||||
// 将文件保存到硬盘
|
||||
try {
|
||||
fileInput.transferTo(saveFile);
|
||||
|
||||
EDDataParams fileParames = new EDDataParams();
|
||||
fileParames.setDataId(edDataInfo.getDataId());
|
||||
if(fileInput.getSize() == saveFile.length())
|
||||
{
|
||||
Path source = Paths.get(fileCacheFullPath);
|
||||
Path target = Paths.get(fileSaveFullPath);
|
||||
Files.move(source, target);
|
||||
fileParames.setSaveStatus("success");
|
||||
}
|
||||
else
|
||||
{
|
||||
saveFile.delete();
|
||||
fileParames.setSaveStatus("failure");
|
||||
}
|
||||
|
||||
boolean isSuccess = edDataRepository.updateFileInfo(fileParames);
|
||||
|
||||
FileUtil.del(uploadFileCachePath);//删除临时目录
|
||||
|
||||
log.info("文件保存成功: " + fileSaveFullPath);
|
||||
|
||||
return Boolean.TRUE;
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 文件保存目录路径
|
||||
String fileSavePath = dataStoragePath + File.separator + filePathOfFolder;
|
||||
String newFileName = edDataInfo.getDataId() + FOLDER_NAME_SEPARATOR + fileFullName;
|
||||
if (!FileUtil.exist(fileSavePath)){
|
||||
FileUtil.mkdir(fileSavePath);
|
||||
}
|
||||
|
||||
String dataCachePath = getDataCachePath();
|
||||
String uploadFileCachePath = dataCachePath + uploadCacheDir + FOLDER_NAME_SEPARATOR + getTimeStampString();
|
||||
if (!FileUtil.exist(uploadFileCachePath)){
|
||||
FileUtil.mkdir(uploadFileCachePath);
|
||||
}
|
||||
|
||||
String fileCacheFullPath = uploadFileCachePath + File.separator + newFileName;
|
||||
String fileSaveFullPath = fileSavePath + File.separator + fileFullName;
|
||||
log.info("文件缓存路径为: " + fileCacheFullPath);
|
||||
|
||||
|
||||
// 这里可以添加将文件保存到本地磁盘或其他存储介质的逻辑
|
||||
File saveFile = new File(fileCacheFullPath);
|
||||
|
||||
// 将文件保存到硬盘
|
||||
EDDataParams fileParames = new EDDataParams();
|
||||
try {
|
||||
fileInput.transferTo(saveFile);
|
||||
|
||||
|
||||
fileParames.setDataId(edDataInfo.getDataId());
|
||||
if(fileInput.getSize() == saveFile.length())
|
||||
{
|
||||
Path source = Paths.get(fileCacheFullPath);
|
||||
Path target = Paths.get(fileSaveFullPath);
|
||||
Files.move(source, target);
|
||||
fileParames.setSaveStatus("success");
|
||||
}
|
||||
else
|
||||
{
|
||||
saveFile.delete();
|
||||
fileParames.setSaveStatus("failure");
|
||||
}
|
||||
|
||||
isSuccess = edDataRepository.updateFileInfo(fileParames);
|
||||
|
||||
FileUtil.del(uploadFileCachePath);//删除临时目录
|
||||
|
||||
log.info("文件保存成功: " + fileSaveFullPath);
|
||||
|
||||
return Boolean.TRUE;
|
||||
|
||||
} catch (IOException e) {
|
||||
fileParames.setSaveStatus("failure");
|
||||
edDataRepository.updateFileInfo(fileParames);
|
||||
log.info("文件保存失败: " + fileSaveFullPath);
|
||||
throw new FileNotFoundException(e.getMessage());
|
||||
}
|
||||
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -659,11 +671,11 @@ public class EDDataServiceImpl implements EDDataService {
|
|||
&& !FileUtil.exist(fileStorageFullPath)
|
||||
)
|
||||
{
|
||||
if(fileInfo.getDataType().equals("folder")) {
|
||||
if(fileInfo.getDataType().equals(dataTypeFolder)) {
|
||||
if (!FileUtil.exist(fileStorageFullPath)) {
|
||||
FileUtil.mkdir(fileStorageFullPath);
|
||||
}
|
||||
} else if(fileInfo.getDataType().equals("file")) {
|
||||
} else if(fileInfo.getDataType().equals(dataTypeFile)) {
|
||||
Path source = Paths.get(importFileCacheFullPath);
|
||||
Path target = Paths.get(fileStorageFullPath);
|
||||
FileUtil.move(source,target,true);
|
||||
|
|
|
|||
|
|
@ -4,10 +4,11 @@ import electromagnetic.data.framework.share.model.BaseRequest;
|
|||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class EDDataRequest extends BaseRequest {
|
||||
public class EDDataRequest extends BaseRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 当前页
|
||||
|
|
|
|||
|
|
@ -55,6 +55,9 @@ public class EDDataFacadeImpl implements EDDataFacade {
|
|||
@Value("${file.encode.passwd}")
|
||||
private String encodePasswd;
|
||||
|
||||
@Value("${data.type.folder}")
|
||||
private String dataTypeFolder;
|
||||
|
||||
private static final String UPLOAD_FILE_CHUNK_SUFFIX = ".part";
|
||||
|
||||
|
||||
|
|
@ -70,12 +73,6 @@ public class EDDataFacadeImpl implements EDDataFacade {
|
|||
public ElectromagneticResult<Boolean> createFolder(EDDataRequest request) {
|
||||
EDDataInfo edDataInfo = EDDataMappers.INSTANCE.getEDDataInfo(request);
|
||||
|
||||
// 临时数据设置
|
||||
if(Boolean.TRUE) {
|
||||
if (request.getUserId() == null || request.getUserId().isEmpty()) request.setUserId(IdWorker.getSnowFlakeIdString());
|
||||
if (request.getUserName() == null || request.getUserName().isEmpty()) request.setUserName("user");
|
||||
}
|
||||
|
||||
edDataInfo.setCreator(request.getUserId());
|
||||
edDataInfo.setCreatorName(request.getUserName());
|
||||
edDataInfo.setModifier(request.getUserId());
|
||||
|
|
@ -87,13 +84,17 @@ public class EDDataFacadeImpl implements EDDataFacade {
|
|||
|
||||
edDataInfo.setDataId(IdWorker.getSnowFlakeIdString());
|
||||
edDataInfo.setDataNo(edDataInfo.getDataId());
|
||||
edDataInfo.setDataType("folder");
|
||||
edDataInfo.setDataType(dataTypeFolder);
|
||||
edDataInfo.setVersion("1.0.0");
|
||||
edDataInfo.setDataStatus("publish");
|
||||
edDataInfo.setSaveStatus("success");
|
||||
|
||||
Boolean isSuccess = edDataService.createDataInfo(edDataInfo);
|
||||
return ElectromagneticResultUtil.success(isSuccess);
|
||||
try {
|
||||
return ElectromagneticResultUtil.success(edDataService.createDataInfo(edDataInfo));
|
||||
} catch (FileNotFoundException e) {
|
||||
log.error("创建文件夹失败。。。", e);
|
||||
return ElectromagneticResultUtil.fail("500", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -125,8 +126,8 @@ public class EDDataFacadeImpl implements EDDataFacade {
|
|||
*/
|
||||
public ElectromagneticResult<Boolean> updateFileInfo(EDDataRequest request)
|
||||
{
|
||||
EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request);
|
||||
try {
|
||||
EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request);
|
||||
return ElectromagneticResultUtil.success(edDataService.updateFileInfo(parames));
|
||||
} catch (FileNotFoundException e) {
|
||||
log.error("文件信息更新失败。。。", e);
|
||||
|
|
@ -159,9 +160,13 @@ public class EDDataFacadeImpl implements EDDataFacade {
|
|||
*/
|
||||
public ElectromagneticResult<Boolean> uploadFile(EDDataRequest request)
|
||||
{
|
||||
EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request);
|
||||
Boolean isSuccess = edDataService.uploadFile(parames);
|
||||
return ElectromagneticResultUtil.success(isSuccess);
|
||||
try {
|
||||
EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request);
|
||||
return ElectromagneticResultUtil.success(edDataService.uploadFile(parames));
|
||||
} catch (FileNotFoundException e) {
|
||||
log.error("上传文件失败。。。", e);
|
||||
return ElectromagneticResultUtil.fail("500", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue