24
24
import com .fasterxml .jackson .databind .node .ArrayNode ;
25
25
import com .fasterxml .jackson .databind .node .ObjectNode ;
26
26
import com .fasterxml .jackson .databind .node .TextNode ;
27
+ import org .apache .commons .io .FileUtils ;
27
28
import org .apache .commons .io .FilenameUtils ;
28
29
import org .apache .jena .ontology .OntModelSpec ;
29
30
import org .apache .jena .query .QuerySolution ;
35
36
import org .apache .jena .riot .RiotException ;
36
37
import org .commonwl .view .docker .DockerService ;
37
38
import org .commonwl .view .git .GitDetails ;
38
- import org .commonwl .view .git .GitService ;
39
39
import org .commonwl .view .graphviz .ModelDotWriter ;
40
40
import org .commonwl .view .graphviz .RDFDotWriter ;
41
41
import org .commonwl .view .workflow .Workflow ;
42
- import org .eclipse .jgit .errors .LargeObjectException ;
43
42
import org .slf4j .Logger ;
44
43
import org .slf4j .LoggerFactory ;
45
44
import org .springframework .beans .factory .annotation .Autowired ;
45
+ import org .springframework .beans .factory .annotation .Value ;
46
46
import org .springframework .stereotype .Service ;
47
47
import org .yaml .snakeyaml .Yaml ;
48
48
49
49
import java .io .ByteArrayInputStream ;
50
+ import java .io .File ;
50
51
import java .io .IOException ;
51
52
import java .io .StringWriter ;
52
53
import java .nio .file .Path ;
53
54
import java .nio .file .Paths ;
54
55
import java .util .*;
55
56
57
+ import static org .apache .commons .io .FileUtils .readFileToString ;
58
+
56
59
/**
57
60
* Provides CWL parsing for workflows to gather an overview
58
61
* for display and visualisation
@@ -65,7 +68,7 @@ public class CWLService {
65
68
// Autowired properties/services
66
69
private final RDFService rdfService ;
67
70
private final CWLTool cwlTool ;
68
- private final GitService gitService ;
71
+ private final int singleFileSizeLimit ;
69
72
70
73
// CWL specific strings
71
74
private final String DOC_GRAPH = "$graph" ;
@@ -95,27 +98,30 @@ public class CWLService {
95
98
* Constructor for the Common Workflow Language service
96
99
* @param rdfService A service for handling RDF queries
97
100
* @param cwlTool Handles cwltool integration
98
- * @param gitService Handles Git repository functionality
101
+ * @param singleFileSizeLimit The file size limit for single files
99
102
*/
100
103
@ Autowired
101
104
public CWLService (RDFService rdfService ,
102
105
CWLTool cwlTool ,
103
- GitService gitService ) {
106
+ @ Value ( "${singleFileSizeLimit}" ) int singleFileSizeLimit ) {
104
107
this .rdfService = rdfService ;
105
108
this .cwlTool = cwlTool ;
106
- this .gitService = gitService ;
109
+ this .singleFileSizeLimit = singleFileSizeLimit ;
107
110
}
108
111
109
112
/**
110
113
* Gets the Workflow object from internal parsing
111
- * @param gitDetails The details for the workflow in the git repository
114
+ * @param workflowFile The workflow file to be parsed
112
115
* @return The constructed workflow object
113
116
*/
114
- public Workflow parseWorkflowNative (GitDetails gitDetails ) throws IOException {
115
- try {
117
+ public Workflow parseWorkflowNative (File workflowFile ) throws IOException {
118
+
119
+ // Check file size limit before parsing
120
+ long fileSizeBytes = workflowFile .length ();
121
+ if (fileSizeBytes <= singleFileSizeLimit ) {
122
+
116
123
// Parse file as yaml
117
- JsonNode cwlFile = yamlStringToJson (gitService .getFile (gitService
118
- .getRepository (gitDetails ).getRepository (), gitDetails .getPath ()));
124
+ JsonNode cwlFile = yamlStringToJson (readFileToString (workflowFile ));
119
125
120
126
// If the CWL file is packed there can be multiple workflows in a file
121
127
Map <String , JsonNode > packedFiles = new HashMap <>();
@@ -132,7 +138,7 @@ public Workflow parseWorkflowNative(GitDetails gitDetails) throws IOException {
132
138
// Use filename for label if there is no defined one
133
139
String label = extractLabel (cwlFile );
134
140
if (label == null ) {
135
- label = FilenameUtils .getName (gitDetails .getPath ());
141
+ label = FilenameUtils .getName (workflowFile .getPath ());
136
142
}
137
143
138
144
// Construct the rest of the workflow model
@@ -157,19 +163,22 @@ public Workflow parseWorkflowNative(GitDetails gitDetails) throws IOException {
157
163
158
164
return workflowModel ;
159
165
160
- } catch (LargeObjectException ex ) {
161
- throw new IOException ("File '" + FilenameUtils .getName (gitDetails .getPath ()) +
162
- "' is over singleFileSizeLimit" ;
166
+ } else {
167
+ throw new IOException ("File '" + workflowFile .getName () + "' is over singleFileSizeLimit - " +
168
+ FileUtils .byteCountToDisplaySize (fileSizeBytes ) + "/" +
169
+ FileUtils .byteCountToDisplaySize (singleFileSizeLimit ));
163
170
}
164
171
165
172
}
166
173
167
174
/**
168
175
* Create a workflow model using cwltool rdf output
169
176
* @param basicModel The basic workflow object created thus far
177
+ * @param workflowFile The workflow file to run cwltool on
170
178
* @return The constructed workflow object
171
179
*/
172
- public Workflow parseWorkflowWithCwltool (Workflow basicModel ) throws CWLValidationException {
180
+ public Workflow parseWorkflowWithCwltool (Workflow basicModel ,
181
+ File workflowFile ) throws CWLValidationException {
173
182
GitDetails gitDetails = basicModel .getRetrievedFrom ();
174
183
String latestCommit = basicModel .getLastCommit ();
175
184
String packedWorkflowID = basicModel .getPackedWorkflowID ();
0 commit comments