fix(缺陷管理): 修复缺陷详情页上传文件不显示的问题

This commit is contained in:
song-tianyang 2024-03-01 17:55:41 +08:00 committed by wxg0103
parent 569ed4c933
commit bc9678eb90
6 changed files with 52 additions and 124 deletions

View File

@ -165,6 +165,15 @@ public class ApiDebugControllerTests extends BaseTest {
return file;
}
private static MockMultipartFile getMockMultipartFile(String fileName) {
return new MockMultipartFile(
"file",
fileName,
MediaType.APPLICATION_OCTET_STREAM_VALUE,
"Hello, World!".getBytes()
);
}
@Test
@Order(2)
public void add() throws Exception {
@ -687,17 +696,36 @@ public class ApiDebugControllerTests extends BaseTest {
request.setProjectId(DEFAULT_PROJECT_ID);
request.setModuleId("root");
request.setLocal(true);
uploadFileId = doUploadTempFile(getMockMultipartFile());
uploadFileId = doUploadTempFile(getMockMultipartFile("test-debug-file.txt"));
request.setFileId(uploadFileId);
this.requestPost(TRANSFER, request).andExpect(status().isOk());
//文件不存在
request.setFileId("111");
this.requestPost(TRANSFER, request).andExpect(status().is5xxServerError());
//文件已经上传
ApiDebugAddRequest addRequest = new ApiDebugAddRequest();
addRequest.setPath("http://test.com");
addRequest.setMethod("GET");
addRequest.setName("test-add-file");
addRequest.setProtocol(ApiConstants.HTTP_PROTOCOL);
addRequest.setModuleId("default");
addRequest.setProjectId(DEFAULT_PROJECT_ID);
MsHTTPElement msHttpElement = MsHTTPElementTest.getMsHttpElement();
msHttpElement.setBody(addBodyLinkFile(msHttpElement.getBody(), fileMetadataId));
addRequest.setRequest(getMsElementParam(msHttpElement));
uploadFileId = doUploadTempFile(getMockMultipartFile("test-debug-file1.txt"));
addRequest.setUploadFileIds(List.of(uploadFileId));
MvcResult mvcResult = this.requestPostWithOkAndReturn(DEFAULT_ADD, addRequest);
ApiDebug resultData = getResultData(mvcResult, ApiDebug.class);
ApiFileResourceExample apiFileResourceExample = new ApiFileResourceExample();
apiFileResourceExample.createCriteria().andResourceIdEqualTo(addApiDebug.getId());
apiFileResourceExample.createCriteria().andResourceIdEqualTo(resultData.getId());
List<ApiFileResource> apiFileResources = apiFileResourceMapper.selectByExample(apiFileResourceExample);
Assertions.assertFalse(apiFileResources.isEmpty());
request = new ApiTransferRequest();
request.setSourceId(resultData.getId());
request.setProjectId(DEFAULT_PROJECT_ID);
request.setModuleId("root");
request.setFileId(apiFileResources.get(0).getFileId());
this.requestPost(TRANSFER, request).andExpect(status().isOk());
}

View File

@ -198,7 +198,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
msHttpResponse.get(0).setBody(ApiDebugControllerTests.addBodyLinkFile(msHttpResponse.get(0).getBody(), fileMetadataId));
request.setResponse(msHttpResponse);
uploadFileId = doUploadTempFile(getMockMultipartFile("file_upload.JPG"));
uploadFileId = doUploadTempFile(getMockMultipartFile("api-add-file_upload.JPG"));
request.setUploadFileIds(List.of(uploadFileId));
request.setLinkFileIds(List.of(fileMetadataId));
@ -216,7 +216,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
apiTransferRequest.setProjectId(DEFAULT_PROJECT_ID);
apiTransferRequest.setModuleId("root");
apiTransferRequest.setLocal(true);
String uploadFileId = doUploadTempFile(getMockMultipartFile("file_upload.JPG"));
String uploadFileId = doUploadTempFile(getMockMultipartFile("api-file_upload.JPG"));
apiTransferRequest.setFileId(uploadFileId);
this.requestPost("/api/definition/transfer", apiTransferRequest).andExpect(status().isOk());
//文件不存在

View File

@ -218,10 +218,7 @@ public class FileMetadataService {
if (StringUtils.isBlank(fileName)) {
throw new MSException(Translator.get("file.name.cannot.be.empty"));
}
fileName = this.genTransferFileName(StringUtils.trim(fileName), projectId);
FileMetadata fileMetadata = this.genFileMetadata(fileName, StorageType.MINIO.name(), fileBytes.length, false, projectId, moduleId, operator);
FileRequest uploadFileRequest = new FileRequest();
uploadFileRequest.setFileName(fileMetadata.getId());
uploadFileRequest.setFolder(this.generateMinIOFilePath(projectId));
@ -235,39 +232,6 @@ public class FileMetadataService {
return fileMetadata.getId();
}
public String genTransferFileName(String fullFileName, String projectId) {
if (StringUtils.containsAny(fullFileName, "/")) {
throw new MSException(Translator.get("file.name.error"));
}
String fileName;
String fileType = null;
if (StringUtils.lastIndexOf(fullFileName, ".") > 0) {
//采用这种判断方式可以避免将隐藏文件的后缀名作为文件类型
fileName = StringUtils.substring(fullFileName, 0, fullFileName.lastIndexOf("."));
fileType = StringUtils.substring(fullFileName, fullFileName.lastIndexOf(".") + 1);
} else {
fileName = fullFileName;
}
FileMetadataExample example = new FileMetadataExample();
example.createCriteria().andNameEqualTo(fileName).andProjectIdEqualTo(projectId);
int fileIndex = 0;
String originFileName = fileName;
while (fileMetadataMapper.countByExample(example) > 0) {
fileIndex++;
fileName = originFileName + "(" + fileIndex + ")";
example = new FileMetadataExample();
example.createCriteria().andNameEqualTo(fileName).andProjectIdEqualTo(projectId);
}
if (StringUtils.isNotEmpty(fileType)) {
fileName = fileName + "." + fileType;
}
return fileName;
}
private void checkMinIOFileName(String id, String fileName, String type, String projectId) {
if (StringUtils.isBlank(fileName)) {
throw new MSException(Translator.get("file.name.cannot.be.empty"));

View File

@ -38,6 +38,7 @@ import io.metersphere.system.utils.Pager;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
@ -50,7 +51,6 @@ import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.nio.charset.StandardCharsets;
@ -1774,25 +1774,22 @@ public class FileManagementControllerTests extends BaseTest {
FileMetadataExample example = new FileMetadataExample();
example.createCriteria().andIdEqualTo(fileID).andNameEqualTo("testTransferFile").andTypeEqualTo("JPG");
Assertions.assertEquals(fileMetadataMapper.countByExample(example), 1);
//重复转存检查文件名是否加1
fileID = fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransferFile.jpg", TempFileUtils.getFile(filePath), "sty-file-association-bug-id-4", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
example.clear();
example.createCriteria().andIdEqualTo(fileID).andNameEqualTo("testTransferFile(1)").andTypeEqualTo("JPG");
//重复转存检查是否报错
boolean error = false;
try {
fileID = fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransferFile.jpg", TempFileUtils.getFile(filePath), "sty-file-association-bug-id-4", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
} catch (Exception e) {
error = true;
}
Assertions.assertTrue(error);
Assertions.assertEquals(fileMetadataMapper.countByExample(example), 1);
//重复转存检查文件名是否加1
fileID = fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransferFile.jpg", TempFileUtils.getFile(filePath), "sty-file-association-bug-id-4", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
example.clear();
example.createCriteria().andIdEqualTo(fileID).andNameEqualTo("testTransferFile(2)").andTypeEqualTo("JPG");
Assertions.assertEquals(fileMetadataMapper.countByExample(example), 1);
//测试没有后缀的文件名
fileID = fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransfer", TempFileUtils.getFile(filePath), "sty-file-association-bug-id-4", FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
example.clear();
example.createCriteria().andIdEqualTo(fileID).andNameEqualTo("testTransfer");
Assertions.assertEquals(fileMetadataMapper.countByExample(example), 1);
//资源不存在
boolean error = false;
error = false;
try {
fileAssociationService.transferAndAssociation(new FileAssociationDTO("testTransferFile.jpg", TempFileUtils.getFile(filePath), IDGenerator.nextStr(), FileAssociationSourceUtil.SOURCE_TYPE_BUG, fileLogRecord));
} catch (Exception e) {
@ -1816,14 +1813,6 @@ public class FileManagementControllerTests extends BaseTest {
error = true;
}
Assertions.assertTrue(error);
error = false;
try {
fileMetadataService.genTransferFileName("testTransfer/File.jpg", null);
} catch (Exception e) {
error = true;
}
Assertions.assertTrue(error);
}
public void associationDelete() {

View File

@ -24,41 +24,9 @@
<a-button type="secondary" @click="handleCancel">{{ t('common.cancel') }}</a-button>
<a-button class="ml-[12px]" type="primary" :loading="confirmLoading" @click="handleSave">
{{ t('common.save') }}
</a-button></div
>
<div v-if="props.allowEdit">
<div class="mt-[16px] font-medium text-[var(--color-text-1)]">
{{ t('bugManagement.edit.file') }}
</div>
<div class="mt-[16px] pb-[4px]">
<a-dropdown position="tr" trigger="hover">
<a-button v-permission="['PROJECT_BUG:READ+UPDATE']" type="outline">
<template #icon> <icon-plus class="text-[14px]" /> </template
>{{ t('system.orgTemplate.addAttachment') }}</a-button
>
<template #content>
<a-upload
ref="uploadRef"
v-model:file-list="fileList"
:auto-upload="false"
:show-file-list="false"
:before-upload="beforeUpload"
>
<template #upload-button>
<a-button type="text" class="!text-[var(--color-text-1)]">
<icon-upload />{{ t('caseManagement.featureCase.uploadFile') }}
</a-button>
</template>
</a-upload>
<a-button type="text" class="!text-[var(--color-text-1)]" @click="associatedFile">
<MsIcon type="icon-icon_link-copy_outlined" size="16" />
{{ t('caseManagement.featureCase.associatedFile') }}
</a-button>
</template>
</a-dropdown>
</div>
</a-button>
</div>
<div class="mb-[8px] mt-[2px] text-[var(--color-text-4)]">{{ t('bugManagement.edit.fileExtra') }}</div>
<AddAttachment v-model:file-list="fileList" @link-file="associatedFile"/>
<MsFileList
ref="fileListRef"
v-model:file-list="fileList"
@ -70,6 +38,7 @@
:upload-func="uploadOrAssociationFile"
:handle-delete="deleteFileHandler"
:show-delete="props.allowEdit"
@finish="uploadFileOver"
>
<template #actions="{ item }">
<div>
@ -167,6 +136,7 @@
import MsRichText from '@/components/pure/ms-rich-text/MsRichText.vue';
import MsFileList from '@/components/pure/ms-upload/fileList.vue';
import { MsFileItem } from '@/components/pure/ms-upload/types';
import AddAttachment from "@/components/business/ms-add-attachment/index.vue";
import RelateFileDrawer from '@/components/business/ms-link-file/associatedFileDrawer.vue';
import TransferModal from '@/views/case-management/caseManagementFeature/components/tabContent/transferModal.vue';
@ -375,6 +345,9 @@
async function startUpload() {
await sleep(300);
fileListRef.value?.startUpload();
}
async function uploadFileOver() {
emit('updateSuccess');
}

View File

@ -39,40 +39,13 @@
:upload-image="handleUploadImage"
/>
</a-form-item>
<a-form-item field="attachment" :label="t('bugManagement.edit.file')">
<a-form-item field="attachment">
<div class="flex flex-col">
<div class="mb-1">
<a-dropdown position="tr" trigger="hover">
<a-button type="outline">
<template #icon> <icon-plus class="text-[14px]" /> </template>
{{ t('bugManagement.edit.uploadFile') }}
</a-button>
<template #content>
<a-upload
ref="uploadRef"
v-model:file-list="fileList"
:auto-upload="false"
:show-file-list="false"
:before-upload="beforeUpload"
@change="handleChange"
>
<template #upload-button>
<a-button type="text" class="!text-[var(--color-text-1)]">
<icon-upload />{{ t('bugManagement.edit.localUpload') }}</a-button
>
</template>
</a-upload>
<a-button type="text" class="!text-[var(--color-text-1)]" @click="associatedFile">
<MsIcon type="icon-icon_link-copy_outlined" size="16" />{{
t('bugManagement.edit.linkFile')
}}</a-button
>
</template>
</a-dropdown>
<AddAttachment v-model:file-list="fileList" @change="handleChange" @link-file="associatedFile"/>
</div>
</div>
</a-form-item>
<div class="mb-[8px] mt-[2px] text-[var(--color-text-4)]">{{ t('bugManagement.edit.fileExtra') }}</div>
<MsFileList ref="fileListRef" v-model:file-list="fileList" mode="static">
<template #actions="{ item }">
<!-- 本地文件 -->
@ -200,6 +173,7 @@
import MsFileList from '@/components/pure/ms-upload/fileList.vue';
import MsUpload from '@/components/pure/ms-upload/index.vue';
import { MsFileItem } from '@/components/pure/ms-upload/types';
import AddAttachment from '@/components/business/ms-add-attachment/index.vue';
import RelateFileDrawer from '@/components/business/ms-link-file/associatedFileDrawer.vue';
import TransferModal from '@/views/case-management/caseManagementFeature/components/tabContent/transferModal.vue';