1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 source("./R/Subcollection.R")
6 source("./R/ArvadosFile.R")
9 CollectionTree <- R6::R6Class(
15 initialize = function(fileContent, collection)
17 self$pathsList <- fileContent
19 treeBranches <- sapply(fileContent, function(filePath)
21 splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
22 branch <- private$createBranch(splitPath)
25 root <- Subcollection$new("")
27 sapply(treeBranches, function(branch)
29 private$addBranch(root, branch)
32 root$setCollection(collection)
36 getElement = function(relativePath)
38 relativePath <- trimFromStart(relativePath, "./")
39 relativePath <- trimFromEnd(relativePath, "/")
41 if(endsWith(relativePath, "/"))
42 relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
44 splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
45 returnElement <- private$tree
47 for(pathFragment in splitPath)
49 returnElement <- returnElement$get(pathFragment)
51 if(is.null(returnElement))
58 getTree = function() private$tree
65 createBranch = function(splitPath)
68 lastElementIndex <- length(splitPath)
70 for(elementIndex in lastElementIndex:1)
72 if(elementIndex == lastElementIndex)
74 branch <- ArvadosFile$new(splitPath[[elementIndex]])
78 newFolder <- Subcollection$new(splitPath[[elementIndex]])
87 addBranch = function(container, node)
89 child <- container$get(node$getName())
97 # Note: REST always returns folder name alone before other folder
98 # content, so in first iteration we don't know if it's a file
99 # or folder since its just a name, so we assume it's a file.
100 # If we encounter that same name again we know
101 # it's a folder so we need to replace ArvadosFile with Subcollection.
102 if("ArvadosFile" %in% class(child))
104 child = private$replaceFileWithSubcollection(child)
107 private$addBranch(child, node$getFirst())
111 replaceFileWithSubcollection = function(arvadosFile)
113 subcollection <- Subcollection$new(arvadosFile$getName())
114 fileParent <- arvadosFile$getParent()
115 fileParent$remove(arvadosFile$getName())
116 fileParent$add(subcollection)
118 arvadosFile$setParent(NULL)