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")
7 source("./R/RESTService.R")
12 #' Collection class provides interface for working with Arvados collections.
15 #' \preformatted{collection = Collection$new(arv, uuid)}
17 #' @section Arguments:
19 #' \item{arv}{Arvados object.}
20 #' \item{uuid}{UUID of a collection.}
25 #' \item{add(content)}{Adds ArvadosFile or Subcollection specified by content to the collection.}
26 #' \item{create(fileNames, relativePath = "")}{Creates one or more ArvadosFiles and adds them to the collection at specified path.}
27 #' \item{remove(fileNames)}{Remove one or more files from the collection.}
28 #' \item{move(content, newLocation)}{Moves ArvadosFile or Subcollection to another location in the collection.}
29 #' \item{getFileListing()}{Returns collections file content as character vector.}
30 #' \item{get(relativePath)}{If relativePath is valid, returns ArvadosFile or Subcollection specified by relativePath, else returns NULL.}
36 #' arv <- Arvados$new("your Arvados token", "example.arvadosapi.com")
37 #' collection <- Collection$new(arv, "uuid")
39 #' newFile <- ArvadosFile$new("myFile")
40 #' collection$add(newFile, "myFolder")
42 #' createdFiles <- collection$create(c("main.cpp", lib.dll), "cpp/src/")
44 #' collection$remove("location/to/my/file.cpp")
46 #' collection$move("folder/file.cpp", "file.cpp")
48 #' arvadosFile <- collection$get("location/to/my/file.cpp")
49 #' arvadosSubcollection <- collection$get("location/to/my/directory/")
54 Collection <- R6::R6Class(
62 initialize = function(api, uuid)
64 private$REST <- api$getRESTService()
68 add = function(content, relativePath = "")
70 if(is.null(private$tree))
71 private$generateCollectionTreeStructure()
73 if(relativePath == "" ||
74 relativePath == "." ||
77 subcollection <- private$tree$getTree()
81 relativePath <- trimFromEnd(relativePath, "/")
82 subcollection <- self$get(relativePath)
85 if(is.null(subcollection))
86 stop(paste("Subcollection", relativePath, "doesn't exist."))
88 if("ArvadosFile" %in% class(content) ||
89 "Subcollection" %in% class(content))
91 if(content$getName() == "")
92 stop("Content has invalid name.")
94 subcollection$add(content)
99 stop(paste0("Expected AravodsFile or Subcollection object, got ",
100 paste0("(", paste0(class(content), collapse = ", "), ")"),
105 create = function(fileNames, relativePath = "")
107 if(is.null(private$tree))
108 private$generateCollectionTreeStructure()
110 if(relativePath == "" ||
111 relativePath == "." ||
112 relativePath == "./")
114 subcollection <- private$tree$getTree()
118 relativePath <- trimFromEnd(relativePath, "/")
119 subcollection <- self$get(relativePath)
122 if(is.null(subcollection))
123 stop(paste("Subcollection", relativePath, "doesn't exist."))
125 if(is.character(fileNames))
128 sapply(fileNames, function(fileName)
130 childWithSameName <- subcollection$get(fileName)
131 if(!is.null(childWithSameName))
132 stop("Destination already contains file with same name.")
134 newFile <- ArvadosFile$new(fileName)
135 subcollection$add(newFile)
137 arvadosFiles <<- c(arvadosFiles, newFile)
140 if(length(arvadosFiles) == 1)
141 return(arvadosFiles[[1]])
147 stop(paste0("Expected character vector, got ",
148 paste0("(", paste0(class(fileNames), collapse = ", "), ")"),
153 remove = function(paths)
155 if(is.null(private$tree))
156 private$generateCollectionTreeStructure()
158 if(is.character(paths))
160 sapply(paths, function(filePath)
162 filePath <- trimFromEnd(filePath, "/")
163 file <- self$get(filePath)
166 stop(paste("File", filePath, "doesn't exist."))
168 parent <- file$getParent()
171 stop("You can't delete root folder.")
173 parent$remove(file$getName())
180 stop(paste0("Expected character vector, got ",
181 paste0("(", paste0(class(paths), collapse = ", "), ")"),
186 move = function(content, destination)
188 if(is.null(private$tree))
189 private$generateCollectionTreeStructure()
191 content <- trimFromEnd(content, "/")
193 elementToMove <- self$get(content)
195 if(is.null(elementToMove))
196 stop("Content you want to move doesn't exist in the collection.")
198 elementToMove$move(destination)
201 copy = function(content, destination)
203 if(is.null(private$tree))
204 private$generateCollectionTreeStructure()
206 content <- trimFromEnd(content, "/")
208 elementToCopy <- self$get(content)
210 if(is.null(elementToCopy))
211 stop("Content you want to copy doesn't exist in the collection.")
213 elementToCopy$copy(destination)
218 private$tree$getTree()$setCollection(NULL, setRecursively = TRUE)
222 getFileListing = function()
224 if(is.null(private$tree))
225 private$generateCollectionTreeStructure()
227 content <- private$REST$getCollectionContent(self$uuid)
228 content[order(tolower(content))]
231 get = function(relativePath)
233 if(is.null(private$tree))
234 private$generateCollectionTreeStructure()
236 private$tree$getElement(relativePath)
239 getRESTService = function() private$REST,
240 setRESTService = function(newRESTService) private$REST <- newRESTService
249 generateCollectionTreeStructure = function()
251 if(is.null(self$uuid))
252 stop("Collection uuid is not defined.")
254 if(is.null(private$REST))
255 stop("REST service is not defined.")
257 private$fileContent <- private$REST$getCollectionContent(self$uuid)
258 private$tree <- CollectionTree$new(private$fileContent, self)
267 #' Custom print function for Collection class
269 #' @param x Instance of Collection class
270 #' @param ... Optional arguments.
272 print.Collection = function(x, ...)
274 cat(paste0("Type: ", "\"", "Arvados Collection", "\""), sep = "\n")
275 cat(paste0("uuid: ", "\"", x$uuid, "\""), sep = "\n")