8 ArvadosFile <- R6::R6Class(
14 initialize = function(name)
22 getName = function() private$name,
24 getFileListing = function(fullpath = TRUE)
29 getSizeInBytes = function()
31 if(is.null(private$collection))
34 REST <- private$collection$getRESTService()
36 fileSize <- REST$getResourceSize(self$getRelativePath(),
37 private$collection$uuid)
42 get = function(fileLikeObjectName)
52 getCollection = function() private$collection,
54 setCollection = function(collection)
56 private$collection <- collection
59 getRelativePath = function()
61 relativePath <- c(private$name)
62 parent <- private$parent
64 while(!is.null(parent))
66 relativePath <- c(parent$getName(), relativePath)
67 parent <- parent$getParent()
70 relativePath <- relativePath[relativePath != ""]
71 paste0(relativePath, collapse = "/")
74 getParent = function() private$parent,
76 setParent = function(newParent) private$parent <- newParent,
78 read = function(contentType = "raw", offset = 0, length = 0)
80 if(is.null(private$collection))
81 stop("ArvadosFile doesn't belong to any collection.")
83 if(offset < 0 || length < 0)
84 stop("Offset and length must be positive values.")
86 REST <- private$collection$getRESTService()
88 fileContent <- REST$read(self$getRelativePath(),
89 private$collection$uuid,
90 contentType, offset, length)
94 connection = function(rw)
96 if (rw == "r" || rw == "rb")
98 REST <- private$collection$getRESTService()
99 return(REST$getConnection(private$collection$uuid,
100 self$getRelativePath(),
105 private$buffer <- textConnection(NULL, "w")
107 return(private$buffer)
113 v <- textConnectionValue(private$buffer)
114 close(private$buffer)
115 self$write(paste(v, collapse='\n'))
118 write = function(content, contentType = "text/html")
120 if(is.null(private$collection))
121 stop("ArvadosFile doesn't belong to any collection.")
123 REST <- private$collection$getRESTService()
125 writeResult <- REST$write(self$getRelativePath(),
126 private$collection$uuid,
127 content, contentType)
131 move = function(newLocation)
133 if(is.null(private$collection))
134 stop("ArvadosFile doesn't belong to any collection")
136 newLocation <- trimFromEnd(newLocation, "/")
137 nameAndPath <- splitToPathAndName(newLocation)
139 newParent <- private$collection$get(nameAndPath$path)
141 if(is.null(newParent))
143 stop("Unable to get destination subcollection")
146 childWithSameName <- newParent$get(nameAndPath$name)
148 if(!is.null(childWithSameName))
149 stop("Destination already contains content with same name.")
151 REST <- private$collection$getRESTService()
152 REST$move(self$getRelativePath(),
153 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
154 private$collection$uuid)
156 private$dettachFromCurrentParent()
157 private$attachToNewParent(newParent)
159 private$name <- nameAndPath$name
161 "Content moved successfully."
173 attachToNewParent = function(newParent)
175 #Note: We temporary set parents collection to NULL. This will ensure that
176 # add method doesn't post file on REST.
177 parentsCollection <- newParent$getCollection()
178 newParent$setCollection(NULL, setRecursively = FALSE)
182 newParent$setCollection(parentsCollection, setRecursively = FALSE)
184 private$parent <- newParent
187 dettachFromCurrentParent = function()
189 #Note: We temporary set parents collection to NULL. This will ensure that
190 # remove method doesn't remove this subcollection from REST.
191 parent <- private$parent
192 parentsCollection <- parent$getCollection()
193 parent$setCollection(NULL, setRecursively = FALSE)
195 parent$remove(private$name)
197 parent$setCollection(parentsCollection, setRecursively = FALSE)
204 #' @export print.ArvadosFile
205 print.ArvadosFile = function(arvadosFile)
208 relativePath <- arvadosFile$getRelativePath()
210 if(!is.null(arvadosFile$getCollection()))
212 collection <- arvadosFile$getCollection()$uuid
213 relativePath <- paste0("/", relativePath)
216 cat(paste0("Type: ", "\"", "ArvadosFile", "\""), sep = "\n")
217 cat(paste0("Name: ", "\"", arvadosFile$getName(), "\""), sep = "\n")
218 cat(paste0("Relative path: ", "\"", relativePath, "\""), sep = "\n")
219 cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n")