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