diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/TempFileUtils.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/TempFileUtils.java index d1d9173352..93ef353a2f 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/TempFileUtils.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/TempFileUtils.java @@ -1,5 +1,6 @@ package io.metersphere.sdk.util; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import javax.imageio.ImageIO; @@ -26,20 +27,32 @@ public class TempFileUtils { } - public static String getImgFileTmpPath(String fileId) { - return TEMP_FILE_FOLDER + fileId + ".jpg"; + //获取缩略图路径 + public static String getPreviewImgFilePath(String fileId) { + return TEMP_FILE_FOLDER + "preview/" + fileId + ".jpg"; + } + + //获取临时文件路径 + public static String getTmpFilePath(String fileId) { + return TEMP_FILE_FOLDER + "tmp/" + fileId; } public static void deleteTmpFile(String fileId) { - File file = new File(getImgFileTmpPath(fileId)); - if (file.exists()) { - file.delete(); + try { + File file = new File(getPreviewImgFilePath(fileId)); + FileUtils.forceDelete(file); + file = new File(getTmpFilePath(fileId)); + FileUtils.forceDelete(file); + } catch (Exception ignore) { + } } + + //压缩图片 public static String catchCompressImgIfNotExists(String fileId, byte[] fileBytes) { try { - String previewPath = getImgFileTmpPath(fileId); + String previewPath = getPreviewImgFilePath(fileId); compressPic(fileBytes, previewPath); return previewPath; } catch (Exception ignore) { @@ -93,7 +106,7 @@ public class TempFileUtils { } - private static void createFile(String filePath, byte[] fileBytes) { + public static String createFile(String filePath, byte[] fileBytes) { File file = new File(filePath); if (file.exists()) { file.delete(); @@ -117,15 +130,17 @@ public class TempFileUtils { } catch (IOException e) { LogUtils.error(e); } + return filePath; } - public static boolean isImgFileExists(String fileId) { - File file = new File(getImgFileTmpPath(fileId)); + //图片缩略图是否存在 + public static boolean isImgPreviewFileExists(String fileId) { + File file = new File(getPreviewImgFilePath(fileId)); return file.exists(); } - public static byte[] getPreviewFile(String filePreviewPath) { - File file = new File(filePreviewPath); + public static byte[] getFile(String filePath) { + File file = new File(filePath); byte[] previewByte = new byte[0]; if (file.exists()) { try (FileInputStream fis = new FileInputStream(file); diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java index e6a8625a15..e9bb683ff8 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/FileMetadataService.java @@ -19,7 +19,6 @@ import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.constants.StorageType; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.JSON; -import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.TempFileUtils; import io.metersphere.sdk.util.Translator; import io.metersphere.system.dto.sdk.RemoteFileAttachInfo; @@ -199,10 +198,23 @@ public class FileMetadataService { return fileService.upload(file, uploadFileRequest); } - public ResponseEntity downloadById(String id) throws Exception { + public byte[] getFileByte(FileMetadata fileMetadata) { + String filePath = null; + if (TempFileUtils.isImgPreviewFileExists(fileMetadata.getId())) { + filePath = TempFileUtils.getTmpFilePath(fileMetadata.getId()); + } else { + try { + filePath = TempFileUtils.createFile(TempFileUtils.getTmpFilePath(fileMetadata.getId()), this.getFile(fileMetadata)); + } catch (Exception ignore) { + } + } + byte[] bytes = TempFileUtils.getFile(filePath); + return bytes; + } + public ResponseEntity downloadById(String id) { FileMetadata fileMetadata = fileMetadataMapper.selectByPrimaryKey(id); - byte[] bytes = this.getFile(fileMetadata); + byte[] bytes = this.getFileByte(fileMetadata); return ResponseEntity.ok() .contentType(MediaType.parseMediaType("application/octet-stream")) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + this.getFileName(fileMetadata.getId(), fileMetadata.getType()) + "\"") @@ -294,17 +306,9 @@ public class FileMetadataService { public byte[] batchDownload(List fileMetadataList) { Map files = new LinkedHashMap<>(); fileMetadataList.forEach(fileMetadata -> { - byte[] bytes; - try { - bytes = this.getFile(fileMetadata); - if (bytes != null) { - files.put(this.getFileName(fileMetadata.getName(), fileMetadata.getType()), bytes); - } - } catch (Exception e) { - LogUtils.error("下载文件失败", e); - } + byte[] bytes = this.getFileByte(fileMetadata); + files.put(this.getFileName(fileMetadata.getName(), fileMetadata.getType()), bytes); }); - return FileDownloadUtils.listBytesToZip(files); } @@ -377,8 +381,8 @@ public class FileMetadataService { FileMetadata fileMetadata = fileMetadataMapper.selectByPrimaryKey(id); String previewImgPath = null; if (TempFileUtils.isImage(fileMetadata.getType())) { - if (TempFileUtils.isImgFileExists(fileMetadata.getId())) { - previewImgPath = TempFileUtils.getImgFileTmpPath(fileMetadata.getId()); + if (TempFileUtils.isImgPreviewFileExists(fileMetadata.getId())) { + previewImgPath = TempFileUtils.getPreviewImgFilePath(fileMetadata.getId()); } else { previewImgPath = TempFileUtils.catchCompressImgIfNotExists(fileMetadata.getId(), this.getFile(fileMetadata)); } @@ -386,7 +390,7 @@ public class FileMetadataService { byte[] bytes; if (StringUtils.isNotBlank(previewImgPath)) { - bytes = TempFileUtils.getPreviewFile(previewImgPath); + bytes = TempFileUtils.getFile(previewImgPath); } else { bytes = new byte[]{}; } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java index 6e79866280..1f54917f55 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/filemanagement/FileManagementControllerTests.java @@ -440,6 +440,23 @@ public class FileManagementControllerTests extends BaseTest { Assertions.assertTrue(uploadedFileTypes.contains(fileType)); } + //上传隐藏文件 .yincangwenjian + filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/.yincangwenjian")).getPath(); + file = new MockMultipartFile("file", ".yincangwenjian", MediaType.APPLICATION_OCTET_STREAM_VALUE, FileManagementBaseUtils.getFileBytes(filePath)); + paramMap = new LinkedMultiValueMap<>(); + paramMap.add("file", file); + paramMap.add("request", JSON.toJSONString(fileUploadRequest)); + mvcResult = this.requestMultipartWithOkAndReturn(FileManagementRequestUtils.URL_FILE_UPLOAD, paramMap); + returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString(); + checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD); + FILE_ID_PATH.put(returnId, filePath); + uploadedFileTypes.add(""); + + //检查文件类型是不是为空 + FileMetadata fileMetadata = fileMetadataMapper.selectByPrimaryKey(returnId); + Assertions.assertEquals(fileMetadata.getName(), ".yincangwenjian"); + Assertions.assertEquals(fileMetadata.getType(), StringUtils.EMPTY); + //文件上传到a1-a1节点 BaseTreeNode a1a1Node = FileManagementBaseUtils.getNodeByName(preliminaryTreeNodes, "a1-a1"); fileUploadRequest = new FileUploadRequest(); @@ -620,7 +637,6 @@ public class FileManagementControllerTests extends BaseTest { String downloadMD5 = FileManagementBaseUtils.getFileMD5(fileBytes); Assertions.assertEquals(fileMD5, downloadMD5); } - //测试权限 this.requestGetPermissionTest(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_DOWNLOAD, String.format(FileManagementRequestUtils.URL_FILE_DOWNLOAD, picFileId)); } diff --git a/backend/services/project-management/src/test/resources/file/.yincangwenjian b/backend/services/project-management/src/test/resources/file/.yincangwenjian new file mode 100644 index 0000000000..a9fdb5a88e --- /dev/null +++ b/backend/services/project-management/src/test/resources/file/.yincangwenjian @@ -0,0 +1 @@ +这是一个隐藏文件 \ No newline at end of file