Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: handle the scene of file not exist #475

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,17 @@ public Result getSmartTestGraphXML(@CurrentSecurityContext SysUser requestor,
if (requestor == null) {
return Result.error(HttpStatus.UNAUTHORIZED.value(), "unauthorized");
}
File graphZipFile = loadGraphFile(fileId);

File graphFile = new File(graphZipFile.getParentFile().getAbsolutePath(), Const.SmartTestConfig.GRAPH_FILE_NAME);
try (FileInputStream in = new FileInputStream(graphFile);
ServletOutputStream out = response.getOutputStream()) {
try {
File graphZipFile = loadGraphFile(fileId);

File graphFile = new File(graphZipFile.getParentFile().getAbsolutePath(), Const.SmartTestConfig.GRAPH_FILE_NAME);
if (!graphFile.exists()) {
throw new HydraLabRuntimeException("Graph xml file not found");
}

FileInputStream in = new FileInputStream(graphFile);
ServletOutputStream out = response.getOutputStream();

response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
int len;
Expand All @@ -258,6 +264,12 @@ public Result getSmartTestGraphXML(@CurrentSecurityContext SysUser requestor,
out.write(buffer, 0, len);
}
out.flush();
} catch (HydraLabRuntimeException e) {
logger.error(e.getMessage(), e);
return Result.error(e.getCode(), e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage(), e);
return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e);
} finally {
response.flushBuffer();
}
Expand All @@ -272,19 +284,30 @@ public Result getSmartTestGraphPhoto(@CurrentSecurityContext SysUser requestor,
if (requestor == null) {
return Result.error(HttpStatus.UNAUTHORIZED.value(), "unauthorized");
}
File graphZipFile = loadGraphFile(fileId);
if (!LogUtils.isLegalStr(node, Const.RegexString.INTEGER, false)) {
return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Error param! Should be Integer");
}
File nodeFile = new File(graphZipFile.getParentFile().getAbsolutePath(), node + "/" + node + "-0.jpg");
try (FileInputStream inputStream = new FileInputStream(nodeFile);
ServletOutputStream out = response.getOutputStream()) {
try {
File graphZipFile = loadGraphFile(fileId);

File nodeFile = new File(graphZipFile.getParentFile().getAbsolutePath(), node + "/" + node + "-0.jpg");

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression

This path depends on a [user-provided value](1).
if (!nodeFile.exists()) {
throw new HydraLabRuntimeException("Graph photo file not found");
}

FileInputStream inputStream = new FileInputStream(nodeFile);

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression

This path depends on a [user-provided value](1).
ServletOutputStream out = response.getOutputStream();
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes, 0, inputStream.available());
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
out.write(bytes);
out.flush();
} catch (HydraLabRuntimeException e) {
logger.error(e.getMessage(), e);
return Result.error(e.getCode(), e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage(), e);
return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e);
} finally {
response.flushBuffer();
}
Expand Down Expand Up @@ -316,7 +339,10 @@ public Result saveGPTSuggestion(@CurrentSecurityContext SysUser requestor,
}

private File loadGraphFile(String fileId) {
StorageFileInfo graphBlobFile = storageFileInfoRepository.findById(fileId).get();
StorageFileInfo graphBlobFile = storageFileInfoRepository.findById(fileId).orElse(null);
if (graphBlobFile == null) {
throw new HydraLabRuntimeException("Graph zip file not exist!");
}
File graphZipFile = new File(CENTER_TEMP_FILE_DIR, graphBlobFile.getBlobPath());

if (!graphZipFile.exists()) {
Expand Down
16 changes: 11 additions & 5 deletions react/src/component/TestReportView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,17 @@ export default class TestReportView extends BaseView {

loadGEXF = c => {
axios.get("/api/test/loadGraph/" + this.state.graphFileId).then((res) => {
this.setState({
graph: parse(Graph, String(res.data)),
container: c
})
this.loadGraph()
if(res.data.code && res.data.code === 500){
this.handleStatus("snackbarIsShown", true)
this.handleStatus("snackbarSeverity", "error")
this.handleStatus("snackbarMessage", res.data.message)
}else{
this.setState({
graph: parse(Graph, String(res.data)),
container: c
})
this.loadGraph()
}
})
}

Expand Down