1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
9 #' ArvadosFile class represents a file inside Arvados collection.
12 #' \preformatted{file = ArvadosFile$new(name)}
14 #' @section Arguments:
16 #' \item{name}{Name of the file.}
21 #' \item{getName()}{Returns name of the file.}
22 #' \item{getRelativePath()}{Returns file path relative to the root.}
23 #' \item{read(contentType = "raw", offset = 0, length = 0)}{Read file content.}
24 #' \item{write(content, contentType = "text/html")}{Write to file (override current content of the file).}
25 #' \item{connection(rw)}{Get connection opened in "read" or "write" mode.}
26 #' \item{flush()}{Write connections content to a file (override current content of the file).}
27 #' \item{remove(name)}{Removes ArvadosFile or Subcollection specified by name from the subcollection.}
28 #' \item{getSizeInBytes()}{Returns file size in bytes.}
29 #' \item{move(destination)}{Moves file to a new location inside collection.}
30 #' \item{copy(destination)}{Copies file to a new location inside collection.}
36 #' myFile <- ArvadosFile$new("myFile")
38 #' myFile$write("This is new file content")
39 #' fileContent <- myFile$read()
40 #' fileContent <- myFile$read("text")
41 #' fileContent <- myFile$read("raw", offset = 8, length = 4)
44 #' arvConnection <- myFile$connection("w")
45 #' write.table(mytable, arvConnection)
46 #' arvadosFile$flush()
49 #' arvConnection <- myFile$connection("r")
50 #' mytable <- read.table(arvConnection)
52 #' myFile$move("newFolder/myFile")
53 #' myFile$copy("newFolder/myFile")
58 ArvadosFile <- R6::R6Class(
64 initialize = function(name)
72 getName = function() private$name,
74 getFileListing = function(fullpath = TRUE)
79 getSizeInBytes = function()
81 if(is.null(private$collection))
84 REST <- private$collection$getRESTService()
86 fileSize <- REST$getResourceSize(self$getRelativePath(),
87 private$collection$uuid)
91 get = function(fileLikeObjectName)
101 getCollection = function() private$collection,
103 setCollection = function(collection, setRecursively = TRUE)
105 private$collection <- collection
108 getRelativePath = function()
110 relativePath <- c(private$name)
111 parent <- private$parent
113 while(!is.null(parent))
115 relativePath <- c(parent$getName(), relativePath)
116 parent <- parent$getParent()
119 relativePath <- relativePath[relativePath != ""]
120 paste0(relativePath, collapse = "/")
123 getParent = function() private$parent,
125 setParent = function(newParent) private$parent <- newParent,
127 read = function(contentType = "raw", offset = 0, length = 0)
129 if(is.null(private$collection))
130 stop("ArvadosFile doesn't belong to any collection.")
132 if(offset < 0 || length < 0)
133 stop("Offset and length must be positive values.")
135 REST <- private$collection$getRESTService()
137 fileContent <- REST$read(self$getRelativePath(),
138 private$collection$uuid,
139 contentType, offset, length)
143 connection = function(rw)
145 if (rw == "r" || rw == "rb")
147 REST <- private$collection$getRESTService()
148 return(REST$getConnection(self$getRelativePath(),
149 private$collection$uuid,
154 private$buffer <- textConnection(NULL, "w")
156 return(private$buffer)
162 v <- textConnectionValue(private$buffer)
163 close(private$buffer)
164 self$write(paste(v, collapse='\n'))
167 write = function(content, contentType = "text/html")
169 if(is.null(private$collection))
170 stop("ArvadosFile doesn't belong to any collection.")
172 REST <- private$collection$getRESTService()
174 writeResult <- REST$write(self$getRelativePath(),
175 private$collection$uuid,
176 content, contentType)
180 move = function(destination)
182 if(is.null(private$collection))
183 stop("ArvadosFile doesn't belong to any collection.")
185 destination <- trimFromEnd(destination, "/")
186 nameAndPath <- splitToPathAndName(destination)
188 newParent <- private$collection$get(nameAndPath$path)
190 if(is.null(newParent))
191 stop("Unable to get destination subcollection.")
193 childWithSameName <- newParent$get(nameAndPath$name)
195 if(!is.null(childWithSameName))
196 stop("Destination already contains content with same name.")
198 REST <- private$collection$getRESTService()
199 REST$move(self$getRelativePath(),
200 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
201 private$collection$uuid)
203 private$dettachFromCurrentParent()
204 private$attachToNewParent(self, newParent)
206 private$parent <- newParent
207 private$name <- nameAndPath$name
212 copy = function(destination)
214 if(is.null(private$collection))
215 stop("ArvadosFile doesn't belong to any collection.")
217 destination <- trimFromEnd(destination, "/")
218 nameAndPath <- splitToPathAndName(destination)
220 newParent <- private$collection$get(nameAndPath$path)
222 if(is.null(newParent))
223 stop("Unable to get destination subcollection.")
225 childWithSameName <- newParent$get(nameAndPath$name)
227 if(!is.null(childWithSameName))
228 stop("Destination already contains content with same name.")
230 REST <- private$collection$getRESTService()
231 REST$copy(self$getRelativePath(),
232 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
233 private$collection$uuid)
235 newFile <- self$duplicate(nameAndPath$name)
236 newFile$setCollection(self$getCollection())
237 private$attachToNewParent(newFile, newParent)
238 newFile$setParent(newParent)
243 duplicate = function(newName = NULL)
245 name <- if(!is.null(newName)) newName else private$name
246 newFile <- ArvadosFile$new(name)
259 attachToNewParent = function(content, newParent)
261 # We temporary set parents collection to NULL. This will ensure that
262 # add method doesn't post this file on REST.
263 # We also need to set content's collection to NULL because
264 # add method throws exception if we try to add content that already
265 # belongs to a collection.
266 parentsCollection <- newParent$getCollection()
267 content$setCollection(NULL, setRecursively = FALSE)
268 newParent$setCollection(NULL, setRecursively = FALSE)
269 newParent$add(content)
270 content$setCollection(parentsCollection, setRecursively = FALSE)
271 newParent$setCollection(parentsCollection, setRecursively = FALSE)
274 dettachFromCurrentParent = function()
276 # We temporary set parents collection to NULL. This will ensure that
277 # remove method doesn't remove this file from REST.
278 parent <- private$parent
279 parentsCollection <- parent$getCollection()
280 parent$setCollection(NULL, setRecursively = FALSE)
281 parent$remove(private$name)
282 parent$setCollection(parentsCollection, setRecursively = FALSE)
291 #' Custom print function for ArvadosFile class
293 #' @param x Instance of ArvadosFile class
294 #' @param ... Optional arguments.
296 print.ArvadosFile = function(x, ...)
299 relativePath <- x$getRelativePath()
301 if(!is.null(x$getCollection()))
303 collection <- x$getCollection()$uuid
304 relativePath <- paste0("/", relativePath)
307 cat(paste0("Type: ", "\"", "ArvadosFile", "\""), sep = "\n")
308 cat(paste0("Name: ", "\"", x$getName(), "\""), sep = "\n")
309 cat(paste0("Relative path: ", "\"", relativePath, "\""), sep = "\n")
310 cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n")