1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 #' R6 Class Representing a ArvadosFile
8 #' ArvadosFile class represents a file inside Arvados collection.
11 ArvadosFile <- R6::R6Class(
18 #' Initialize new enviroment.
19 #' @param name Name of the new enviroment.
20 #' @return A new `ArvadosFile` object.
23 #' myFile <- ArvadosFile$new("myFile")
25 initialize = function(name)
34 #' Returns name of the file.
37 #' arvadosFile$getName()
39 getName = function() private$name,
42 #' Returns collections file content as character vector.
43 #' @param fullPath Checking if TRUE.
46 #' arvadosFile$getFileListing()
48 getFileListing = function(fullpath = TRUE)
54 #' Returns collections content size in bytes.
57 #' arvadosFile$getSizeInBytes()
59 getSizeInBytes = function()
61 if(is.null(private$collection))
64 REST <- private$collection$getRESTService()
66 fileSize <- REST$getResourceSize(self$getRelativePath(),
67 private$collection$uuid)
71 get = function(fileLikeObjectName)
82 #' Returns collection UUID.
83 getCollection = function() private$collection,
86 #' Sets new collection.
87 setCollection = function(collection, setRecursively = TRUE)
89 private$collection <- collection
93 #' Returns file path relative to the root.
94 getRelativePath = function()
96 relativePath <- c(private$name)
97 parent <- private$parent
99 while(!is.null(parent))
101 relativePath <- c(parent$getName(), relativePath)
102 parent <- parent$getParent()
105 relativePath <- relativePath[relativePath != ""]
106 paste0(relativePath, collapse = "/")
110 #' Returns project UUID.
111 getParent = function() private$parent,
114 #' Sets project collection.
115 setParent = function(newParent) private$parent <- newParent,
118 #' Read file content.
119 #' @param contentType Type of content. Possible is "text", "raw".
120 #' @param offset Describes the location of a piece of data compared to another location
121 #' @param length Length of content
124 #' collection <- Collection$new(arv, collectionUUID)
125 #' arvadosFile <- collection$get(fileName)
126 #' fileContent <- arvadosFile$read("text")
128 read = function(contentType = "raw", offset = 0, length = 0)
130 if(is.null(private$collection))
131 stop("ArvadosFile doesn't belong to any collection.")
133 if(offset < 0 || length < 0)
134 stop("Offset and length must be positive values.")
136 REST <- private$collection$getRESTService()
138 fileContent <- REST$read(self$getRelativePath(),
139 private$collection$uuid,
140 contentType, offset, length)
145 #' Get connection opened in "read" or "write" mode.
146 #' @param rw Type of connection.
149 #' collection <- Collection$new(arv, collectionUUID)
150 #' arvadosFile <- collection$get(fileName)
151 #' arvConnection <- arvadosFile$connection("w")
153 connection = function(rw)
155 if (rw == "r" || rw == "rb")
157 REST <- private$collection$getRESTService()
158 return(REST$getConnection(self$getRelativePath(),
159 private$collection$uuid,
164 private$buffer <- textConnection(NULL, "w")
166 return(private$buffer)
171 #' Write connections content to a file or override current content of the file.
174 #' collection <- Collection$new(arv, collectionUUID)
175 #' arvadosFile <- collection$get(fileName)
176 #' myFile$write("This is new file content")
177 #' arvadosFile$flush()
181 v <- textConnectionValue(private$buffer)
182 close(private$buffer)
183 self$write(paste(v, collapse='\n'))
187 #' Write to file or override current content of the file.
188 #' @param content File to write.
189 #' @param contentType Type of content. Possible is "text", "raw".
192 #' collection <- Collection$new(arv, collectionUUID)
193 #' arvadosFile <- collection$get(fileName)
194 #' myFile$write("This is new file content")
196 write = function(content, contentType = "text/html")
198 if(is.null(private$collection))
199 stop("ArvadosFile doesn't belong to any collection.")
201 REST <- private$collection$getRESTService()
203 writeResult <- REST$write(self$getRelativePath(),
204 private$collection$uuid,
205 content, contentType)
210 #' Moves file to a new location inside collection.
211 #' @param destination Path to new folder.
214 #' arvadosFile$move(newPath)
216 move = function(destination)
218 if(is.null(private$collection))
219 stop("ArvadosFile doesn't belong to any collection.")
221 destination <- trimFromEnd(destination, "/")
222 nameAndPath <- splitToPathAndName(destination)
224 newParent <- private$collection$get(nameAndPath$path)
226 if(is.null(newParent))
227 stop("Unable to get destination subcollection.")
229 childWithSameName <- newParent$get(nameAndPath$name)
231 if(!is.null(childWithSameName))
232 stop("Destination already contains content with same name.")
234 REST <- private$collection$getRESTService()
235 REST$move(self$getRelativePath(),
236 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
237 private$collection$uuid)
239 private$dettachFromCurrentParent()
240 private$attachToNewParent(self, newParent)
242 private$parent <- newParent
243 private$name <- nameAndPath$name
249 #' Copies file to a new location inside collection.
250 #' @param destination Path to new folder.
253 #' arvadosFile$copy("NewName.format")
255 copy = function(destination)
257 if(is.null(private$collection))
258 stop("ArvadosFile doesn't belong to any collection.")
260 destination <- trimFromEnd(destination, "/")
261 nameAndPath <- splitToPathAndName(destination)
263 newParent <- private$collection$get(nameAndPath$path)
265 if(is.null(newParent))
266 stop("Unable to get destination subcollection.")
268 childWithSameName <- newParent$get(nameAndPath$name)
270 if(!is.null(childWithSameName))
271 stop("Destination already contains content with same name.")
273 REST <- private$collection$getRESTService()
274 REST$copy(self$getRelativePath(),
275 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
276 private$collection$uuid)
278 newFile <- self$duplicate(nameAndPath$name)
279 newFile$setCollection(self$getCollection())
280 private$attachToNewParent(newFile, newParent)
281 newFile$setParent(newParent)
287 #' Duplicate file and gives it a new name.
288 #' @param newName New name for duplicated file.
289 duplicate = function(newName = NULL)
291 name <- if(!is.null(newName)) newName else private$name
292 newFile <- ArvadosFile$new(name)
305 attachToNewParent = function(content, newParent)
307 # We temporary set parents collection to NULL. This will ensure that
308 # add method doesn't post this file on REST.
309 # We also need to set content's collection to NULL because
310 # add method throws exception if we try to add content that already
311 # belongs to a collection.
313 parentsCollection <- newParent$getCollection()
314 #parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
315 #private$parent <- parent
316 content$setCollection(NULL, setRecursively = FALSE)
317 newParent$setCollection(NULL, setRecursively = FALSE)
318 newParent$add(content)
319 content$setCollection(parentsCollection, setRecursively = FALSE)
320 newParent$setCollection(parentsCollection, setRecursively = FALSE)
323 dettachFromCurrentParent = function()
325 # We temporary set parents collection to NULL. This will ensure that
326 # remove method doesn't remove this file from REST.
328 #private$parent$.__enclos_env__$private$removeChild(private$name)
329 #private$parent <- NULL
330 parent <- private$parent
331 parentsCollection <- parent$getCollection()
332 parent$setCollection(NULL, setRecursively = FALSE)
333 parent$remove(private$name)
334 parent$setCollection(parentsCollection, setRecursively = FALSE)
343 #' Custom print function for ArvadosFile class
345 #' @param x Instance of ArvadosFile class
346 #' @param ... Optional arguments.
348 print.ArvadosFile = function(x, ...)
351 relativePath <- x$getRelativePath()
353 if(!is.null(x$getCollection()))
355 collection <- x$getCollection()$uuid
356 relativePath <- paste0("/", relativePath)
359 cat(paste0("Type: ", "\"", "ArvadosFile", "\""), sep = "\n")
360 cat(paste0("Name: ", "\"", x$getName(), "\""), sep = "\n")
361 cat(paste0("Relative path: ", "\"", relativePath, "\""), sep = "\n")
362 cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n")