feat(项目设置): 文件下载的时候增加“临时缓存文件”功能
This commit is contained in:
parent
3d3b1cac7f
commit
92cfdfb771
|
@ -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);
|
||||
|
|
|
@ -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<byte[]> 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<byte[]> 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<FileMetadata> fileMetadataList) {
|
||||
Map<String, byte[]> 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[]{};
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
这是一个隐藏文件
|
Loading…
Reference in New Issue