Skip to content

Commit

Permalink
bugfix(controller): override contentLength in InputStreamResource ret…
Browse files Browse the repository at this point in the history
…urned by ModelService.getFileData so that the underlying InputStream won't be read twice (#2409)
  • Loading branch information
xuchuan authored Jun 26, 2023
1 parent ffeccdb commit b257518
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ public ResponseEntity<ResponseMessage<ListFilesResult>> listFiles(
@Override
public ResponseEntity<InputStreamResource> getFileData(String project, String model, String version, String path) {
var result = this.modelService.getFileData(project, model, version, path);
return ResponseEntity.ok(new InputStreamResource(result));
return ResponseEntity.ok(new InputStreamResource(result) {
@Override
public long contentLength() {
return result.getSize();
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import ai.starwhale.mlops.exception.SwValidationException;
import ai.starwhale.mlops.exception.SwValidationException.ValidSubject;
import ai.starwhale.mlops.exception.api.StarwhaleApiException;
import ai.starwhale.mlops.storage.LengthAbleInputStream;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
Expand Down Expand Up @@ -602,20 +603,21 @@ private FileNode convert(ModelPackageStorage.File file) {
.build();
}

public InputStream getFileData(String project, String model, String version, String path) {
public LengthAbleInputStream getFileData(String project, String model, String version, String path) {
return this.getFileData(this.getModelMetaBlob(project, model, version, ""), path);
}

private InputStream getFileData(ModelPackageStorage.MetaBlob metaBlob, String path) {
private LengthAbleInputStream getFileData(ModelPackageStorage.MetaBlob metaBlob, String path) {
var files = new LinkedList<>(this.getFile(metaBlob, path));
if (files.get(0).getSize() == 0) {
return new ByteArrayInputStream(new byte[0]);
var size = files.get(0).getSize();
if (size == 0) {
return new LengthAbleInputStream(new ByteArrayInputStream(new byte[0]), 0);
}
var compressionAlgorithm = files.get(0).getCompressionAlgorithm();
if (files.get(0).getBlobIdsCount() == 0) {
files.removeFirst();
}
return new SequenceInputStream(new Enumeration<>() {
var in = new SequenceInputStream(new Enumeration<>() {
int index = 0;

@Override
Expand Down Expand Up @@ -656,6 +658,7 @@ public InputStream nextElement() {
}
}
});
return new LengthAbleInputStream(in, size);
}

private InputStream readFileData(
Expand Down

0 comments on commit b257518

Please sign in to comment.