1 source("./R/Subcollection.R")
2 source("./R/ArvadosFile.R")
3 source("./R/HttpRequest.R")
4 source("./R/HttpParser.R")
6 #' Arvados Collection Object
10 #' @examples arv = Collection$new(api, uuid)
12 Collection <- R6::R6Class(
21 initialize = function(api, uuid)
24 private$http <- HttpRequest$new()
25 private$httpParser <- HttpParser$new()
28 collection <- self$api$getCollection(uuid)
30 private$fileContent <- private$getCollectionContent()
31 private$tree <- CollectionTree$new(private$fileContent, self)
34 add = function(content, relativePath = "")
36 if(relativePath == "" ||
37 relativePath == "." ||
40 subcollection <- private$tree$.__enclos_env__$private$tree
44 if(endsWith(relativePath, "/") && nchar(relativePath) > 0)
45 relativePath <- substr(relativePath, 1, nchar(relativePath) - 1)
47 subcollection <- self$get(relativePath)
50 if(is.null(subcollection))
51 stop(paste("Subcollection", relativePath, "doesn't exist."))
53 if(is.character(content))
55 sapply(content, function(fileName)
57 subcollection$add(ArvadosFile$new(fileName))
60 else if("ArvadosFile" %in% class(content) ||
61 "Subcollection" %in% class(content))
63 subcollection$add(content)
67 remove = function(content)
69 if(is.character(content))
71 sapply(content, function(filePath)
73 if(endsWith(filePath, "/") && nchar(filePath) > 0)
74 filePath <- substr(filePath, 1, nchar(filePath) - 1)
76 file <- self$get(filePath)
79 stop(paste("File", filePath, "doesn't exist."))
81 file$removeFromCollection()
84 else if("ArvadosFile" %in% class(content) ||
85 "Subcollection" %in% class(content))
87 if(is.null(content$.__enclos_env__$private$collection) ||
88 content$.__enclos_env__$private$collection$uuid != self$uuid)
89 stop("Subcollection doesn't belong to this collection.")
91 content$removeFromCollection()
95 move = function(content, newLocation)
97 if(endsWith(content, "/"))
98 content <- substr(content, 0, nchar(content) - 1)
100 elementToMove <- self$get(content)
102 if(is.null(elementToMove))
103 stop("Element you want to move doesn't exist in the collection.")
105 elementToMove$move(newLocation)
108 getFileContent = function() private$getCollectionContent(),
110 get = function(relativePath)
112 private$tree$getElement(relativePath)
124 getCollectionContent = function()
126 collectionURL <- URLencode(paste0(self$api$getWebDavHostName(), "c=", self$uuid))
128 headers = list("Authorization" = paste("OAuth2", self$api$getToken()))
130 response <- private$http$PROPFIND(collectionURL, headers)
132 parsedResponse <- private$httpParser$parseWebDAVResponse(response, collectionURL)
136 createFilesOnREST = function(files)
138 sapply(files, function(filePath)
140 private$createNewFile(filePath, NULL, "text/html")
144 generateTree = function(content)
146 treeBranches <- sapply(collectionContent, function(filePath)
148 splitPath <- unlist(strsplit(filePath$name, "/", fixed = TRUE))
150 branch = private$createBranch(splitPath, filePath$fileSize)
154 createNewFile = function(relativePath, content, contentType)
156 fileURL <- paste0(self$api$getWebDavHostName(), "c=", self$uuid, "/", relativePath);
157 headers <- list(Authorization = paste("OAuth2", self$api$getToken()),
158 "Content-Type" = contentType)
161 serverResponse <- private$http$PUT(fileURL, headers, body)
163 if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
164 stop(paste("Server code:", serverResponse$status_code))
166 print(paste("File created:", relativePath))
169 deleteFromREST = function(relativePath)
171 fileURL <- paste0(self$api$getWebDavHostName(), "c=", self$uuid, "/", relativePath);
172 headers <- list(Authorization = paste("OAuth2", self$api$getToken()))
174 serverResponse <- private$http$DELETE(fileURL, headers)
176 if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
177 stop(paste("Server code:", serverResponse$status_code))
179 print(paste("File deleted:", relativePath))
182 moveOnREST = function(from, to)
184 collectionURL <- URLencode(paste0(self$api$getWebDavHostName(), "c=", self$uuid, "/"))
185 fromURL <- paste0(collectionURL, from)
186 toURL <- paste0(collectionURL, to)
188 headers = list("Authorization" = paste("OAuth2", self$api$getToken()),
189 "Destination" = toURL)
191 serverResponse <- private$http$MOVE(fromURL, headers)
193 if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
194 stop(paste("Server code:", serverResponse$status_code))