19
19
20
20
package org .commonwl .viewer .domain ;
21
21
22
+ import org .apache .commons .io .FileUtils ;
22
23
import org .apache .commons .io .FilenameUtils ;
23
24
import org .apache .taverna .robundle .Bundle ;
24
25
import org .apache .taverna .robundle .Bundles ;
30
31
import org .slf4j .Logger ;
31
32
import org .slf4j .LoggerFactory ;
32
33
33
- import java .io .File ;
34
34
import java .io .IOException ;
35
35
import java .net .URI ;
36
36
import java .net .URISyntaxException ;
@@ -56,6 +56,7 @@ public class ROBundle {
56
56
private GithubDetails githubInfo ;
57
57
private String commitSha ;
58
58
private Agent thisApp ;
59
+ private int singleFileSizeLimit ;
59
60
private Set <HashableAgent > authors = new HashSet <HashableAgent >();
60
61
61
62
// Pattern for extracting version from a cwl file
@@ -68,8 +69,9 @@ public class ROBundle {
68
69
* @throws IOException Any API errors which may have occurred
69
70
*/
70
71
public ROBundle (GitHubService githubService , GithubDetails githubInfo , String commitSha ,
71
- String appName , String appURL ) throws IOException {
72
- // TODO: Add back file size checking on individual files as well as whole bundle
72
+ String appName , String appURL , int singleFileSizeLimit ) throws IOException {
73
+ // File size limits
74
+ this .singleFileSizeLimit = singleFileSizeLimit ;
73
75
74
76
// Create a new RO bundle
75
77
this .bundle = Bundles .createBundle ();
@@ -137,29 +139,48 @@ private void addFiles(List<RepositoryContents> repoContents, Path path) throws I
137
139
// Otherwise this is a file so add to the bundle
138
140
} else if (repoContent .getType ().equals ("file" )) {
139
141
140
- // Get the content of this file from Github
141
- GithubDetails githubFile = new GithubDetails (githubInfo .getOwner (),
142
- githubInfo .getRepoName (), githubInfo .getBranch (), repoContent .getPath ());
143
- String fileContent = githubService .downloadFile (githubFile , commitSha );
144
-
145
- // Save file to research object bundle
146
- Path newFilePort = path .resolve (repoContent .getName ());
147
- Bundles .setStringValue (newFilePort , fileContent );
142
+ try {
143
+ // Where to store the new file in bundle
144
+ Path bundleFilePath = path .resolve (repoContent .getName ());
145
+
146
+ // Raw URI of the bundle
147
+ GithubDetails githubFile = new GithubDetails (githubInfo .getOwner (),
148
+ githubInfo .getRepoName (), githubInfo .getBranch (), repoContent .getPath ());
149
+ URI rawURI = new URI ("https://raw.githubusercontent.com/" + githubFile .getOwner () + "/" +
150
+ githubFile .getRepoName () + "/" + commitSha + "/" + githubFile .getPath ());
151
+
152
+ // Variable to store file contents
153
+ String fileContent = null ;
154
+
155
+ // Download or externally link if oversized
156
+ if (repoContent .getSize () <= singleFileSizeLimit ) {
157
+ // Get the content of this file from Github
158
+ fileContent = githubService .downloadFile (githubFile , commitSha );
159
+
160
+ // Save file to research object bundle
161
+ Bundles .setStringValue (bundleFilePath , fileContent );
162
+ } else {
163
+ logger .info ("File " + repoContent .getName () + " is too large to download -" +
164
+ FileUtils .byteCountToDisplaySize (repoContent .getSize ()) + "/" +
165
+ FileUtils .byteCountToDisplaySize (singleFileSizeLimit ) +
166
+ " + linking externally to RO bundle" );
167
+ bundleFilePath = Bundles .setReference (bundleFilePath , rawURI );
168
+ }
148
169
149
- // Manifest aggregation
150
- PathMetadata aggregation = bundle .getManifest ().getAggregation (newFilePort );
170
+ // Manifest aggregation
171
+ PathMetadata aggregation = bundle .getManifest ().getAggregation (bundleFilePath );
151
172
152
- try {
153
173
// Special handling for cwl files
154
174
if (FilenameUtils .getExtension (repoContent .getName ()).equals ("cwl" )) {
155
175
// Correct mime type (no official standard for yaml)
156
176
aggregation .setMediatype ("text/x-yaml" );
157
177
158
178
// Add conformsTo for version extracted from regex
159
- // Lower overhead vs parsing entire file
160
- Matcher m = cwlVersionPattern .matcher (fileContent );
161
- if (m .find ()) {
162
- aggregation .setConformsTo (new URI ("https://w3id.org/cwl/" + m .group (1 )));
179
+ if (fileContent != null ) {
180
+ Matcher m = cwlVersionPattern .matcher (fileContent );
181
+ if (m .find ()) {
182
+ aggregation .setConformsTo (new URI ("https://w3id.org/cwl/" + m .group (1 )));
183
+ }
163
184
}
164
185
}
165
186
@@ -169,15 +190,13 @@ private void addFiles(List<RepositoryContents> repoContents, Path path) throws I
169
190
aggregation .setAuthoredBy (new ArrayList <Agent >(fileAuthors ));
170
191
171
192
// Set retrieved information for this file in the manifest
172
- aggregation .setRetrievedFrom (new URI ("https://raw.githubusercontent.com/" + githubFile .getOwner () + "/" +
173
- githubFile .getRepoName () + "/" + commitSha + "/" + githubFile .getPath ()));
193
+ aggregation .setRetrievedFrom (rawURI );
174
194
aggregation .setRetrievedBy (thisApp );
175
195
aggregation .setRetrievedOn (aggregation .getCreatedOn ());
176
196
177
197
} catch (URISyntaxException ex ) {
178
198
logger .error ("Error creating URI for RO Bundle" , ex );
179
199
}
180
-
181
200
}
182
201
}
183
202
}
0 commit comments