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.
22 #' myFile <- ArvadosFile$new("myFile")
23 initialize = function(name)
32 #' Returns name of the file.
34 #' arvadosFile$getName()
35 getName = function() private$name,
38 #' Returns collections file content as character vector.
39 #' @param fullPath Checking if TRUE.
41 #' arvadosFile$getFileListing()
42 getFileListing = function(fullpath = TRUE)
48 #' Returns collections content size in bytes.
50 #' arvadosFile$getSizeInBytes()
51 getSizeInBytes = function()
53 if(is.null(private$collection))
56 REST <- private$collection$getRESTService()
58 fileSize <- REST$getResourceSize(self$getRelativePath(),
59 private$collection$uuid)
63 get = function(fileLikeObjectName)
74 #' Returns collection UUID.
75 getCollection = function() private$collection,
78 #' Sets new collection.
79 setCollection = function(collection, setRecursively = TRUE)
81 private$collection <- collection
85 #' Returns file path relative to the root.
86 getRelativePath = function()
88 relativePath <- c(private$name)
89 parent <- private$parent
91 while(!is.null(parent))
93 relativePath <- c(parent$getName(), relativePath)
94 parent <- parent$getParent()
97 relativePath <- relativePath[relativePath != ""]
98 paste0(relativePath, collapse = "/")
102 #' Returns project UUID.
103 getParent = function() private$parent,
106 #' Sets project collection.
107 setParent = function(newParent) private$parent <- newParent,
110 #' Read file content.
111 #' @param contentType Type of content. Possible is "text", "raw".
112 #' @param offset Describes the location of a piece of data compared to another location
113 #' @param length Length of content
115 #' collection <- Collection$new(arv, collectionUUID)
116 #' arvadosFile <- collection$get(fileName)
117 #' fileContent <- arvadosFile$read("text")
118 read = function(contentType = "raw", offset = 0, length = 0)
120 if(is.null(private$collection))
121 stop("ArvadosFile doesn't belong to any collection.")
123 if(offset < 0 || length < 0)
124 stop("Offset and length must be positive values.")
126 REST <- private$collection$getRESTService()
128 fileContent <- REST$read(self$getRelativePath(),
129 private$collection$uuid,
130 contentType, offset, length)
135 #' Get connection opened in "read" or "write" mode.
136 #' @param rw Type of connection.
138 #' collection <- Collection$new(arv, collectionUUID)
139 #' arvadosFile <- collection$get(fileName)
140 #' arvConnection <- arvadosFile$connection("w")
141 connection = function(rw)
143 if (rw == "r" || rw == "rb")
145 REST <- private$collection$getRESTService()
146 return(REST$getConnection(self$getRelativePath(),
147 private$collection$uuid,
152 private$buffer <- textConnection(NULL, "w")
154 return(private$buffer)
159 #' Write connections content to a file or override current content of the file.
161 #' collection <- Collection$new(arv, collectionUUID)
162 #' arvadosFile <- collection$get(fileName)
163 #' myFile$write("This is new file content")
164 #' arvadosFile$flush()
167 v <- textConnectionValue(private$buffer)
168 close(private$buffer)
169 self$write(paste(v, collapse='\n'))
173 #' Write to file or override current content of the file.
174 #' @param content File to write.
175 #' @param contentType Type of content. Possible is "text", "raw".
177 #' collection <- Collection$new(arv, collectionUUID)
178 #' arvadosFile <- collection$get(fileName)
179 #' myFile$write("This is new file content")
180 write = function(content, contentType = "text/html")
182 if(is.null(private$collection))
183 stop("ArvadosFile doesn't belong to any collection.")
185 REST <- private$collection$getRESTService()
187 writeResult <- REST$write(self$getRelativePath(),
188 private$collection$uuid,
189 content, contentType)
194 #' Moves file to a new location inside collection.
195 #' @param destination Path to new folder.
197 #' arvadosFile$move(newPath)
198 move = function(destination)
200 if(is.null(private$collection))
201 stop("ArvadosFile doesn't belong to any collection.")
203 destination <- trimFromEnd(destination, "/")
204 nameAndPath <- splitToPathAndName(destination)
206 newParent <- private$collection$get(nameAndPath$path)
208 if(is.null(newParent))
209 stop("Unable to get destination subcollection.")
211 childWithSameName <- newParent$get(nameAndPath$name)
213 if(!is.null(childWithSameName))
214 stop("Destination already contains content with same name.")
216 REST <- private$collection$getRESTService()
217 REST$move(self$getRelativePath(),
218 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
219 private$collection$uuid)
221 private$dettachFromCurrentParent()
222 private$attachToNewParent(self, newParent)
224 private$parent <- newParent
225 private$name <- nameAndPath$name
231 #' Copies file to a new location inside collection.
232 #' @param destination Path to new folder.
234 #' arvadosFile$copy("NewName.format")
235 copy = function(destination)
237 if(is.null(private$collection))
238 stop("ArvadosFile doesn't belong to any collection.")
240 destination <- trimFromEnd(destination, "/")
241 nameAndPath <- splitToPathAndName(destination)
243 newParent <- private$collection$get(nameAndPath$path)
245 if(is.null(newParent))
246 stop("Unable to get destination subcollection.")
248 childWithSameName <- newParent$get(nameAndPath$name)
250 if(!is.null(childWithSameName))
251 stop("Destination already contains content with same name.")
253 REST <- private$collection$getRESTService()
254 REST$copy(self$getRelativePath(),
255 paste0(newParent$getRelativePath(), "/", nameAndPath$name),
256 private$collection$uuid)
258 newFile <- self$duplicate(nameAndPath$name)
259 newFile$setCollection(self$getCollection())
260 private$attachToNewParent(newFile, newParent)
261 newFile$setParent(newParent)
267 #' Duplicate file and gives it a new name.
268 #' @param newName New name for duplicated file.
269 duplicate = function(newName = NULL)
271 name <- if(!is.null(newName)) newName else private$name
272 newFile <- ArvadosFile$new(name)
285 attachToNewParent = function(content, newParent)
287 # We temporary set parents collection to NULL. This will ensure that
288 # add method doesn't post this file on REST.
289 # We also need to set content's collection to NULL because
290 # add method throws exception if we try to add content that already
291 # belongs to a collection.
293 parentsCollection <- newParent$getCollection()
294 #parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
295 #private$parent <- parent
296 content$setCollection(NULL, setRecursively = FALSE)
297 newParent$setCollection(NULL, setRecursively = FALSE)
298 newParent$add(content)
299 content$setCollection(parentsCollection, setRecursively = FALSE)
300 newParent$setCollection(parentsCollection, setRecursively = FALSE)
303 dettachFromCurrentParent = function()
305 # We temporary set parents collection to NULL. This will ensure that
306 # remove method doesn't remove this file from REST.
308 #private$parent$.__enclos_env__$private$removeChild(private$name)
309 #private$parent <- NULL
310 parent <- private$parent
311 parentsCollection <- parent$getCollection()
312 parent$setCollection(NULL, setRecursively = FALSE)
313 parent$remove(private$name)
314 parent$setCollection(parentsCollection, setRecursively = FALSE)
323 #' Custom print function for ArvadosFile class
325 #' @param x Instance of ArvadosFile class
326 #' @param ... Optional arguments.
328 print.ArvadosFile = function(x, ...)
331 relativePath <- x$getRelativePath()
333 if(!is.null(x$getCollection()))
335 collection <- x$getCollection()$uuid
336 relativePath <- paste0("/", relativePath)
339 cat(paste0("Type: ", "\"", "ArvadosFile", "\""), sep = "\n")
340 cat(paste0("Name: ", "\"", x$getName(), "\""), sep = "\n")
341 cat(paste0("Relative path: ", "\"", relativePath, "\""), sep = "\n")
342 cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n")