调通了文件复制和移动
This commit is contained in:
parent
28b6c4f772
commit
07e9ae9d23
|
|
@ -8,7 +8,7 @@ public interface FileSystemService {
|
||||||
|
|
||||||
void copyFile(String source, String destination);
|
void copyFile(String source, String destination);
|
||||||
|
|
||||||
void moveFile(String source, String destination, boolean deleteSource);
|
void moveFile(String source, String destination);
|
||||||
|
|
||||||
void save(InputStream inputStream, String destination);
|
void save(InputStream inputStream, String destination);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -458,7 +458,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
}
|
}
|
||||||
String destPath = commonService.getEleDataPath() + File.separator + sysFilePath;
|
String destPath = commonService.getEleDataPath() + File.separator + sysFilePath;
|
||||||
String sourcePath = prjDirPath + File.separator + sysFilePath;
|
String sourcePath = prjDirPath + File.separator + sysFilePath;
|
||||||
fileSystemService.moveFile(sourcePath, destPath, true);
|
fileSystemService.moveFile(sourcePath, destPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -633,10 +633,42 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public ElectromagneticResult<?> moveFile(String id, String targetFolderId, Integer strategy) {
|
public ElectromagneticResult<?> moveFile(String id, String targetFolderId, Integer strategy) {
|
||||||
return moveFile(id, targetFolderId, strategy, true);
|
|
||||||
|
// 获取原文件mysql模型
|
||||||
|
EdFileInfo srcFileInfo = this.baseMapper.selectById(id);
|
||||||
|
String srcFileDbPath = srcFileInfo.getFilePath();
|
||||||
|
String fileCode = srcFileInfo.getFileCode();
|
||||||
|
// 判断目标路径下是否有同名文件,如果所有的同名文件:1)如果所有文件都已经被作废,则该文件为新文件,版本号从100开始。2)如果有没有被作废的文件,则冲突处理方式按---1-跳过冲突文件 2-做版本更新 3-重命名,文件名加"_1"
|
||||||
|
long count = this.baseMapper.selectCount(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||||
|
.eq(EdFileInfo::getParentId, targetFolderId)
|
||||||
|
.eq(EdFileInfo::getFileName, srcFileInfo.getFileName())
|
||||||
|
.eq(EdFileInfo::getFileType, srcFileInfo.getFileType())
|
||||||
|
.eq(EdFileInfo::getEffectFlag, EffectFlagEnum.EFFECT.code));
|
||||||
|
EdFileInfo destFolderInfo = this.baseMapper.selectOne(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||||
|
.eq(EdFileInfo::getId, targetFolderId));
|
||||||
|
if (count == 0) {
|
||||||
|
// 没有同名文件
|
||||||
|
// 首先将信息保存到MySQL
|
||||||
|
String fileTime = EleCommonUtil.getNowTimeStr();
|
||||||
|
String codePathByDbPath = commonService.getCodePathByDbPath(destFolderInfo.getFilePath());
|
||||||
|
String newFileCode = commonService.createFileCode(codePathByDbPath, srcFileInfo.getFileType(), FILE_START_VERSION, fileTime);
|
||||||
|
srcFileInfo.setParentId(targetFolderId)
|
||||||
|
.setFileVersion(FILE_START_VERSION)
|
||||||
|
.setFileTime(fileTime)
|
||||||
|
.setFilePath(destFolderInfo.getFilePath() + MYSQL_FILE_PATH_SPLIT + srcFileInfo.getId())
|
||||||
|
.setFileCode(newFileCode);
|
||||||
|
this.baseMapper.updateById(srcFileInfo);
|
||||||
|
// 文件系统移动文件
|
||||||
|
String srcFilePath = commonService.getFileSysPath(srcFileDbPath);
|
||||||
|
String destFilePath = commonService.getFileSysPath(destFolderInfo.getFilePath());
|
||||||
|
fileSystemService.moveFile(srcFilePath, destFilePath);
|
||||||
|
} else {
|
||||||
|
return handMoveConflict(targetFolderId, strategy, srcFileInfo, destFolderInfo);
|
||||||
|
}
|
||||||
|
return ElectromagneticResultUtil.success(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ElectromagneticResult<?> handMoveConflict(String targetFolderId, Integer strategy, EdFileInfo srcFileInfo, EdFileInfo destFolderInfo, boolean deleteSrc) {
|
private ElectromagneticResult<?> handMoveConflict(String targetFolderId, Integer strategy, EdFileInfo srcFileInfo, EdFileInfo destFolderInfo) {
|
||||||
|
|
||||||
// 禁止同目录下移动和复制
|
// 禁止同目录下移动和复制
|
||||||
if (srcFileInfo.getParentId().equals(destFolderInfo.getId())) {
|
if (srcFileInfo.getParentId().equals(destFolderInfo.getId())) {
|
||||||
|
|
@ -667,7 +699,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
this.baseMapper.insert(destSaveFileInfo);
|
this.baseMapper.insert(destSaveFileInfo);
|
||||||
String srcFilePath = commonService.getFileSysPath(srcFileInfo.getFilePath());
|
String srcFilePath = commonService.getFileSysPath(srcFileInfo.getFilePath());
|
||||||
String destFilePath = commonService.getFileSysPath(destSaveFileInfo.getFilePath());
|
String destFilePath = commonService.getFileSysPath(destSaveFileInfo.getFilePath());
|
||||||
fileSystemService.moveFile(srcFilePath, destFilePath, true);
|
fileSystemService.copyFile(srcFilePath, destFilePath);
|
||||||
this.baseMapper.deleteById(srcFileInfo.getId());
|
this.baseMapper.deleteById(srcFileInfo.getId());
|
||||||
this.baseMapper.update(null, Wrappers.lambdaUpdate(EdFileInfo.class)
|
this.baseMapper.update(null, Wrappers.lambdaUpdate(EdFileInfo.class)
|
||||||
.set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)
|
.set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)
|
||||||
|
|
@ -688,17 +720,23 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
// 移动文件
|
// 移动文件
|
||||||
String srcFileSysPath = commonService.getFileSysPath(srcFileInfo.getFilePath());
|
String srcFileSysPath = commonService.getFileSysPath(srcFileInfo.getFilePath());
|
||||||
String destFileSysPath = commonService.getFileSysPath(newEdFileInfo.getFilePath());
|
String destFileSysPath = commonService.getFileSysPath(newEdFileInfo.getFilePath());
|
||||||
fileSystemService.moveFile(srcFileSysPath, destFileSysPath, deleteSrc);
|
fileSystemService.moveFile(srcFileSysPath, destFileSysPath);
|
||||||
this.baseMapper.deleteById(srcFileInfo.getId());
|
this.baseMapper.deleteById(srcFileInfo.getId());
|
||||||
}
|
}
|
||||||
return ElectromagneticResultUtil.success(true);
|
return ElectromagneticResultUtil.success(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ElectromagneticResult<?> moveFile(String id, String targetFolderId, Integer strategy, boolean deleteSrc) {
|
/**
|
||||||
|
* 复制文件
|
||||||
|
* @param id
|
||||||
|
* @param targetFolderId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ElectromagneticResult<?> copyFile(String id, String targetFolderId, Integer strategy) {
|
||||||
// 获取原文件mysql模型
|
// 获取原文件mysql模型
|
||||||
EdFileInfo srcFileInfo = this.baseMapper.selectById(id);
|
EdFileInfo srcFileInfo = this.baseMapper.selectById(id);
|
||||||
String srcFileDbPath = srcFileInfo.getFilePath();
|
String srcFileDbPath = srcFileInfo.getFilePath();
|
||||||
String fileCode = srcFileInfo.getFileCode();
|
|
||||||
// 判断目标路径下是否有同名文件,如果所有的同名文件:1)如果所有文件都已经被作废,则该文件为新文件,版本号从100开始。2)如果有没有被作废的文件,则冲突处理方式按---1-跳过冲突文件 2-做版本更新 3-重命名,文件名加"_1"
|
// 判断目标路径下是否有同名文件,如果所有的同名文件:1)如果所有文件都已经被作废,则该文件为新文件,版本号从100开始。2)如果有没有被作废的文件,则冲突处理方式按---1-跳过冲突文件 2-做版本更新 3-重命名,文件名加"_1"
|
||||||
long count = this.baseMapper.selectCount(Wrappers.lambdaQuery(EdFileInfo.class)
|
long count = this.baseMapper.selectCount(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||||
.eq(EdFileInfo::getParentId, targetFolderId)
|
.eq(EdFileInfo::getParentId, targetFolderId)
|
||||||
|
|
@ -713,31 +751,74 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
String fileTime = EleCommonUtil.getNowTimeStr();
|
String fileTime = EleCommonUtil.getNowTimeStr();
|
||||||
String codePathByDbPath = commonService.getCodePathByDbPath(destFolderInfo.getFilePath());
|
String codePathByDbPath = commonService.getCodePathByDbPath(destFolderInfo.getFilePath());
|
||||||
String newFileCode = commonService.createFileCode(codePathByDbPath, srcFileInfo.getFileType(), FILE_START_VERSION, fileTime);
|
String newFileCode = commonService.createFileCode(codePathByDbPath, srcFileInfo.getFileType(), FILE_START_VERSION, fileTime);
|
||||||
srcFileInfo.setParentId(targetFolderId)
|
EdFileInfo destFileInfo = BeanUtil.copyProperties(srcFileInfo, EdFileInfo.class);
|
||||||
|
destFileInfo.newInit();
|
||||||
|
destFileInfo.setParentId(targetFolderId)
|
||||||
.setFileVersion(FILE_START_VERSION)
|
.setFileVersion(FILE_START_VERSION)
|
||||||
.setFileTime(fileTime)
|
.setFileTime(fileTime)
|
||||||
.setFilePath(destFolderInfo.getFilePath() + MYSQL_FILE_PATH_SPLIT + srcFileInfo.getId())
|
.setFilePath(destFolderInfo.getFilePath() + MYSQL_FILE_PATH_SPLIT + destFileInfo.getId())
|
||||||
.setFileCode(newFileCode);
|
.setFileCode(newFileCode);
|
||||||
this.baseMapper.updateById(srcFileInfo);
|
this.baseMapper.insert(destFileInfo);
|
||||||
// 文件系统移动文件
|
// 文件系统移动文件
|
||||||
String srcFilePath = commonService.getFileSysPath(srcFileDbPath);
|
String srcFilePath = commonService.getFileSysPath(srcFileDbPath);
|
||||||
String destFilePath = commonService.getFileSysPath(destFolderInfo.getFilePath());
|
String destFilePath = commonService.getFileSysPath(destFileInfo.getFilePath());
|
||||||
fileSystemService.moveFile(srcFilePath, destFilePath, deleteSrc);
|
fileSystemService.copyFile(srcFilePath, destFilePath);
|
||||||
} else {
|
} else {
|
||||||
return handMoveConflict(targetFolderId, strategy, srcFileInfo, destFolderInfo, deleteSrc);
|
return handCopyConflict(targetFolderId, strategy, srcFileInfo, destFolderInfo);
|
||||||
}
|
}
|
||||||
return ElectromagneticResultUtil.success(true);
|
return ElectromagneticResultUtil.success(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private ElectromagneticResult<?> handCopyConflict(String targetFolderId, Integer strategy, EdFileInfo srcFileInfo, EdFileInfo destFolderInfo) {
|
||||||
* 复制文件
|
// 禁止同目录下移动和复制
|
||||||
* @param id
|
if (srcFileInfo.getParentId().equals(destFolderInfo.getId())) {
|
||||||
* @param targetFolderId
|
String info = "禁止相同文件夹下移动文件";
|
||||||
* @return
|
log.info(info);
|
||||||
*/
|
return ElectromagneticResultUtil.fail("-1", info);
|
||||||
@Override
|
}
|
||||||
public ElectromagneticResult<?> copyFile(String id, String targetFolderId, Integer strategy) {
|
|
||||||
return moveFile(id, targetFolderId, strategy, false);
|
if (strategy == 2) {
|
||||||
|
// 做版本更新
|
||||||
|
List<EdFileInfo> sameFileInfos = this.baseMapper.selectList(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||||
|
.eq(EdFileInfo::getParentId, targetFolderId)
|
||||||
|
.eq(EdFileInfo::getFileName, srcFileInfo.getFileName())
|
||||||
|
.eq(EdFileInfo::getFileType, srcFileInfo.getFileType()));
|
||||||
|
Integer maxFileVersion = Collections.max(sameFileInfos, Comparator.comparing(EdFileInfo::getFileVersion)).getFileVersion();
|
||||||
|
String newFileDbId = IdWorker.getSnowFlakeIdString();
|
||||||
|
String fileTime = EleCommonUtil.getNowTimeStr();
|
||||||
|
String codePathByDbPath = commonService.getCodePathByDbPath(destFolderInfo.getFilePath());
|
||||||
|
String fileCode = commonService.createFileCode(codePathByDbPath, srcFileInfo.getFileType(), maxFileVersion + 1, fileTime);
|
||||||
|
EdFileInfo fileInfoTmp = sameFileInfos.stream().filter(e -> e.getEffectFlag().equals(EffectFlagEnum.EFFECT.code)).findFirst().orElseThrow(RuntimeException::new);
|
||||||
|
EdFileInfo destSaveFileInfo = BeanUtil.copyProperties(fileInfoTmp, EdFileInfo.class);
|
||||||
|
destSaveFileInfo.setId(newFileDbId);
|
||||||
|
destSaveFileInfo.setFileVersion(maxFileVersion + 1)
|
||||||
|
.setFilePath(destFolderInfo.getFilePath() + MYSQL_FILE_PATH_SPLIT + newFileDbId)
|
||||||
|
.setPreVersion(maxFileVersion)
|
||||||
|
.setEffectFlag(EffectFlagEnum.EFFECT.code)
|
||||||
|
.setFileCode(fileCode);
|
||||||
|
this.baseMapper.insert(destSaveFileInfo);
|
||||||
|
String srcFilePath = commonService.getFileSysPath(srcFileInfo.getFilePath());
|
||||||
|
String destFilePath = commonService.getFileSysPath(destSaveFileInfo.getFilePath());
|
||||||
|
fileSystemService.copyFile(srcFilePath, destFilePath);
|
||||||
|
} else if (strategy == 3) {
|
||||||
|
// 文件名加“_1”,版本号从100开始
|
||||||
|
// 处理MySQL相关逻辑
|
||||||
|
EdFileInfo newEdFileInfo = BeanUtil.copyProperties(srcFileInfo, EdFileInfo.class);
|
||||||
|
newEdFileInfo.newInit();
|
||||||
|
String dbPathByDbPath = commonService.getCodePathByDbPath(destFolderInfo.getFilePath());
|
||||||
|
newEdFileInfo.setParentId(targetFolderId)
|
||||||
|
.setFileVersion(FILE_START_VERSION)
|
||||||
|
.setFileName(srcFileInfo.getFileName() + "_1")
|
||||||
|
.setFileCode(commonService.createFileCode(dbPathByDbPath, srcFileInfo.getFileType(), FILE_START_VERSION, newEdFileInfo.getFileTime()))
|
||||||
|
.setFilePath(destFolderInfo.getFilePath() + MYSQL_FILE_PATH_SPLIT + newEdFileInfo.getId());
|
||||||
|
resetFileInfoName(newEdFileInfo);
|
||||||
|
this.baseMapper.insert(newEdFileInfo);
|
||||||
|
// 移动文件
|
||||||
|
String srcFileSysPath = commonService.getFileSysPath(srcFileInfo.getFilePath());
|
||||||
|
String destFileSysPath = commonService.getFileSysPath(newEdFileInfo.getFilePath());
|
||||||
|
fileSystemService.copyFile(srcFileSysPath, destFileSysPath);
|
||||||
|
}
|
||||||
|
return ElectromagneticResultUtil.success(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
|
|
||||||
|
|
@ -18,16 +18,12 @@ public class FileSystemServiceImpl implements FileSystemService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void copyFile(String source, String destination) {
|
public void copyFile(String source, String destination) {
|
||||||
moveFile(source, destination, false);
|
FileUtil.copy(source, destination, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void moveFile(String source, String destination, boolean deleteSource) {
|
public void moveFile(String source, String destination) {
|
||||||
if (deleteSource) {
|
|
||||||
FileUtil.move(new File(source), new File(destination), true);
|
FileUtil.move(new File(source), new File(destination), true);
|
||||||
return;
|
|
||||||
}
|
|
||||||
FileUtil.copy(new File(source), new File(destination), false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue