fix: 文件上传接口优化 (#3118)

Co-authored-by: chenjianxing <jianxing.chen@fit2cloud.com>
Co-authored-by: jianxing <41557596+AgAngle@users.noreply.github.com>
This commit is contained in:
metersphere-bot 2021-05-25 19:03:19 +08:00 committed by GitHub
parent a4c6d24e14
commit b3fb62cdab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 44 deletions

View File

@ -49,10 +49,7 @@ import org.apache.jorphan.collections.ListedHashTree;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@ -319,6 +316,25 @@ public abstract class MsTestElement {
return false;
}
}
public static <T> List<T> findFromHashTreeByType(MsTestElement hashTree, Class<T> clazz, List<T> requests) {
if (requests == null) {
requests = new ArrayList<>();
}
if (clazz.isInstance(hashTree)) {
requests.add((T) hashTree);
} else {
if (hashTree != null) {
LinkedList<MsTestElement> childHashTree = hashTree.getHashTree();
if (CollectionUtils.isNotEmpty(childHashTree)) {
for (MsTestElement item : childHashTree) {
findFromHashTreeByType(item, clazz, requests);
}
}
}
}
return requests;
}
}

View File

@ -658,23 +658,8 @@ public class MsHTTPSamplerProxy extends MsTestElement {
return this.getRest().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).toArray().length > 0;
}
public static List<MsHTTPSamplerProxy> findHttpSampleFromHashTree(MsTestElement hashTree, List<MsHTTPSamplerProxy> requests) {
if (requests == null) {
requests = new ArrayList<>();
}
if (hashTree instanceof MsHTTPSamplerProxy) {
requests.add((MsHTTPSamplerProxy) hashTree);
} else {
if (hashTree != null) {
LinkedList<MsTestElement> childHashTree = hashTree.getHashTree();
if (CollectionUtils.isNotEmpty(childHashTree)) {
for (MsTestElement item : childHashTree) {
findHttpSampleFromHashTree(item, requests);
}
}
}
}
return requests;
public static List<MsHTTPSamplerProxy> findHttpSampleFromHashTree(MsTestElement hashTree) {
return findFromHashTreeByType(hashTree, MsHTTPSamplerProxy.class, null);
}
}

View File

@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.scenario.request.BodyFile;
import io.metersphere.commons.json.JSONSchemaGenerator;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.ScriptEngineUtils;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
@ -24,6 +23,7 @@ public class Body {
private List<KeyValue> kvs;
private List<KeyValue> binary;
private Object jsonSchema;
private String tmpFilePath;
public final static String KV = "KeyValue";
public final static String FORM_DATA = "Form Data";
@ -131,6 +131,8 @@ public class Body {
if (StringUtils.isNotBlank(file.getId())) {
// 旧数据
path = FileUtils.BODY_FILE_DIR + '/' + file.getId() + '_' + file.getName();
} else if (StringUtils.isNotBlank(this.tmpFilePath)) {
path = FileUtils.BODY_FILE_DIR + '/' + this.tmpFilePath + '/' + file.getName();
} else {
path = FileUtils.BODY_FILE_DIR + '/' + requestId + '/' + file.getName();
}
@ -167,5 +169,4 @@ public class Body {
this.binary = new ArrayList<>();
this.binary.add(new KeyValue());
}
}

View File

@ -327,7 +327,7 @@ public class ApiAutomationService {
ApiScenarioWithBLOBs oldScenario = apiScenarioMapper.selectByPrimaryKey(scenario.getId());
Set<String> newRequestIds = getRequestIds(scenario.getScenarioDefinition());
MsTestElement msTestElement = parseScenarioDefinition(oldScenario.getScenarioDefinition());
List<MsHTTPSamplerProxy> oldRequests = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msTestElement, null);
List<MsHTTPSamplerProxy> oldRequests = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msTestElement);
oldRequests.forEach(item -> {
if (item.isCustomizeReq() && !newRequestIds.contains(item.getId())) {
FileUtils.deleteBodyFiles(item.getId());
@ -343,7 +343,7 @@ public class ApiAutomationService {
public Set<String> getRequestIds(String scenarioDefinition) {
MsScenario msScenario = parseScenarioDefinition(scenarioDefinition);
List<MsHTTPSamplerProxy> httpSampleFromHashTree = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msScenario, null);
List<MsHTTPSamplerProxy> httpSampleFromHashTree = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msScenario);
return httpSampleFromHashTree.stream()
.map(MsHTTPSamplerProxy::getId).collect(Collectors.toSet());
}
@ -442,7 +442,7 @@ public class ApiAutomationService {
public void deleteBodyFile(String scenarioDefinition) {
MsTestElement msTestElement = parseScenarioDefinition(scenarioDefinition);
List<MsHTTPSamplerProxy> httpSampleFromHashTree = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msTestElement, null);
List<MsHTTPSamplerProxy> httpSampleFromHashTree = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msTestElement);
httpSampleFromHashTree.forEach((httpSamplerProxy) -> {
if (httpSamplerProxy.isCustomizeReq()) {
FileUtils.deleteBodyFiles(httpSamplerProxy.getId());

View File

@ -15,6 +15,7 @@ import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.ScheduleInfoSwaggerUrlRequest;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.scenario.request.RequestType;
import io.metersphere.api.dto.swaggerurl.SwaggerTaskResult;
@ -108,8 +109,6 @@ public class ApiDefinitionService {
private static Cache cache = Cache.newHardMemoryCache(0, 3600 * 24);
private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR;
public List<ApiDefinitionResult> list(ApiDefinitionRequest request) {
request = this.initRequest(request, true, true);
List<ApiDefinitionResult> resList = extApiDefinitionMapper.list(request);
@ -576,16 +575,17 @@ public class ApiDefinitionService {
}
}
List<MsHTTPSamplerProxy> requests = MsHTTPSamplerProxy.findHttpSampleFromHashTree(request.getTestElement(), null);
// 单接口调试生成tmp临时目录
requests.forEach(item -> {
String originId = item.getId();
item.setId("tmp/" + UUID.randomUUID().toString());
FileUtils.copyBdyFile(originId, item.getId());
FileUtils.createBodyFiles(item.getId(), bodyFiles);
});
if (CollectionUtils.isNotEmpty(bodyFiles)) {
List<MsHTTPSamplerProxy> requests = MsHTTPSamplerProxy.findHttpSampleFromHashTree(request.getTestElement());
// 单接口调试生成tmp临时目录
requests.forEach(item -> {
Body body = item.getBody();
String tmpFilePath = "tmp/" + UUID.randomUUID().toString();
body.setTmpFilePath(tmpFilePath);
FileUtils.copyBdyFile(item.getId(), tmpFilePath);
FileUtils.createBodyFiles(tmpFilePath, bodyFiles);
});
}
HashTree hashTree = request.getTestElement().generateHashTree(config);
String runMode = ApiRunMode.DEFINITION.name();

View File

@ -359,13 +359,10 @@
this.runData.projectId = this.request.projectId;
this.request.useEnvironment = this.currentEnvironmentId;
this.request.customizeReq = this.isCustomizeReq;
let requestParam = JSON.parse(JSON.stringify(this.request));
//
requestParam.enable = true;
let debugData = {
id: this.currentScenario.id, name: this.currentScenario.name, type: "scenario",
variables: this.currentScenario.variables, referenced: 'Created', headers: this.currentScenario.headers,
enableCookieShare: this.enableCookieShare, environmentId: this.currentEnvironmentId, hashTree: [requestParam],
enableCookieShare: this.enableCookieShare, environmentId: this.currentEnvironmentId, hashTree: [this.request],
};
this.runData.push(debugData);
/*触发执行操作*/

View File

@ -7,11 +7,11 @@
<el-dropdown-menu>
<el-dropdown-item command="STEP">
<div>{{ $t('test_track.case.step_describe') }}</div>
<div>{{ $t('test_track.case.step_describe_tip') }}</div>
<!-- <div>{{ $t('test_track.case.step_describe_tip') }}</div>-->
</el-dropdown-item>
<el-dropdown-item command="TEXT">
<div>{{ $t('test_track.case.text_describe') }}</div>
<div>{{ $t('test_track.case.text_describe_tip') }}</div>
<!-- <div>{{ $t('test_track.case.text_describe_tip') }}</div>-->
</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>