From 820862bcaf866047f72cd305362ec72b08dd1d0a Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Fri, 19 Apr 2024 23:35:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=BC=BA=E9=99=B7=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6Path=20Manipulation=E6=BC=8F=E6=B4=9E?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/bug/service/BugAttachmentService.java | 9 +++++---- .../main/java/io/metersphere/bug/service/BugService.java | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugAttachmentService.java b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugAttachmentService.java index be1e4c81d6..e525f7f40e 100644 --- a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugAttachmentService.java +++ b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugAttachmentService.java @@ -43,6 +43,7 @@ import io.metersphere.system.uid.IDGenerator; import jakarta.annotation.Resource; import org.apache.commons.collections.MapUtils; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; @@ -440,7 +441,7 @@ public class BugAttachmentService { FileMetadata meta = fileMetadataMap.get(fileId); if (meta != null) { try { - File uploadTmpFile = new File(LocalRepositoryDir.getBugTmpDir() + "/" + meta.getName() + "." + meta.getType()); + File uploadTmpFile = new File(FilenameUtils.normalize(LocalRepositoryDir.getBugTmpDir() + File.separator + meta.getName() + "." + meta.getType())); byte[] fileByte = fileMetadataService.getFileByte(meta); FileUtils.writeByteArrayToFile(uploadTmpFile, fileByte); linkSyncFiles.add(new SyncAttachmentToPlatformRequest(platformBugKey, uploadTmpFile, SyncAttachmentType.UPLOAD.syncOperateType())); @@ -482,7 +483,7 @@ public class BugAttachmentService { fileService.upload(file, fileRequest); if (!StringUtils.equals(platformName, BugPlatform.LOCAL.getName())) { // 非本地平台,同步附件到平台 - File uploadTmpFile = new File(LocalRepositoryDir.getBugTmpDir() + "/" + file.getOriginalFilename()); + File uploadTmpFile = new File(FilenameUtils.normalize(LocalRepositoryDir.getBugTmpDir() + File.separator + file.getOriginalFilename())); FileUtils.writeByteArrayToFile(uploadTmpFile, file.getBytes()); localSyncFiles.add(new SyncAttachmentToPlatformRequest(platformBugKey, uploadTmpFile, SyncAttachmentType.UPLOAD.syncOperateType())); } @@ -510,7 +511,7 @@ public class BugAttachmentService { FileMetadata fileMetadata = fileMetadataMapper.selectByExample(example).get(0); // 取消关联的附件同步至平台 if (!StringUtils.equals(platformName, BugPlatform.LOCAL.getName())) { - File deleteTmpFile = new File(LocalRepositoryDir.getBugTmpDir() + "/" + fileMetadata.getName() + "." + fileMetadata.getType()); + File deleteTmpFile = new File(FilenameUtils.normalize(LocalRepositoryDir.getBugTmpDir() + File.separator + fileMetadata.getName() + "." + fileMetadata.getType())); linkSyncFiles.add(new SyncAttachmentToPlatformRequest(platformBugKey, deleteTmpFile, SyncAttachmentType.DELETE.syncOperateType())); } // 取消关联的附件, FILE_ASSOCIATION表 @@ -540,7 +541,7 @@ public class BugAttachmentService { fileService.deleteFile(fileRequest); // 删除的本地的附件同步至平台 if (!StringUtils.equals(platformName, BugPlatform.LOCAL.getName())) { - File deleteTmpFile = new File(LocalRepositoryDir.getBugTmpDir() + "/" + localAttachment.getFileName()); + File deleteTmpFile = new File(FilenameUtils.normalize(LocalRepositoryDir.getBugTmpDir() + File.separator + localAttachment.getFileName())); syncLocalFiles.add(new SyncAttachmentToPlatformRequest(platformBugKey, deleteTmpFile, SyncAttachmentType.DELETE.syncOperateType())); } } catch (Exception e) { diff --git a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java index d309557c57..e38753e2e3 100644 --- a/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java +++ b/backend/services/bug-management/src/main/java/io/metersphere/bug/service/BugService.java @@ -59,6 +59,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.ListUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; @@ -1059,7 +1060,7 @@ public class BugService { FileCenter.getDefaultRepository().saveFile(bytes, buildBugFileRequest(request.getProjectId(), request.getId(), localAttachment.getFileId(), localFile.getFileName())); // 同步新上传的附件至平台 if (!StringUtils.equals(platformName, BugPlatform.LOCAL.getName())) { - File uploadTmpFile = new File(LocalRepositoryDir.getBugTmpDir() + "/" + localFile.getFileName()); + File uploadTmpFile = new File(FilenameUtils.normalize(LocalRepositoryDir.getBugTmpDir() + "/" + localFile.getFileName())); FileUtils.writeByteArrayToFile(uploadTmpFile, bytes); uploadPlatformAttachments.add(new SyncAttachmentToPlatformRequest(platformBug.getPlatformBugKey(), uploadTmpFile, SyncAttachmentType.UPLOAD.syncOperateType())); } @@ -1091,7 +1092,7 @@ public class BugService { fileService.upload(file, fileRequest); // 同步新上传的附件至平台 if (!StringUtils.equals(platformName, BugPlatform.LOCAL.getName())) { - File uploadTmpFile = new File(LocalRepositoryDir.getBugTmpDir() + "/" + file.getOriginalFilename()); + File uploadTmpFile = new File(FilenameUtils.normalize(LocalRepositoryDir.getBugTmpDir() + File.separator + file.getOriginalFilename())); FileUtils.writeByteArrayToFile(uploadTmpFile, file.getBytes()); uploadPlatformAttachments.add(new SyncAttachmentToPlatformRequest(platformBug.getPlatformBugKey(), uploadTmpFile, SyncAttachmentType.UPLOAD.syncOperateType())); } @@ -1115,7 +1116,7 @@ public class BugService { FileMetadata meta = fileMetadataMap.get(fileId); if (meta != null) { try { - File uploadTmpFile = new File(LocalRepositoryDir.getBugTmpDir() + "/" + meta.getName() + "." + meta.getType()); + File uploadTmpFile = new File(FilenameUtils.normalize(LocalRepositoryDir.getBugTmpDir() + File.separator + meta.getName() + "." + meta.getType())); byte[] fileByte = fileMetadataService.getFileByte(meta); FileUtils.writeByteArrayToFile(uploadTmpFile, fileByte); uploadPlatformAttachments.add(new SyncAttachmentToPlatformRequest(platformBug.getPlatformBugKey(), uploadTmpFile, SyncAttachmentType.UPLOAD.syncOperateType())); @@ -1184,7 +1185,7 @@ public class BugService { FileRequest downloadRequest = buildTmpImageFileRequest(tmpFileId); try { byte[] tmpBytes = fileService.download(downloadRequest); - File uploadTmpFile = new File(LocalRepositoryDir.getBugTmpDir() + "/" + tmpFileId + "/" + downloadRequest.getFileName()); + File uploadTmpFile = new File(FilenameUtils.normalize(LocalRepositoryDir.getBugTmpDir() + File.separator + tmpFileId + File.separator + downloadRequest.getFileName())); FileUtils.writeByteArrayToFile(uploadTmpFile, tmpBytes); platformRequest.getRichFileMap().put(tmpFileId, uploadTmpFile); } catch (Exception e) {