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 write = function(content, contentType = "text/html")
117 if(is.null(private$collection))
118 stop("ArvadosFile doesn't belong to any collection.")
120 fileURL = paste0(private$collection$api$getWebDavHostName(),
121 "c=", private$collection$uuid, "/", self$getRelativePath());
122 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
123 "Content-Type" = contentType)
126 serverResponse <- private$http$PUT(fileURL, headers, body)
128 if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
129 stop(paste("Server code:", serverResponse$status_code))
131 parsedServerResponse <- httr::content(serverResponse, "text")
135 move = function(newLocation)
137 #todo test if file can be moved
139 if(is.null(private$collection))
140 stop("ArvadosFile doesn't belong to any collection.")
142 if(endsWith(newLocation, paste0(private$name, "/")))
144 newLocation <- substr(newLocation, 0,
146 - nchar(paste0(private$name, "/")))
148 else if(endsWith(newLocation, private$name))
150 newLocation <- substr(newLocation, 0,
151 nchar(newLocation) - nchar(private$name))
155 stop("Destination path is not valid.")
158 newParent <- private$collection$get(newLocation)
160 if(is.null(newParent))
162 stop("Unable to get destination subcollection.")
165 childWithSameName <- newParent$get(private$name)
167 if(!is.null(childWithSameName))
168 stop("Destination already contains file with same name.")
170 status <- private$collection$moveOnREST(self$getRelativePath(),
171 paste0(newParent$getRelativePath(),
172 "/", self$getName()))
174 #Note: We temporary set parents collection to NULL. This will ensure that
175 # add method doesn't post file on REST server.
176 parentsCollection <- newParent$getCollection()
177 newParent$setCollection(NULL, setRecursively = FALSE)
181 newParent$setCollection(parentsCollection, setRecursively = FALSE)
183 private$parent <- newParent
185 "Content moved successfully."