Skip to content

Commit cb10f7a

Browse files
author
Mark Robinson
committed
Add total file size limit to the content of the repository at that path
Closes #13
1 parent 4d1093e commit cb10f7a

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed

src/main/java/org/commonwl/viewer/domain/CWLCollection.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import org.apache.commons.io.FileUtils;
2828
import org.apache.commons.io.FilenameUtils;
2929
import org.commonwl.viewer.services.DockerService;
30-
import org.eclipse.egit.github.core.RepositoryContents;
3130
import org.commonwl.viewer.services.GitHubService;
31+
import org.eclipse.egit.github.core.RepositoryContents;
3232
import org.yaml.snakeyaml.Yaml;
3333

3434
import java.io.IOException;
@@ -44,6 +44,8 @@ public class CWLCollection {
4444
private GithubDetails githubInfo;
4545
private String commitSha;
4646

47+
private int totalFileSize;
48+
private int totalFileSizeLimit;
4749
private int singleFileSizeLimit;
4850

4951
// Maps of ID to associated JSON
@@ -93,11 +95,13 @@ public class CWLCollection {
9395
* @throws IOException Any API errors which may have occurred
9496
*/
9597
public CWLCollection(GitHubService githubService, GithubDetails githubInfo,
96-
String commitSha, int singleFileSizeLimit) throws IOException {
98+
String commitSha, int singleFileSizeLimit, int totalFileSizeLimit) throws IOException {
9799
this.githubInfo = githubInfo;
98100
this.githubService = githubService;
99101
this.commitSha = commitSha;
100102
this.singleFileSizeLimit = singleFileSizeLimit;
103+
this.totalFileSizeLimit = totalFileSizeLimit;
104+
this.totalFileSize = 0;
101105

102106
// Add any CWL files from the Github repo to this collection
103107
List<RepositoryContents> repoContents = githubService.getContents(githubInfo);
@@ -123,37 +127,41 @@ private void addDocs(List<RepositoryContents> repoContents) throws IOException {
123127
// Add the files in the subdirectory to this new folder
124128
addDocs(subdirectory);
125129

126-
// Otherwise this is a file so add to the bundle
127130
} else if (repoContent.getType().equals(FILE)) {
128-
129-
// Get the file extension
130-
int eIndex = repoContent.getName().lastIndexOf('.') + 1;
131-
if (eIndex > 0) {
132-
String extension = repoContent.getName().substring(eIndex);
133-
134-
// If this is a cwl file which needs to be parsed
135-
if (extension.equals(CWL_EXTENSION)) {
136-
if (repoContent.getSize() <= singleFileSizeLimit) {
137-
// Get the content of this file from Github
138-
GithubDetails githubFile = new GithubDetails(githubInfo.getOwner(),
139-
githubInfo.getRepoName(), githubInfo.getBranch(), repoContent.getPath());
140-
String fileContent = githubService.downloadFile(githubFile, commitSha);
141-
142-
// Parse yaml to JsonNode
143-
Yaml reader = new Yaml();
144-
ObjectMapper mapper = new ObjectMapper();
145-
JsonNode cwlFile = mapper.valueToTree(reader.load(fileContent));
146-
147-
// Add document to those being considered
148-
addDoc(cwlFile, repoContent.getName());
149-
} else {
150-
throw new IOException("File '" + repoContent.getName() + "' is over singleFileSizeLimit - " +
151-
FileUtils.byteCountToDisplaySize(repoContent.getSize()) + "/" +
152-
FileUtils.byteCountToDisplaySize(singleFileSizeLimit));
131+
// Keep track of total file size for limit
132+
totalFileSize += repoContent.getSize();
133+
if (totalFileSize <= totalFileSizeLimit) {
134+
// Get the file extension
135+
int eIndex = repoContent.getName().lastIndexOf('.') + 1;
136+
if (eIndex > 0) {
137+
String extension = repoContent.getName().substring(eIndex);
138+
139+
// If this is a cwl file which needs to be parsed
140+
if (extension.equals(CWL_EXTENSION)) {
141+
if (repoContent.getSize() <= singleFileSizeLimit) {
142+
// Get the content of this file from Github
143+
GithubDetails githubFile = new GithubDetails(githubInfo.getOwner(),
144+
githubInfo.getRepoName(), githubInfo.getBranch(), repoContent.getPath());
145+
String fileContent = githubService.downloadFile(githubFile, commitSha);
146+
147+
// Parse yaml to JsonNode
148+
Yaml reader = new Yaml();
149+
ObjectMapper mapper = new ObjectMapper();
150+
JsonNode cwlFile = mapper.valueToTree(reader.load(fileContent));
151+
152+
// Add document to those being considered
153+
addDoc(cwlFile, repoContent.getName());
154+
} else {
155+
throw new IOException("File '" + repoContent.getName() + "' is over singleFileSizeLimit - " +
156+
FileUtils.byteCountToDisplaySize(repoContent.getSize()) + "/" +
157+
FileUtils.byteCountToDisplaySize(singleFileSizeLimit));
158+
}
153159
}
154160
}
161+
} else {
162+
throw new IOException("Contents of the repository are over totalFileSizeLimit of " +
163+
FileUtils.byteCountToDisplaySize(totalFileSizeLimit));
155164
}
156-
157165
}
158166
}
159167
}

src/main/java/org/commonwl/viewer/services/WorkflowService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,23 @@ public class WorkflowService {
4343
private final int cacheDays;
4444
private final String graphvizStorage;
4545
private final int singleFileSizeLimit;
46+
private final int totalFileSizeLimit;
4647

4748
@Autowired
4849
public WorkflowService(GitHubService githubService,
4950
WorkflowRepository workflowRepository,
5051
ROBundleFactory ROBundleFactory,
5152
@Value("${cacheDays}") int cacheDays,
5253
@Value("${graphvizStorage}") String graphvizStorage,
53-
@Value("${singleFileSizeLimit}") int singleFileSizeLimit) {
54+
@Value("${singleFileSizeLimit}") int singleFileSizeLimit,
55+
@Value("${totalFileSizeLimit}") int totalFileSizeLimit) {
5456
this.githubService = githubService;
5557
this.workflowRepository = workflowRepository;
5658
this.ROBundleFactory = ROBundleFactory;
5759
this.cacheDays = cacheDays;
5860
this.graphvizStorage = graphvizStorage;
5961
this.singleFileSizeLimit = singleFileSizeLimit;
62+
this.totalFileSizeLimit = totalFileSizeLimit;
6063
}
6164

6265
/**
@@ -71,7 +74,8 @@ public Workflow newWorkflowFromGithub(GithubDetails githubInfo) {
7174
String latestCommit = githubService.getCommitSha(githubInfo);
7275

7376
// Set up CWL utility to collect the documents
74-
CWLCollection cwlFiles = new CWLCollection(githubService, githubInfo, latestCommit, singleFileSizeLimit);
77+
CWLCollection cwlFiles = new CWLCollection(githubService, githubInfo, latestCommit,
78+
singleFileSizeLimit, totalFileSizeLimit);
7579

7680
// Get the workflow model
7781
Workflow workflowModel = cwlFiles.getWorkflow();

src/main/resources/application.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ graphvizStorage = /tmp
1616
cacheDays = 1
1717

1818
# File size limit for individual files in bytes
19+
# CWL files must be lower than this, but other files in the repo may be lower and in this case will
20+
# be externally linked in the Research Object Bundle
1921
singleFileSizeLimit = 5242880
2022

21-
# File size limit for the contents of the entire Research Object Bundle in bytes
23+
# File size limit for the contents of the entire repository path in bytes
2224
totalFileSizeLimit = 1073741824
2325

2426
#=======================

0 commit comments

Comments
 (0)