1 source("./R/Subcollection.R")
2 source("./R/ArvadosFile.R")
4 #' Arvados Collection Object
8 #' @examples arv = Collection$new(api, uuid)
10 Collection <- R6::R6Class(
16 #Todo(Fudo): Encapsulate this?
21 modified_by_client_uuid = NULL,
22 modified_by_user_uuid = NULL,
24 portable_data_hash = NULL,
25 replication_desired = NULL,
26 replication_confirmed_at = NULL,
27 replication_confirmed = NULL,
38 initialize = function(api, uuid)
41 result <- private$api$getCollection(uuid)
43 self$uuid <- result$uuid
44 self$etag <- result$etag
45 self$owner_uuid <- result$owner_uuid
46 self$created_at <- result$created_at
47 self$modified_by_client_uuid <- result$modified_by_client_uuid
48 self$modified_by_user_uuid <- result$modified_by_user_uuid
49 self$modified_at <- result$modified_at
50 self$portable_data_hash <- result$portable_data_hash
51 self$replication_desired <- result$replication_desired
52 self$replication_confirmed_at <- result$replication_confirmed_at
53 self$replication_confirmed <- result$replication_confirmed
54 self$updated_at <- result$updated_at
55 self$manifest_text <- result$manifest_text
56 self$name <- result$name
57 self$description <- result$description
58 self$properties <- result$properties
59 self$delete_at <- result$delete_at
60 self$file_names <- result$file_names
61 self$trash_at <- result$trash_at
62 self$is_trashed <- result$is_trashed
64 #Todo(Fudo): Replace this when you get access to webDAV server.
65 private$fileItems <- private$getCollectionContent()
67 private$fileTree <- private$generateTree(private$fileItems)
70 printFileContent = function(pretty = TRUE)
73 private$fileTree$printContent(0)
75 print(private$fileItems)
79 get = function(relativePath)
81 treeNode <- private$traverseInOrder(private$fileTree, function(node)
83 if(node$relativePath == relativePath)
89 if(!is.null(treeNode))
91 return(private$createSubcollectionTree(treeNode))
101 items = function(value)
104 return(private$fileItems)
106 print("Value is read-only.")
118 createSubcollectionTree = function(treeNode)
120 if(treeNode$hasChildren())
124 for(child in treeNode$children)
126 child <- private$createSubcollectionTree(child)
127 children <- c(children, child)
130 return(Subcollection$new(treeNode$name, treeNode$relativePath, children))
134 if(treeNode$type == "file")
135 return(ArvadosFile$new(treeNode$name, treeNode$relativePath, private$api, self))
136 else if(treeNode$type == "folder" || treeNode$type == "root")
137 return(Subcollection$new(treeNode$name, treeNode$relativePath, NULL))
141 createSubcollectionFromNode = function(treeNode, children)
144 if(treeNode$type == "file")
145 subcollection = ArvadosFile$new(treeNode$name, treeNode$relativePath)
146 else if(treeNode$type == "folder" || treeNode$type == "root")
147 subcollection = Subcollection$new(treeNode$name, treeNode$relativePath, children)
152 getCollectionContent = function()
154 #TODO(Fudo): Use proper URL here.
155 uri <- URLencode(paste0(private$api$getWebDavHostName(), "c=", self$uuid))
157 # fetch directory listing via curl and parse XML response
158 h <- curl::new_handle()
159 curl::handle_setopt(h, customrequest = "PROPFIND")
161 #TODO(Fudo): Use proper token here.
162 curl::handle_setheaders(h, "Authorization" = paste("OAuth2", private$api$getToken()))
163 response <- curl::curl_fetch_memory(uri, h)
166 HttpParser$new()$parseWebDAVResponse(response, uri)
169 #Todo(Fudo): Move tree creation to another file.
170 generateTree = function(collectionContent)
172 treeBranches <- sapply(collectionContent, function(filePath)
174 splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
176 pathEndsWithSlash <- substr(filePath, nchar(filePath), nchar(filePath)) == "/"
178 branch = private$createBranch(splitPath, pathEndsWithSlash)
181 root <- TreeNode$new("./", "root")
182 root$relativePath = ""
184 sapply(treeBranches, function(branch)
186 private$addNode(root, branch)
192 createBranch = function(splitPath, pathEndsWithSlash)
195 lastElementIndex <- length(splitPath)
197 lastElementInPathType = "file"
198 if(pathEndsWithSlash)
199 lastElementInPathType = "folder"
201 for(elementIndex in lastElementIndex:1)
203 if(elementIndex == lastElementIndex)
205 branch = TreeNode$new(splitPath[[elementIndex]], lastElementInPathType)
209 newFolder = TreeNode$new(splitPath[[elementIndex]], "folder")
210 newFolder$addChild(branch)
214 branch$relativePath <- paste(unlist(splitPath[1:elementIndex]), collapse = "/")
220 addNode = function(container, node)
222 child = container$getChild(node$name)
226 container$addChild(node)
230 private$addNode(child, node$getFirstChild())
234 traverseInOrder = function(node, predicate)
236 if(node$hasChildren())
238 result <- predicate(node)
243 for(child in node$children)
245 result <- private$traverseInOrder(child, predicate)
255 return(predicate(node))
264 TreeNode <- R6::R6Class(
276 initialize = function(name, type)
279 name <- paste0(name, "/")
283 self$children <- list()
286 addChild = function(node)
288 self$children <- c(self$children, node)
293 setParent = function(parent)
298 getChild = function(childName)
300 for(child in self$children)
302 if(childName == child$name)
309 hasChildren = function()
311 if(length(self$children) != 0)
317 getFirstChild = function()
319 if(!self$hasChildren())
322 return(self$children[[1]])
325 printContent = function(depth)
327 indentation <- paste(rep("....", depth), collapse = "")
328 print(paste0(indentation, self$name))
330 for(child in self$children)
331 child$printContent(depth + 1)