6 ArvadosFile <- R6::R6Class(
12 initialize = function(name)
15 private$http <- HttpRequest$new()
16 private$httpParser <- HttpParser$new()
19 getName = function() private$name,
21 getFileList = function(fullpath = TRUE)
26 getSizeInBytes = function()
28 collectionURL <- URLencode(paste0(private$collection$api$getWebDavHostName(),
29 "c=", private$collection$uuid))
30 fileURL <- paste0(collectionURL, "/", self$getRelativePath());
32 headers = list("Authorization" = paste("OAuth2", private$collection$api$getToken()))
34 propfindResponse <- private$http$PROPFIND(fileURL, headers)
36 sizes <- private$httpParser$extractFileSizeFromWebDAVResponse(propfindResponse, collectionURL)
40 removeFromCollection = function()
42 if(is.null(private$collection))
43 stop("ArvadosFile doesn't belong to any collection.")
45 private$collection$.__enclos_env__$private$deleteFromREST(self$getRelativePath())
47 private$addToCollection(NULL)
48 private$detachFromParent()
50 "Content removed successfully."
53 getRelativePath = function()
55 relativePath <- c(private$name)
56 parent <- private$parent
58 while(!is.null(parent))
60 relativePath <- c(parent$getName(), relativePath)
61 parent <- parent$getParent()
64 relativePath <- relativePath[relativePath != ""]
65 paste0(relativePath, collapse = "/")
68 getParent = function() private$parent,
70 read = function(contentType = "raw", offset = 0, length = 0)
72 if(is.null(private$collection))
73 stop("ArvadosFile doesn't belong to any collection.")
75 if(offset < 0 || length < 0)
76 stop("Offset and length must be positive values.")
78 if(!(contentType %in% private$http$validContentTypes))
79 stop("Invalid contentType. Please use text or raw.")
81 range = paste0("bytes=", offset, "-")
84 range = paste0(range, offset + length - 1)
86 fileURL = paste0(private$collection$api$getWebDavHostName(),
87 "c=", private$collection$uuid, "/", self$getRelativePath());
89 if(offset == 0 && length == 0)
91 headers <- list(Authorization = paste("OAuth2",
92 private$collection$api$getToken()))
96 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
100 serverResponse <- private$http$GET(fileURL, headers)
102 if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
103 stop(paste("Server code:", serverResponse$status_code))
105 parsedServerResponse <- httr::content(serverResponse, contentType)
109 write = function(content, contentType = "text/html")
111 if(is.null(private$collection))
112 stop("ArvadosFile doesn't belong to any collection.")
114 fileURL = paste0(private$collection$api$getWebDavHostName(),
115 "c=", private$collection$uuid, "/", self$getRelativePath());
116 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
117 "Content-Type" = contentType)
120 serverResponse <- private$http$PUT(fileURL, headers, body)
122 if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
123 stop(paste("Server code:", serverResponse$status_code))
125 parsedServerResponse <- httr::content(serverResponse, "text")
129 move = function(newLocation)
131 if(is.null(private$collection))
132 stop("ArvadosFile doesn't belong to any collection.")
134 if(endsWith(newLocation, paste0(private$name, "/")))
136 newLocation <- substr(newLocation, 0,
137 nchar(newLocation) - nchar(paste0(private$name, "/")))
139 else if(endsWith(newLocation, private$name))
141 newLocation <- substr(newLocation, 0, 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."
173 getChild = function(name)
178 getFirstChild = function()
183 addToCollection = function(collection)
185 private$collection <- collection
188 detachFromParent = function()
190 if(!is.null(private$parent))
192 private$parent$.__enclos_env__$private$removeChild(private$name)
193 private$parent <- NULL
197 attachToParent = function(parent)
199 parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
200 private$parent <- parent