fix:添加文件注释

This commit is contained in:
LiWeiJie 2024-09-09 08:52:56 +08:00
parent cb30e91944
commit b7b815c125

View File

@ -87,52 +87,65 @@ public class FiletransferService implements IFiletransferService {
@Override
public UploadFileVo uploadFileSpeed(UploadFileDTO uploadFileDTO) {
// 创建一个UploadFileVo对象用于返回上传文件的结果
UploadFileVo uploadFileVo = new UploadFileVo();
// 创建一个HashMap来存放查询参数
Map<String, Object> param = new HashMap<>();
// 将文件的唯一标识符放入查询参数用于后续查询文件是否已存在
param.put("identifier", uploadFileDTO.getIdentifier());
// 通过fileMapper查询数据库检查文件是否已存在
List<FileBean> list = fileMapper.selectByMap(param);
// 从UploadFileDTO获取文件路径和相对路径
String filePath = uploadFileDTO.getFilePath();
String relativePath = uploadFileDTO.getRelativePath();
// 创建QiwenFile对象用于表示文件
QiwenFile qiwenFile = null;
// 根据相对路径是否包含"/"来决定文件对象的创建方式
if (relativePath.contains("/")) {
// 如果相对路径包含"/"则使用相对路径
qiwenFile = new QiwenFile(filePath, relativePath, false);
} else {
// 如果不包含则使用文件名
qiwenFile = new QiwenFile(filePath, uploadFileDTO.getFilename(), false);
}
// 如果文件已存在
if (list != null && !list.isEmpty()) {
// 获取查询结果中的第一个文件信息
FileBean file = list.get(0);
// 创建UserFile对象关联用户和文件
UserFile userFile = new UserFile(qiwenFile, SessionUtil.getUserId(), file.getFileId());
try {
// 将UserFile对象插入数据库因为以及有了就不用在次上传文件了
userFileMapper.insert(userFile);
// 处理Elasticsearch相关的操作
fileDealComp.uploadESByUserFileId(userFile.getUserFileId());
} catch (Exception e) {
// 如果发生异常记录警告日志
log.warn("极速上传文件冲突重命名处理: {}", JSON.toJSONString(userFile));
}
// 如果相对路径包含"/"则异步执行恢复父文件路径的操作
if (relativePath.contains("/")) {
QiwenFile finalQiwenFile = qiwenFile;
exec.execute(() -> {
fileDealComp.restoreParentFilePath(finalQiwenFile, SessionUtil.getUserId());
});
}
// 设置上传结果为跳过上传
uploadFileVo.setSkipUpload(true);
} else {
// 如果文件不存在设置上传结果为不跳过上传
uploadFileVo.setSkipUpload(false);
// 查询已经上传的分块编号列表
List<Integer> uploaded = uploadTaskDetailMapper.selectUploadedChunkNumList(uploadFileDTO.getIdentifier());
// 如果存在已上传的分块编号则设置到上传结果中
if (uploaded != null && !uploaded.isEmpty()) {
uploadFileVo.setUploaded(uploaded);
} else {
// 如果没有已上传的分块编号检查上传任务是否已创建
LambdaQueryWrapper<UploadTask> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UploadTask::getIdentifier, uploadFileDTO.getIdentifier());
List<UploadTask> rslist = uploadTaskMapper.selectList(lambdaQueryWrapper);
// 如果上传任务不存在则创建新的上传任务并插入数据库
if (rslist == null || rslist.isEmpty()) {
UploadTask uploadTask = new UploadTask();
uploadTask.setIdentifier(uploadFileDTO.getIdentifier());
@ -145,61 +158,83 @@ public class FiletransferService implements IFiletransferService {
uploadTaskMapper.insert(uploadTask);
}
}
}
// 返回上传文件的结果对象
return uploadFileVo;
}
@Override
public void uploadFile(HttpServletRequest request, UploadFileDTO uploadFileDto, String userId) {
// 创建UploadFile对象用于存储上传文件的相关信息
UploadFile uploadFile = new UploadFile();
// 设置上传文件的分块编号
uploadFile.setChunkNumber(uploadFileDto.getChunkNumber());
// 设置上传文件的单块大小
uploadFile.setChunkSize(uploadFileDto.getChunkSize());
// 设置上传文件的总块数
uploadFile.setTotalChunks(uploadFileDto.getTotalChunks());
// 设置上传文件的唯一标识符
uploadFile.setIdentifier(uploadFileDto.getIdentifier());
// 设置上传文件的总大小
uploadFile.setTotalSize(uploadFileDto.getTotalSize());
// 设置当前上传的分块大小
uploadFile.setCurrentChunkSize(uploadFileDto.getCurrentChunkSize());
// 通过工厂方法获取上传器
Uploader uploader = ufopFactory.getUploader();
// 如果上传器为null记录错误日志并抛出上传异常
if (uploader == null) {
log.error("上传失败请检查storageType是否配置正确");
throw new UploadException("上传失败");
}
// 存储上传文件的结果列表
List<UploadFileResult> uploadFileResultList;
try {
// 执行上传操作并获取上传结果列表
uploadFileResultList = uploader.upload(request, uploadFile);
} catch (Exception e) {
// 如果上传过程中发生异常记录错误日志并抛出上传异常
log.error("上传失败请检查UFOP连接配置是否正确");
throw new UploadException("上传失败", e);
}
// 遍历上传文件的结果列表
for (int i = 0; i < uploadFileResultList.size(); i++) {
// 获取单个上传文件的结果
UploadFileResult uploadFileResult = uploadFileResultList.get(i);
// 获取上传文件的相对路径
String relativePath = uploadFileDto.getRelativePath();
// 初始化QiwenFile对象
QiwenFile qiwenFile = null;
// 根据相对路径是否包含"/"来决定文件对象的创建方式是文件相对路径还是文件名
if (relativePath.contains("/")) {
qiwenFile = new QiwenFile(uploadFileDto.getFilePath(), relativePath, false);
} else {
qiwenFile = new QiwenFile(uploadFileDto.getFilePath(), uploadFileDto.getFilename(), false);
}
// 如果上传文件状态为成功
if (UploadFileStatusEnum.SUCCESS.equals(uploadFileResult.getStatus())) {
// 创建FileBean对象并设置属性
FileBean fileBean = new FileBean(uploadFileResult);
fileBean.setCreateUserId(userId);
try {
// 插入FileBean对象到数据库
fileMapper.insert(fileBean);
} catch (Exception e) {
// 如果发生异常记录警告日志并重新查询FileBean对象
log.warn("identifier Duplicate: {}", fileBean.getIdentifier());
fileBean = fileMapper.selectOne(new QueryWrapper<FileBean>().lambda().eq(FileBean::getIdentifier, fileBean.getIdentifier()));
}
// 创建UserFile对象并设置属性
UserFile userFile = new UserFile(qiwenFile, userId, fileBean.getFileId());
try {
// 插入UserFile对象到数据库
userFileMapper.insert(userFile);
// 处理Elasticsearch相关操作
fileDealComp.uploadESByUserFileId(userFile.getUserFileId());
} catch (Exception e) {
// 如果发生异常查询UserFile对象并处理文件冲突
UserFile userFile1 = userFileMapper.selectOne(new QueryWrapper<UserFile>().lambda()
.eq(UserFile::getUserId, userFile.getUserId())
.eq(UserFile::getFilePath, userFile.getFilePath())
@ -210,32 +245,26 @@ public class FiletransferService implements IFiletransferService {
FileBean file1 = fileMapper.selectById(userFile1.getFileId());
if (!StringUtils.equals(fileBean.getIdentifier(), file1.getIdentifier())) {
log.warn("文件冲突重命名处理: {}", JSON.toJSONString(userFile1));
// 生成新的文件名并插入UserFile对象
String fileName = fileDealComp.getRepeatFileName(userFile, userFile.getFilePath());
userFile.setFileName(fileName);
userFileMapper.insert(userFile);
fileDealComp.uploadESByUserFileId(userFile.getUserFileId());
}
}
// 如果相对路径包含"/"则异步执行恢复父文件路径的操作
if (relativePath.contains("/")) {
QiwenFile finalQiwenFile = qiwenFile;
exec.execute(() -> {
fileDealComp.restoreParentFilePath(finalQiwenFile, userId);
});
}
LambdaQueryWrapper<UploadTaskDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UploadTaskDetail::getIdentifier, uploadFileDto.getIdentifier());
uploadTaskDetailMapper.delete(lambdaQueryWrapper);
// 更新UploadTask的上传状态为成功
LambdaUpdateWrapper<UploadTask> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.set(UploadTask::getUploadStatus, UploadFileStatusEnum.SUCCESS.getCode())
.eq(UploadTask::getIdentifier, uploadFileDto.getIdentifier());
uploadTaskMapper.update(null, lambdaUpdateWrapper);
// 如果上传的文件是图片则生成图片缩略图并保存到数据库
try {
if (UFOPUtils.isImageFile(uploadFileResult.getExtendName())) {
BufferedImage src = uploadFileResult.getBufferedImage();
@ -246,12 +275,13 @@ public class FiletransferService implements IFiletransferService {
imageMapper.insert(image);
}
} catch (Exception e) {
// 如果生成缩略图失败记录错误日志
log.error("生成图片缩略图失败!", e);
}
// 如果上传的文件是音乐文件则解析音乐文件信息
fileDealComp.parseMusicFile(uploadFileResult.getExtendName(), uploadFileResult.getStorageType().getCode(), uploadFileResult.getFileUrl(), fileBean.getFileId());
} else if (UploadFileStatusEnum.UNCOMPLATE.equals(uploadFileResult.getStatus())) {
// 如果上传文件状态为未完成
UploadTaskDetail uploadTaskDetail = new UploadTaskDetail();
uploadTaskDetail.setFilePath(qiwenFile.getParent());
uploadTaskDetail.setFilename(qiwenFile.getNameNotExtend());
@ -261,20 +291,22 @@ public class FiletransferService implements IFiletransferService {
uploadTaskDetail.setTotalChunks(uploadFileDto.getTotalChunks());
uploadTaskDetail.setTotalSize((int) uploadFileDto.getTotalSize());
uploadTaskDetail.setIdentifier(uploadFileDto.getIdentifier());
// 插入UploadTaskDetail记录到数据库
uploadTaskDetailMapper.insert(uploadTaskDetail);
} else if (UploadFileStatusEnum.FAIL.equals(uploadFileResult.getStatus())) {
// 如果上传文件状态为失败
// 删除UploadTaskDetail中对应identifier的记录
LambdaQueryWrapper<UploadTaskDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UploadTaskDetail::getIdentifier, uploadFileDto.getIdentifier());
uploadTaskDetailMapper.delete(lambdaQueryWrapper);
// 更新UploadTask的上传状态为失败
LambdaUpdateWrapper<UploadTask> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.set(UploadTask::getUploadStatus, UploadFileStatusEnum.FAIL.getCode())
.eq(UploadTask::getIdentifier, uploadFileDto.getIdentifier());
uploadTaskMapper.update(null, lambdaUpdateWrapper);
}
// 结束for循环继续处理其他上传文件结果或执行其他逻辑
}
}