1 #' Arvados SubCollection Object
5 #' @export Subcollection
6 Subcollection <- R6::R6Class(
12 initialize = function(name)
15 private$http <- HttpRequest$new()
16 private$httpParser <- HttpParser$new()
19 add = function(content)
21 if("ArvadosFile" %in% class(content) ||
22 "Subcollection" %in% class(content))
24 if(!is.null(content$.__enclos_env__$private$collection))
25 stop("ArvadosFile/Subcollection already belongs to a collection.")
27 childWithSameName <- private$getChild(content$getName())
28 if(!is.null(childWithSameName))
29 stop("Subcollection already contains ArvadosFile
30 or Subcollection with same name.")
32 if(!is.null(private$collection))
34 contentPath <- paste0(self$getRelativePath(),
35 "/", content$getFileListing())
37 private$collection$.__enclos_env__$private$createFilesOnREST(contentPath)
38 content$.__enclos_env__$private$addToCollection(private$collection)
41 private$children <- c(private$children, content)
42 content$.__enclos_env__$private$parent = self
44 "Content added successfully."
48 stop(paste("Expected AravodsFile or Subcollection object, got",
53 removeFromCollection = function()
55 if(is.null(private$collection))
56 stop("Subcollection doesn't belong to any collection.")
58 if(private$name == "")
59 stop("Unable to delete root folder.")
61 collectionList <- paste0(self$getRelativePath(),
62 "/", self$getFileListing(fullpath = FALSE))
63 sapply(collectionList, function(file)
65 private$collection$.__enclos_env__$private$deleteFromREST(file)
68 private$addToCollection(NULL)
69 private$dettachFromParent()
71 "Content removed successfully."
74 getFileListing = function(fullpath = TRUE)
80 for(child in private$children)
81 content <- c(content, child$getFileListing())
83 if(private$name != "")
84 content <- unlist(paste0(private$name, "/", content))
88 for(child in private$children)
89 content <- c(content, child$getName())
95 getSizeInBytes = function()
97 collectionURL <- URLencode(paste0(private$collection$api$getWebDavHostName(),
98 "c=", private$collection$uuid))
99 subcollectionURL <- paste0(collectionURL, "/", self$getRelativePath(), "/");
101 headers = list("Authorization" = paste("OAuth2", private$collection$api$getToken()))
103 propfindResponse <- private$http$PROPFIND(subcollectionURL, headers)
105 sizes <- private$httpParser$extractFileSizeFromWebDAVResponse(propfindResponse, collectionURL)
106 sizes <- as.numeric(sizes[-1])
111 getName = function() private$name,
113 getRelativePath = function()
115 relativePath <- c(private$name)
116 parent <- private$parent
118 while(!is.null(parent))
120 relativePath <- c(parent$getName(), relativePath)
121 parent <- parent$getParent()
124 relativePath <- relativePath[relativePath != ""]
125 paste0(relativePath, collapse = "/")
128 move = function(newLocation)
130 if(is.null(private$collection))
131 stop("Subcollection doesn't belong to any collection.")
133 if(endsWith(newLocation, paste0(private$name, "/")))
135 newLocation <- substr(newLocation, 0,
136 nchar(newLocation) - nchar(paste0(private$name, "/")))
138 else if(endsWith(newLocation, private$name))
140 newLocation <- substr(newLocation, 0,
141 nchar(newLocation) - nchar(private$name))
145 stop("Destination path is not valid.")
148 newParent <- private$collection$get(newLocation)
150 if(is.null(newParent))
152 stop("Unable to get destination subcollection.")
155 status <- private$collection$.__enclos_env__$private$moveOnREST(self$getRelativePath(),
156 paste0(newParent$getRelativePath(), "/", self$getName()))
158 private$attachToParent(newParent)
160 "Content moved successfully."
163 getParent = function() private$parent
175 getChild = function(name)
177 for(child in private$children)
179 if(child$getName() == name)
186 getFirstChild = function()
188 if(length(private$children) == 0)
191 private$children[[1]]
194 removeChild = function(name)
196 numberOfChildren = length(private$children)
197 if(numberOfChildren > 0)
199 for(childIndex in 1:numberOfChildren)
201 if(private$children[[childIndex]]$getName() == name)
203 private$children = private$children[-childIndex]
210 addToCollection = function(collection)
212 for(child in private$children)
213 child$.__enclos_env__$private$addToCollection(collection)
215 private$collection = collection
218 dettachFromParent = function()
220 if(!is.null(private$parent))
222 private$parent$.__enclos_env__$private$removeChild(private$name)
223 private$parent <- NULL
226 stop("Parent doesn't exists.")
229 attachToParent = function(parent)
231 if(private$name != "")
233 parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
234 private$parent <- parent