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("Subcollection doesn't belong to any collection.")
44 private$collection$.__enclos_env__$private$deleteFromREST(self$getRelativePath())
46 #todo rename this add to a collection
47 private$addToCollection(NULL)
48 private$detachFromParent()
51 getRelativePath = function()
53 relativePath <- c(private$name)
54 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(offset = 0, length = 0)
71 #todo range is wrong fix it
72 if(offset < 0 || length < 0)
73 stop("Offset and length must be positive values.")
75 range = paste0("bytes=", offset, "-")
78 range = paste0(range, offset + length - 1)
80 fileURL = paste0(private$collection$api$getWebDavHostName(), "c=", private$collection$uuid, "/", self$getRelativePath());
81 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
84 serverResponse <- private$http$GET(fileURL, headers)
86 if(serverResponse$status_code != 206)
87 stop(paste("Server code:", serverResponse$status_code))
89 parsedServerResponse <- httr::content(serverResponse, "raw")
93 write = function(content, contentType = "text/html")
95 fileURL = paste0(private$collection$api$getWebDavHostName(), "c=", private$collection$uuid, "/", self$getRelativePath());
96 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
97 "Content-Type" = contentType)
100 serverResponse <- private$http$PUT(fileURL, headers, body)
102 if(serverResponse$status_code != 201)
103 stop(paste("Server code:", serverResponse$status_code))
105 parsedServerResponse <- httr::content(serverResponse, "text")
109 move = function(newLocation)
111 if(endsWith(newLocation, paste0(private$name, "/")))
113 newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(paste0(private$name, "/")))
115 else if(endsWith(newLocation, private$name))
117 newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(private$name))
121 stop("Destination path is not valid.")
124 newParent <- private$collection$get(newLocation)
126 if(is.null(newParent))
128 stop("Unable to get destination subcollectin")
131 status <- private$collection$.__enclos_env__$private$moveOnRest(self$getRelativePath(), paste0(newParent$getRelativePath(), "/", self$getName()))
133 private$attachToParent(newParent)
135 paste("Status code :", status$status_code)
148 getChild = function(name)
153 getFirstChild = function()
158 addToCollection = function(collection)
160 private$collection = collection
163 detachFromParent = function()
165 if(!is.null(private$parent))
167 private$parent$.__enclos_env__$private$removeChild(private$name)
168 private$parent <- NULL
172 attachToParent = function(parent)
174 parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
175 private$parent <- parent