From b7b815c12517afad2024d642618cf996a2d5bef0 Mon Sep 17 00:00:00 2001 From: LiWeiJie <3491909454@qq.com> Date: Mon, 9 Sep 2024 08:52:56 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E6=B7=BB=E5=8A=A0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file/service/FiletransferService.java | 108 ++++++++++++------ 1 file changed, 70 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/qiwenshare/file/service/FiletransferService.java b/src/main/java/com/qiwenshare/file/service/FiletransferService.java index f11f2e8..aa2f86e 100644 --- a/src/main/java/com/qiwenshare/file/service/FiletransferService.java +++ b/src/main/java/com/qiwenshare/file/service/FiletransferService.java @@ -87,52 +87,65 @@ public class FiletransferService implements IFiletransferService { @Override public UploadFileVo uploadFileSpeed(UploadFileDTO uploadFileDTO) { + // 创建一个UploadFileVo对象,用于返回上传文件的结果 UploadFileVo uploadFileVo = new UploadFileVo(); + // 创建一个HashMap来存放查询参数 Map param = new HashMap<>(); + // 将文件的唯一标识符放入查询参数,用于后续查询文件是否已存在 param.put("identifier", uploadFileDTO.getIdentifier()); + // 通过fileMapper查询数据库,检查文件是否已存在 List 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(()->{ + exec.execute(() -> { fileDealComp.restoreParentFilePath(finalQiwenFile, SessionUtil.getUserId()); }); - } - + // 设置上传结果为跳过上传 uploadFileVo.setSkipUpload(true); } else { + // 如果文件不存在,设置上传结果为不跳过上传 uploadFileVo.setSkipUpload(false); - + // 查询已经上传的分块编号列表 List uploaded = uploadTaskDetailMapper.selectUploadedChunkNumList(uploadFileDTO.getIdentifier()); + // 如果存在已上传的分块编号,则设置到上传结果中 if (uploaded != null && !uploaded.isEmpty()) { uploadFileVo.setUploaded(uploaded); } else { - + // 如果没有已上传的分块编号,检查上传任务是否已创建 LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(UploadTask::getIdentifier, uploadFileDTO.getIdentifier()); List 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 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++){ + + // 遍历上传文件的结果列表 + 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())){ + // 如果上传文件状态为成功 + 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().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().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(()->{ + exec.execute(() -> { fileDealComp.restoreParentFilePath(finalQiwenFile, userId); }); - } - - LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); - lambdaQueryWrapper.eq(UploadTaskDetail::getIdentifier, uploadFileDto.getIdentifier()); - uploadTaskDetailMapper.delete(lambdaQueryWrapper); - + // 更新UploadTask的上传状态为成功 LambdaUpdateWrapper 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,35 +275,38 @@ 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()); uploadTaskDetail.setChunkNumber(uploadFileDto.getChunkNumber()); - uploadTaskDetail.setChunkSize((int)uploadFileDto.getChunkSize()); + uploadTaskDetail.setChunkSize((int) uploadFileDto.getChunkSize()); uploadTaskDetail.setRelativePath(uploadFileDto.getRelativePath()); uploadTaskDetail.setTotalChunks(uploadFileDto.getTotalChunks()); - uploadTaskDetail.setTotalSize((int)uploadFileDto.getTotalSize()); + uploadTaskDetail.setTotalSize((int) uploadFileDto.getTotalSize()); uploadTaskDetail.setIdentifier(uploadFileDto.getIdentifier()); + // 插入UploadTaskDetail记录到数据库 uploadTaskDetailMapper.insert(uploadTaskDetail); - } else if (UploadFileStatusEnum.FAIL.equals(uploadFileResult.getStatus())) { + // 如果上传文件状态为失败 + // 删除UploadTaskDetail中对应identifier的记录 LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(UploadTaskDetail::getIdentifier, uploadFileDto.getIdentifier()); uploadTaskDetailMapper.delete(lambdaQueryWrapper); - + // 更新UploadTask的上传状态为失败 LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper<>(); lambdaUpdateWrapper.set(UploadTask::getUploadStatus, UploadFileStatusEnum.FAIL.getCode()) .eq(UploadTask::getIdentifier, uploadFileDto.getIdentifier()); uploadTaskMapper.update(null, lambdaUpdateWrapper); } + // 结束for循环,继续处理其他上传文件结果或执行其他逻辑 } - } @@ -431,8 +463,8 @@ public class FiletransferService implements IFiletransferService { previewer.imageOriginalPreview(httpServletResponse, previewFile); } } catch (Exception e){ - //org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。 - if (e.getMessage().contains("ClientAbortException")) { + //org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。 + if (e.getMessage().contains("ClientAbortException")) { //该异常忽略不做处理 } else { log.error("预览文件出现异常:{}", e.getMessage());