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(newLocation)}{Moves file to a new location inside collection.}
35 #' myFile <- ArvadosFile$new("myFile")
37 #' myFile$write("This is new file content")
38 #' fileContent <- myFile$read()
39 #' fileContent <- myFile$read("text")
40 #' fileContent <- myFile$read("raw", offset = 8, length = 4)
43 #' arvConnection <- myFile$connection("w")
44 #' write.table(mytable, arvConnection)
45 #' arvadosFile$flush()
48 #' arvConnection <- myFile$connection("r")
49 #' mytable <- read.table(arvConnection)
51 #' myFile$move("newFolder/myFile")
56 ArvadosFile <- R6::R6Class(
62 initialize = function(name)
70 getName = function() private$name,
72 getFileListing = function(fullpath = TRUE)
77 getSizeInBytes = function()
79 if(is.null(private$collection))
82 REST <- private$collection$getRESTService()
84 fileSize <- REST$getResourceSize(self$getRelativePath(),
85 private$collection$uuid)
90 get = function(fileLikeObjectName)
100 getCollection = function() private$collection,
102 setCollection = function(collection)
104 private$collection <- collection
107 getRelativePath = function()
109 relativePath <- c(private$name)
110 parent <- private$parent
112 while(!is.null(parent))
114 relativePath <- c(parent$getName(), relativePath)
115 parent <- parent$getParent()
118 relativePath <- relativePath[relativePath != ""]
119 paste0(relativePath, collapse = "/")
122 getParent = function() private$parent,
124 setParent = function(newParent) private$parent <- newParent,
126 read = function(contentType = "raw", offset = 0, length = 0)
128 if(is.null(private$collection))
129 stop("ArvadosFile doesn't belong to any collection.")
131 if(offset < 0 || length < 0)
132 stop("Offset and length must be positive values.")
134 REST <- private$collection$getRESTService()
136 fileContent <- REST$read(self$getRelativePath(),
137 private$collection$uuid,
138 contentType, offset, length)
142 connection = function(rw)
144 if (rw == "r" || rw == "rb")
146 REST <- private$collection$getRESTService()
147 return(REST$getConnection(private$collection$uuid,
148 self$getRelativePath(),
153 private$buffer <- textConnection(NULL, "w")
155 return(private$buffer)
161 v <- textConnectionValue(private$buffer)
162 close(private$buffer)
163 self$write(paste(v, collapse='\n'))
166 write = function(content, contentType = "text/html")
168 if(is.null(private$collection))
169 stop("ArvadosFile doesn't belong to any collection.")
171 REST <- private$collection$getRESTService()
173 writeResult <- REST$write(self$getRelativePath(),
174 private$collection$uuid,
175 content, contentType)
179 move = function(newLocation)
181 if(is.null(private$collection))
182 stop("ArvadosFile doesn't belong to any collection")
184 newLocation <- trimFromEnd(newLocation, "/")
185 nameAndPath <- splitToPathAndName(newLocation)
187 newParent <- private$collection$get(nameAndPath$path)
189 if(is.null(newParent))
191 stop("Unable to get destination subcollection")
194 childWithSameName <- newParent$get(nameAndPath$name)
196 if(!is.null(childWithSameName))
197 stop("Destination already contains content with same name.")
199 REST <- private$collection$getRESTService()
200 REST$move(self$getRelativePath(),
201 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
202 private$collection$uuid)
204 private$dettachFromCurrentParent()
205 private$attachToNewParent(newParent)
207 private$name <- nameAndPath$name
209 "Content moved successfully."
221 attachToNewParent = function(newParent)
223 #Note: We temporary set parents collection to NULL. This will ensure that
224 # add method doesn't post file on REST.
225 parentsCollection <- newParent$getCollection()
226 newParent$setCollection(NULL, setRecursively = FALSE)
230 newParent$setCollection(parentsCollection, setRecursively = FALSE)
232 private$parent <- newParent
235 dettachFromCurrentParent = function()
237 #Note: We temporary set parents collection to NULL. This will ensure that
238 # remove method doesn't remove this subcollection from REST.
239 parent <- private$parent
240 parentsCollection <- parent$getCollection()
241 parent$setCollection(NULL, setRecursively = FALSE)
243 parent$remove(private$name)
245 parent$setCollection(parentsCollection, setRecursively = FALSE)
254 #' Custom print function for ArvadosFile class
256 #' @param x Instance of ArvadosFile class
257 #' @param ... Optional arguments.
259 print.ArvadosFile = function(x, ...)
262 relativePath <- x$getRelativePath()
264 if(!is.null(x$getCollection()))
266 collection <- x$getCollection()$uuid
267 relativePath <- paste0("/", relativePath)
270 cat(paste0("Type: ", "\"", "ArvadosFile", "\""), sep = "\n")
271 cat(paste0("Name: ", "\"", x$getName(), "\""), sep = "\n")
272 cat(paste0("Relative path: ", "\"", relativePath, "\""), sep = "\n")
273 cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n")