diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/repository/GitFileRepository.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/repository/GitFileRepository.java index d52f18bfb7..81081e0d0a 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/repository/GitFileRepository.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/repository/GitFileRepository.java @@ -10,7 +10,6 @@ import io.metersphere.metadata.vo.RepositoryRequest; import org.apache.commons.collections.CollectionUtils; import org.springframework.web.multipart.MultipartFile; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -47,7 +46,10 @@ public class GitFileRepository implements FileRepository { @Override public InputStream getFileAsStream(FileRequest request) throws Exception { - return new ByteArrayInputStream(getFile(request)); + RemoteFileAttachInfo gitFileInfo = request.getFileAttachInfo(); + GitRepositoryUtil repositoryUtils = new GitRepositoryUtil( + gitFileInfo.getRepositoryPath(), gitFileInfo.getUserName(), gitFileInfo.getToken()); + return repositoryUtils.getFileAsStream(gitFileInfo.getCommitId(), gitFileInfo.getFilePath()); } @Override diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/utils/GitRepositoryUtil.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/utils/GitRepositoryUtil.java index 70acea50ba..bf9cda82d3 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/utils/GitRepositoryUtil.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/metadata/utils/GitRepositoryUtil.java @@ -23,6 +23,7 @@ import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.filter.PathFilter; +import java.io.InputStream; import java.util.*; import java.util.stream.Collectors; @@ -214,4 +215,19 @@ public class GitRepositoryUtil { return returnList; } + + public InputStream getFileAsStream(String commitId, String filePath) throws Exception { + InMemoryRepository repo = this.getGitRepositoryInMemory(this.repositoryUrl, this.userName, this.token); + ObjectId fileCommitObjectId = repo.resolve(commitId); + RevWalk revWalk = new RevWalk(repo); + RevCommit commit = revWalk.parseCommit(fileCommitObjectId); + RevTree tree = commit.getTree(); + TreeWalk treeWalk = new TreeWalk(repo); + treeWalk.addTree(tree); + treeWalk.setRecursive(true); + treeWalk.setFilter(PathFilter.create(filePath)); + ObjectId objectId = treeWalk.getObjectId(0); + ObjectLoader loader = repo.open(objectId); + return loader.openStream(); + } }