6 ArvadosFile <- R6::R6Class(
12 initialize = function(name)
15 private$http <- HttpRequest$new()
16 private$httpParser <- HttpParser$new()
19 getName = function() private$name,
21 getFileListing = 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 get = function(fileLikeObjectName)
50 getCollection = function() private$collection,
52 setCollection = function(collection)
54 private$collection <- collection
57 getRelativePath = function()
59 relativePath <- c(private$name)
60 parent <- private$parent
62 while(!is.null(parent))
64 relativePath <- c(parent$getName(), relativePath)
65 parent <- parent$getParent()
68 relativePath <- relativePath[relativePath != ""]
69 paste0(relativePath, collapse = "/")
72 getParent = function() private$parent,
74 setParent = function(newParent) private$parent <- newParent,
76 read = function(contentType = "raw", offset = 0, length = 0)
78 if(is.null(private$collection))
79 stop("ArvadosFile doesn't belong to any collection.")
81 if(offset < 0 || length < 0)
82 stop("Offset and length must be positive values.")
84 if(!(contentType %in% private$http$validContentTypes))
85 stop("Invalid contentType. Please use text or raw.")
87 range = paste0("bytes=", offset, "-")
90 range = paste0(range, offset + length - 1)
92 fileURL = paste0(private$collection$api$getWebDavHostName(),
93 "c=", private$collection$uuid, "/", self$getRelativePath());
95 if(offset == 0 && length == 0)
97 headers <- list(Authorization = paste("OAuth2",
98 private$collection$api$getToken()))
102 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
106 serverResponse <- private$http$GET(fileURL, headers)
108 if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
109 stop(paste("Server code:", serverResponse$status_code))
111 parsedServerResponse <- httr::content(serverResponse, contentType)
115 connection = function(rw)
118 return(textConnection(self$read("text")))
119 } else if (rw == "w") {
120 private$buffer <- textConnection(NULL, "w")
121 return(private$buffer)
126 v <- textConnectionValue(private$buffer)
127 close(private$buffer)
128 self$write(paste(v, collapse='\n'))
131 write = function(content, contentType = "text/html")
133 if(is.null(private$collection))
134 stop("ArvadosFile doesn't belong to any collection.")
136 fileURL = paste0(private$collection$api$getWebDavHostName(),
137 "c=", private$collection$uuid, "/", self$getRelativePath());
138 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
139 "Content-Type" = contentType)
142 serverResponse <- private$http$PUT(fileURL, headers, body)
144 if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
145 stop(paste("Server code:", serverResponse$status_code))
147 parsedServerResponse <- httr::content(serverResponse, "text")
151 move = function(newLocation)
153 #todo test if file can be moved
155 if(is.null(private$collection))
156 stop("ArvadosFile doesn't belong to any collection.")
158 if(endsWith(newLocation, paste0(private$name, "/")))
160 newLocation <- substr(newLocation, 0,
162 - nchar(paste0(private$name, "/")))
164 else if(endsWith(newLocation, private$name))
166 newLocation <- substr(newLocation, 0,
167 nchar(newLocation) - nchar(private$name))
171 stop("Destination path is not valid.")
174 newParent <- private$collection$get(newLocation)
176 if(is.null(newParent))
178 stop("Unable to get destination subcollection.")
181 childWithSameName <- newParent$get(private$name)
183 if(!is.null(childWithSameName))
184 stop("Destination already contains file with same name.")
186 REST <- private$collection$getRESTService()
187 status <- REST$move(self$getRelativePath(),
188 paste0(newParent$getRelativePath(),
189 "/", self$getName()),
190 private$collection$uuid)
192 #Note: We temporary set parents collection to NULL. This will ensure that
193 # add method doesn't post file on REST server.
194 parentsCollection <- newParent$getCollection()
195 newParent$setCollection(NULL, setRecursively = FALSE)
199 newParent$setCollection(parentsCollection, setRecursively = FALSE)
201 private$parent <- newParent
203 "Content moved successfully."