1 source("./R/Subcollection.R")
2 source("./R/ArvadosFile.R")
3 source("./R/RESTService.R")
6 #' Arvados Collection Object
10 #' @examples arv = Collection$new(api, uuid)
12 Collection <- R6::R6Class(
21 initialize = function(api, uuid)
24 private$REST <- api$getRESTService()
28 private$fileContent <- private$REST$getCollectionContent(uuid)
29 private$tree <- CollectionTree$new(private$fileContent, self)
32 add = function(content, relativePath = "")
34 if(relativePath == "" ||
35 relativePath == "." ||
38 subcollection <- private$tree$getTree()
42 relativePath <- trimFromEnd(relativePath, "/")
43 subcollection <- self$get(relativePath)
46 if(is.null(subcollection))
47 stop(paste("Subcollection", relativePath, "doesn't exist."))
49 if("ArvadosFile" %in% class(content) ||
50 "Subcollection" %in% class(content))
52 subcollection$add(content)
58 stop(paste0("Expected AravodsFile or Subcollection object, got ",
59 paste0("(", paste0(class(content), collapse = ", "), ")"),
64 create = function(fileNames, relativePath = "")
66 if(relativePath == "" ||
67 relativePath == "." ||
70 subcollection <- private$tree$getTree()
74 relativePath <- trimFromEnd(relativePath, "/")
75 subcollection <- self$get(relativePath)
78 if(is.null(subcollection))
79 stop(paste("Subcollection", relativePath, "doesn't exist."))
81 if(is.character(fileNames))
84 sapply(fileNames, function(fileName)
86 childWithSameName <- subcollection$get(fileName)
87 if(!is.null(childWithSameName))
88 stop("Destination already contains file with same name.")
90 newFile <- ArvadosFile$new(fileName)
91 subcollection$add(newFile)
93 arvadosFiles <<- c(arvadosFiles, newFile)
96 if(length(arvadosFiles) == 1)
97 return(arvadosFiles[[1]])
103 stop(paste0("Expected character vector, got ",
104 paste0("(", paste0(class(fileNames), collapse = ", "), ")"),
109 remove = function(paths)
111 if(is.character(paths))
113 sapply(paths, function(filePath)
115 filePath <- trimFromEnd(filePath, "/")
116 file <- self$get(filePath)
119 stop(paste("File", filePath, "doesn't exist."))
121 parent <- file$getParent()
122 parent$remove(file$getName())
127 stop(paste0("Expected character vector, got ",
128 paste0("(", paste0(class(paths), collapse = ", "), ")"),
133 move = function(content, newLocation)
135 content <- trimFromEnd(content, "/")
137 elementToMove <- self$get(content)
139 if(is.null(elementToMove))
140 stop("Element you want to move doesn't exist in the collection.")
142 elementToMove$move(newLocation)
145 getFileListing = function()
147 content <- private$REST$getCollectionContent(self$uuid)
148 content[order(tolower(content))]
151 get = function(relativePath)
153 private$tree$getElement(relativePath)
156 getRESTService = function() private$REST,
157 setRESTService = function(newRESTService) private$REST <- newRESTService
166 generateTree = function(content)
168 treeBranches <- sapply(collectionContent, function(filePath)
170 splitPath <- unlist(strsplit(filePath$name, "/", fixed = TRUE))
172 branch = private$createBranch(splitPath, filePath$fileSize)