1 source("./R/Subcollection.R")
2 source("./R/ArvadosFile.R")
3 source("./R/FileTree.R")
4 source("./R/HttpRequest.R")
5 source("./R/HttpParser.R")
7 #' Arvados Collection Object
11 #' @examples arv = Collection$new(api, uuid)
13 Collection <- R6::R6Class(
19 #Todo(Fudo): Encapsulate this?
24 modified_by_client_uuid = NULL,
25 modified_by_user_uuid = NULL,
27 portable_data_hash = NULL,
28 replication_desired = NULL,
29 replication_confirmed_at = NULL,
30 replication_confirmed = NULL,
41 initialize = function(api, uuid)
44 result <- private$api$getCollection(uuid)
46 self$uuid <- result$uuid
47 self$etag <- result$etag
48 self$owner_uuid <- result$owner_uuid
49 self$created_at <- result$created_at
50 self$modified_by_client_uuid <- result$modified_by_client_uuid
51 self$modified_by_user_uuid <- result$modified_by_user_uuid
52 self$modified_at <- result$modified_at
53 self$portable_data_hash <- result$portable_data_hash
54 self$replication_desired <- result$replication_desired
55 self$replication_confirmed_at <- result$replication_confirmed_at
56 self$replication_confirmed <- result$replication_confirmed
57 self$updated_at <- result$updated_at
58 self$manifest_text <- result$manifest_text
59 self$name <- result$name
60 self$description <- result$description
61 self$properties <- result$properties
62 self$delete_at <- result$delete_at
63 self$file_names <- result$file_names
64 self$trash_at <- result$trash_at
65 self$is_trashed <- result$is_trashed
67 private$http <- HttpRequest$new()
68 private$httpParser <- HttpParser$new()
70 private$fileItems <- private$getCollectionContent()
71 private$fileTree <- FileTree$new(private$fileItems)
75 printFileContent = function()
77 private$fileTree$printContent(private$fileTree$getRoot(), 0)
80 getFileContent = function()
82 sapply(private$fileItems, function(file)
88 get = function(relativePath)
90 treeNode <- private$fileTree$traverseInOrder(private$fileTree$getRoot(), function(node)
92 if(node$relativePath == relativePath)
98 if(!is.null(treeNode))
100 return(private$createSubcollectionTree(treeNode))
108 createNewFile = function(relativePath, content, contentType)
110 fileURL <- paste0(private$api$getWebDavHostName(), "c=", self$uuid, "/", relativePath);
111 headers <- list(Authorization = paste("OAuth2", private$api$getToken()),
112 "Content-Type" = contentType)
115 serverResponse <- private$http$PUT(fileURL, headers, body)
117 if(serverResponse$status_code != 201)
118 stop(paste("Server code:", serverResponse$status_code))
120 fileSize = private$getNewFileSize(relativePath)
121 private$fileTree$addNode(relativePath, fileSize)
123 paste0("File created (size = ", fileSize , ")")
126 removeFile = function(relativePath)
128 node <- private$fileTree$getNode(relativePath)
131 stop("File doesn't exists.")
133 fileURL <- paste0(private$api$getWebDavHostName(), "c=", self$uuid, "/", relativePath);
134 headers <- list(Authorization = paste("OAuth2", private$api$getToken()))
136 serverResponse <- private$http$DELETE(fileURL, headers)
138 if(serverResponse$status_code != 204)
139 stop(paste("Server code:", serverResponse$status_code))
144 update = function(subcollection, event)
146 #Todo(Fudo): Add some king of check here later on.
147 if(event == "File size changed")
149 private$handleFileSizeChange(subcollection$getRelativePath(),
150 subcollection$getSizeInBytes())
156 items = function(value)
159 return(private$fileItems)
161 print("Value is read-only.")
175 handleFileSizeChange = function(filePath, newSize)
177 node <- private$fileTree$getNode(filePath)
180 stop("File doesn't exits")
185 createSubcollectionTree = function(treeNode)
187 if(treeNode$hasChildren())
191 for(child in treeNode$children)
193 child <- private$createSubcollectionTree(child)
194 children <- c(children, child)
197 return(Subcollection$new(treeNode$name, treeNode$relativePath, children))
201 if(treeNode$type == "file")
202 return(ArvadosFile$new(treeNode$name, treeNode$relativePath, treeNode$size, private$api, self))
204 return(Subcollection$new(treeNode$name, treeNode$relativePath, NULL))
208 getCollectionContent = function()
210 collectionURL <- URLencode(paste0(private$api$getWebDavHostName(), "c=", self$uuid))
212 headers = list("Authorization" = paste("OAuth2", private$api$getToken()))
214 response <- private$http$PROPFIND(collectionURL, headers)
216 parsedResponse <- private$httpParser$parseWebDAVResponse(response, collectionURL)
220 getNewFileSize = function(relativePath)
222 collectionURL <- URLencode(paste0(private$api$getWebDavHostName(), "c=", self$uuid))
223 fileURL = paste0(collectionURL, "/", relativePath);
224 headers = list("Authorization" = paste("OAuth2", private$api$getToken()))
226 propfindResponse <- private$http$PROPFIND(fileURL, headers)
228 fileInfo <- private$httpParser$parseWebDAVResponse(propfindResponse, collectionURL)
230 fileInfo[[1]]$fileSize