1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 CollectionTree <- R6::R6Class(
11 initialize = function(fileContent, collection)
13 self$pathsList <- fileContent
14 treeBranches <- sapply(fileContent, function(filePath) self$createBranch(filePath))
15 root <- Subcollection$new("")
16 sapply(treeBranches, function(branch) self$addBranch(root, branch))
17 root$setCollection(collection)
21 createBranch = function(filePath)
23 splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
25 lastElementIndex <- length(splitPath)
27 for(elementIndex in lastElementIndex:1)
29 if(elementIndex == lastElementIndex)
31 branch <- ArvadosFile$new(splitPath[[elementIndex]])
35 newFolder <- Subcollection$new(splitPath[[elementIndex]])
44 addBranch = function(container, node)
46 child <- container$get(node$getName())
50 # Make sure we are don't make any REST call while adding child
51 collection <- container$getCollection()
52 container$setCollection(NULL, setRecursively = FALSE)
54 container$setCollection(collection, setRecursively = FALSE)
58 # Note: REST always returns folder name alone before other folder
59 # content, so in first iteration we don't know if it's a file
60 # or folder since its just a name, so we assume it's a file.
61 # If we encounter that same name again we know
62 # it's a folder so we need to replace ArvadosFile with Subcollection.
63 if("ArvadosFile" %in% class(child))
64 child = private$replaceFileWithSubcollection(child)
66 self$addBranch(child, node$getFirst())
70 getElement = function(relativePath)
72 relativePath <- trimFromStart(relativePath, "./")
73 relativePath <- trimFromEnd(relativePath, "/")
75 if(endsWith(relativePath, "/"))
76 relativePath <- substr(relativePath, 0, nchar(relativePath) - 1)
78 splitPath <- unlist(strsplit(relativePath, "/", fixed = TRUE))
79 returnElement <- private$tree
81 for(pathFragment in splitPath)
83 returnElement <- returnElement$get(pathFragment)
85 if(is.null(returnElement))
92 getTree = function() private$tree
99 replaceFileWithSubcollection = function(arvadosFile)
101 subcollection <- Subcollection$new(arvadosFile$getName())
102 fileParent <- arvadosFile$getParent()
103 fileParent$remove(arvadosFile$getName())
104 fileParent$add(subcollection)
106 arvadosFile$setParent(NULL)