feat(项目设置): 文件下载的时候增加“临时缓存文件”功能
This commit is contained in:
parent
3d3b1cac7f
commit
92cfdfb771
|
@ -1,5 +1,6 @@
|
||||||
package io.metersphere.sdk.util;
|
package io.metersphere.sdk.util;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
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) {
|
public static void deleteTmpFile(String fileId) {
|
||||||
File file = new File(getImgFileTmpPath(fileId));
|
try {
|
||||||
if (file.exists()) {
|
File file = new File(getPreviewImgFilePath(fileId));
|
||||||
file.delete();
|
FileUtils.forceDelete(file);
|
||||||
|
file = new File(getTmpFilePath(fileId));
|
||||||
|
FileUtils.forceDelete(file);
|
||||||
|
} catch (Exception ignore) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//压缩图片
|
||||||
public static String catchCompressImgIfNotExists(String fileId, byte[] fileBytes) {
|
public static String catchCompressImgIfNotExists(String fileId, byte[] fileBytes) {
|
||||||
try {
|
try {
|
||||||
String previewPath = getImgFileTmpPath(fileId);
|
String previewPath = getPreviewImgFilePath(fileId);
|
||||||
compressPic(fileBytes, previewPath);
|
compressPic(fileBytes, previewPath);
|
||||||
return previewPath;
|
return previewPath;
|
||||||
} catch (Exception ignore) {
|
} 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);
|
File file = new File(filePath);
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
file.delete();
|
file.delete();
|
||||||
|
@ -117,15 +130,17 @@ public class TempFileUtils {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LogUtils.error(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();
|
return file.exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] getPreviewFile(String filePreviewPath) {
|
public static byte[] getFile(String filePath) {
|
||||||
File file = new File(filePreviewPath);
|
File file = new File(filePath);
|
||||||
byte[] previewByte = new byte[0];
|
byte[] previewByte = new byte[0];
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
try (FileInputStream fis = new FileInputStream(file);
|
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.constants.StorageType;
|
||||||
import io.metersphere.sdk.exception.MSException;
|
import io.metersphere.sdk.exception.MSException;
|
||||||
import io.metersphere.sdk.util.JSON;
|
import io.metersphere.sdk.util.JSON;
|
||||||
import io.metersphere.sdk.util.LogUtils;
|
|
||||||
import io.metersphere.sdk.util.TempFileUtils;
|
import io.metersphere.sdk.util.TempFileUtils;
|
||||||
import io.metersphere.sdk.util.Translator;
|
import io.metersphere.sdk.util.Translator;
|
||||||
import io.metersphere.system.dto.sdk.RemoteFileAttachInfo;
|
import io.metersphere.system.dto.sdk.RemoteFileAttachInfo;
|
||||||
|
@ -199,10 +198,23 @@ public class FileMetadataService {
|
||||||
return fileService.upload(file, uploadFileRequest);
|
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);
|
FileMetadata fileMetadata = fileMetadataMapper.selectByPrimaryKey(id);
|
||||||
byte[] bytes = this.getFile(fileMetadata);
|
byte[] bytes = this.getFileByte(fileMetadata);
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.contentType(MediaType.parseMediaType("application/octet-stream"))
|
.contentType(MediaType.parseMediaType("application/octet-stream"))
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + this.getFileName(fileMetadata.getId(), fileMetadata.getType()) + "\"")
|
.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) {
|
public byte[] batchDownload(List<FileMetadata> fileMetadataList) {
|
||||||
Map<String, byte[]> files = new LinkedHashMap<>();
|
Map<String, byte[]> files = new LinkedHashMap<>();
|
||||||
fileMetadataList.forEach(fileMetadata -> {
|
fileMetadataList.forEach(fileMetadata -> {
|
||||||
byte[] bytes;
|
byte[] bytes = this.getFileByte(fileMetadata);
|
||||||
try {
|
files.put(this.getFileName(fileMetadata.getName(), fileMetadata.getType()), bytes);
|
||||||
bytes = this.getFile(fileMetadata);
|
|
||||||
if (bytes != null) {
|
|
||||||
files.put(this.getFileName(fileMetadata.getName(), fileMetadata.getType()), bytes);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
LogUtils.error("下载文件失败", e);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return FileDownloadUtils.listBytesToZip(files);
|
return FileDownloadUtils.listBytesToZip(files);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -377,8 +381,8 @@ public class FileMetadataService {
|
||||||
FileMetadata fileMetadata = fileMetadataMapper.selectByPrimaryKey(id);
|
FileMetadata fileMetadata = fileMetadataMapper.selectByPrimaryKey(id);
|
||||||
String previewImgPath = null;
|
String previewImgPath = null;
|
||||||
if (TempFileUtils.isImage(fileMetadata.getType())) {
|
if (TempFileUtils.isImage(fileMetadata.getType())) {
|
||||||
if (TempFileUtils.isImgFileExists(fileMetadata.getId())) {
|
if (TempFileUtils.isImgPreviewFileExists(fileMetadata.getId())) {
|
||||||
previewImgPath = TempFileUtils.getImgFileTmpPath(fileMetadata.getId());
|
previewImgPath = TempFileUtils.getPreviewImgFilePath(fileMetadata.getId());
|
||||||
} else {
|
} else {
|
||||||
previewImgPath = TempFileUtils.catchCompressImgIfNotExists(fileMetadata.getId(), this.getFile(fileMetadata));
|
previewImgPath = TempFileUtils.catchCompressImgIfNotExists(fileMetadata.getId(), this.getFile(fileMetadata));
|
||||||
}
|
}
|
||||||
|
@ -386,7 +390,7 @@ public class FileMetadataService {
|
||||||
|
|
||||||
byte[] bytes;
|
byte[] bytes;
|
||||||
if (StringUtils.isNotBlank(previewImgPath)) {
|
if (StringUtils.isNotBlank(previewImgPath)) {
|
||||||
bytes = TempFileUtils.getPreviewFile(previewImgPath);
|
bytes = TempFileUtils.getFile(previewImgPath);
|
||||||
} else {
|
} else {
|
||||||
bytes = new byte[]{};
|
bytes = new byte[]{};
|
||||||
}
|
}
|
||||||
|
|
|
@ -440,6 +440,23 @@ public class FileManagementControllerTests extends BaseTest {
|
||||||
Assertions.assertTrue(uploadedFileTypes.contains(fileType));
|
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节点
|
//文件上传到a1-a1节点
|
||||||
BaseTreeNode a1a1Node = FileManagementBaseUtils.getNodeByName(preliminaryTreeNodes, "a1-a1");
|
BaseTreeNode a1a1Node = FileManagementBaseUtils.getNodeByName(preliminaryTreeNodes, "a1-a1");
|
||||||
fileUploadRequest = new FileUploadRequest();
|
fileUploadRequest = new FileUploadRequest();
|
||||||
|
@ -620,7 +637,6 @@ public class FileManagementControllerTests extends BaseTest {
|
||||||
String downloadMD5 = FileManagementBaseUtils.getFileMD5(fileBytes);
|
String downloadMD5 = FileManagementBaseUtils.getFileMD5(fileBytes);
|
||||||
Assertions.assertEquals(fileMD5, downloadMD5);
|
Assertions.assertEquals(fileMD5, downloadMD5);
|
||||||
}
|
}
|
||||||
|
|
||||||
//测试权限
|
//测试权限
|
||||||
this.requestGetPermissionTest(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_DOWNLOAD, String.format(FileManagementRequestUtils.URL_FILE_DOWNLOAD, picFileId));
|
this.requestGetPermissionTest(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_DOWNLOAD, String.format(FileManagementRequestUtils.URL_FILE_DOWNLOAD, picFileId));
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
这是一个隐藏文件
|
Loading…
Reference in New Issue