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(), "c=", private$collection$uuid))
29 fileURL <- paste0(collectionURL, "/", self$getRelativePath());
31 headers = list("Authorization" = paste("OAuth2", private$collection$api$getToken()))
33 propfindResponse <- private$http$PROPFIND(fileURL, headers)
35 sizes <- private$httpParser$extractFileSizeFromWebDAVResponse(propfindResponse, collectionURL)
39 removeFromCollection = function()
41 if(is.null(private$collection))
42 stop("ArvadosFile doesn't belong to any collection.")
44 private$collection$.__enclos_env__$private$deleteFromREST(self$getRelativePath())
46 private$addToCollection(NULL)
47 private$detachFromParent()
49 "Content removed successfully."
52 getRelativePath = function()
54 relativePath <- c(private$name)
55 parent <- private$parent
57 while(!is.null(parent))
59 relativePath <- c(parent$getName(), relativePath)
60 parent <- parent$getParent()
63 relativePath <- relativePath[relativePath != ""]
64 paste0(relativePath, collapse = "/")
67 getParent = function() private$parent,
69 read = function(contentType = "raw", offset = 0, length = 0)
71 if(is.null(private$collection))
72 stop("ArvadosFile doesn't belong to any collection.")
74 if(offset < 0 || length < 0)
75 stop("Offset and length must be positive values.")
77 if(!(contentType %in% private$http$validContentTypes))
78 stop("Invalid contentType. Please use text or raw.")
80 range = paste0("bytes=", offset, "-")
83 range = paste0(range, offset + length - 1)
85 fileURL = paste0(private$collection$api$getWebDavHostName(), "c=", private$collection$uuid, "/", self$getRelativePath());
87 if(offset == 0 && length == 0)
89 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()))
93 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
97 serverResponse <- private$http$GET(fileURL, headers)
99 if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
100 stop(paste("Server code:", serverResponse$status_code))
102 parsedServerResponse <- httr::content(serverResponse, contentType)
106 write = function(content, contentType = "text/html")
108 if(is.null(private$collection))
109 stop("ArvadosFile doesn't belong to any collection.")
111 fileURL = paste0(private$collection$api$getWebDavHostName(), "c=", private$collection$uuid, "/", self$getRelativePath());
112 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
113 "Content-Type" = contentType)
116 serverResponse <- private$http$PUT(fileURL, headers, body)
118 if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
119 stop(paste("Server code:", serverResponse$status_code))
121 parsedServerResponse <- httr::content(serverResponse, "text")
125 move = function(newLocation)
127 if(is.null(private$collection))
128 stop("ArvadosFile doesn't belong to any collection.")
130 if(endsWith(newLocation, paste0(private$name, "/")))
132 newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(paste0(private$name, "/")))
134 else if(endsWith(newLocation, private$name))
136 newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(private$name))
140 stop("Destination path is not valid.")
143 newParent <- private$collection$get(newLocation)
145 if(is.null(newParent))
147 stop("Unable to get destination subcollection.")
150 status <- private$collection$.__enclos_env__$private$moveOnREST(self$getRelativePath(), paste0(newParent$getRelativePath(), "/", self$getName()))
152 private$attachToParent(newParent)
154 "Content moved successfully."
167 getChild = function(name)
172 getFirstChild = function()
177 addToCollection = function(collection)
179 private$collection <- collection
182 detachFromParent = function()
184 if(!is.null(private$parent))
186 private$parent$.__enclos_env__$private$removeChild(private$name)
187 private$parent <- NULL
191 attachToParent = function(parent)
193 parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
194 private$parent <- parent