|
42 | 42 | import java.io.File; |
43 | 43 | import java.io.IOException; |
44 | 44 | import java.nio.file.Path; |
| 45 | +import java.util.ArrayList; |
45 | 46 | import java.util.Calendar; |
46 | 47 | import java.util.Date; |
| 48 | +import java.util.List; |
47 | 49 |
|
48 | 50 | @Service |
49 | 51 | public class WorkflowService { |
@@ -183,6 +185,61 @@ public Workflow getWorkflow(GitDetails gitInfo) { |
183 | 185 | return workflow; |
184 | 186 | } |
185 | 187 |
|
| 188 | + /** |
| 189 | + * Get a list of workflows from a directory |
| 190 | + * @param gitInfo The Git directory information |
| 191 | + * @return The list of workflow overviews |
| 192 | + */ |
| 193 | + public List<WorkflowOverview> getWorkflowsFromDirectory(GitDetails gitInfo) throws IOException, GitAPIException { |
| 194 | + List<WorkflowOverview> workflowsInDir = new ArrayList<>(); |
| 195 | + try { |
| 196 | + Git repo = null; |
| 197 | + while (repo == null) { |
| 198 | + try { |
| 199 | + boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl()); |
| 200 | + repo = gitService.getRepository(gitInfo, safeToAccess); |
| 201 | + } catch (RefNotFoundException ex) { |
| 202 | + // Attempt slashes in branch fix |
| 203 | + GitDetails correctedForSlash = transferPathToBranch(gitInfo); |
| 204 | + if (correctedForSlash != null) { |
| 205 | + gitSemaphore.release(gitInfo.getRepoUrl()); |
| 206 | + gitInfo = correctedForSlash; |
| 207 | + } else { |
| 208 | + throw ex; |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + Path localPath = repo.getRepository().getWorkTree().toPath(); |
| 214 | + Path pathToDirectory = localPath.resolve(gitInfo.getPath()).normalize().toAbsolutePath(); |
| 215 | + if (pathToDirectory.toString().equals("/")) { |
| 216 | + pathToDirectory = localPath; |
| 217 | + } else if (!pathToDirectory.startsWith(localPath.normalize().toAbsolutePath())) { |
| 218 | + // Prevent path traversal attacks |
| 219 | + throw new WorkflowNotFoundException(); |
| 220 | + } |
| 221 | + |
| 222 | + File directory = new File(pathToDirectory.toString()); |
| 223 | + if (directory.exists() && directory.isDirectory()) { |
| 224 | + for (final File file : directory.listFiles()) { |
| 225 | + int eIndex = file.getName().lastIndexOf('.') + 1; |
| 226 | + if (eIndex > 0) { |
| 227 | + String extension = file.getName().substring(eIndex); |
| 228 | + if (extension.equals("cwl")) { |
| 229 | + WorkflowOverview overview = cwlService.getWorkflowOverview(file); |
| 230 | + if (overview != null) { |
| 231 | + workflowsInDir.add(overview); |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + } finally { |
| 238 | + gitSemaphore.release(gitInfo.getRepoUrl()); |
| 239 | + } |
| 240 | + return workflowsInDir; |
| 241 | + } |
| 242 | + |
186 | 243 | /** |
187 | 244 | * Get the RO bundle for a Workflow, triggering re-download if it does not exist |
188 | 245 | * @param gitDetails The origin details of the workflow |
@@ -224,7 +281,6 @@ public QueuedWorkflow createQueuedWorkflow(GitDetails gitInfo) |
224 | 281 | QueuedWorkflow queuedWorkflow; |
225 | 282 |
|
226 | 283 | try { |
227 | | - // Clone repository to temporary folder |
228 | 284 | Git repo = null; |
229 | 285 | while (repo == null) { |
230 | 286 | try { |
|
0 commit comments