X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/1012f12d29be01b56f2bbbe2e9bd5969d69f7b89..39c17737ac69d7693684fe2f95bef0ec235a28bf:/sdk/R/R/ArvadosFile.R diff --git a/sdk/R/R/ArvadosFile.R b/sdk/R/R/ArvadosFile.R index 3437933a73..70bb4450ec 100644 --- a/sdk/R/R/ArvadosFile.R +++ b/sdk/R/R/ArvadosFile.R @@ -1,10 +1,60 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 + source("./R/util.R") -#' ArvadosFile Object +#' ArvadosFile +#' +#' ArvadosFile class represents a file inside Arvados collection. +#' +#' @section Usage: +#' \preformatted{file = ArvadosFile$new(name)} +#' +#' @section Arguments: +#' \describe{ +#' \item{name}{Name of the file.} +#' } +#' +#' @section Methods: +#' \describe{ +#' \item{getName()}{Returns name of the file.} +#' \item{getRelativePath()}{Returns file path relative to the root.} +#' \item{read(contentType = "raw", offset = 0, length = 0)}{Read file content.} +#' \item{write(content, contentType = "text/html")}{Write to file (override current content of the file).} +#' \item{connection(rw)}{Get connection opened in "read" or "write" mode.} +#' \item{flush()}{Write connections content to a file (override current content of the file).} +#' \item{remove(name)}{Removes ArvadosFile or Subcollection specified by name from the subcollection.} +#' \item{getSizeInBytes()}{Returns file size in bytes.} +#' \item{move(destination)}{Moves file to a new location inside collection.} +#' \item{copy(destination)}{Copies file to a new location inside collection.} +#' } +#' +#' @name ArvadosFile +#' @examples +#' \dontrun{ +#' myFile <- ArvadosFile$new("myFile") #' -#' Update description +#' myFile$write("This is new file content") +#' fileContent <- myFile$read() +#' fileContent <- myFile$read("text") +#' fileContent <- myFile$read("raw", offset = 8, length = 4) #' -#' @export ArvadosFile +#' #Write a table: +#' arvConnection <- myFile$connection("w") +#' write.table(mytable, arvConnection) +#' arvadosFile$flush() +#' +#' #Read a table: +#' arvConnection <- myFile$connection("r") +#' mytable <- read.table(arvConnection) +#' +#' myFile$move("newFolder/myFile") +#' myFile$copy("newFolder/myFile") +#' } +NULL + +#' @export ArvadosFile <- R6::R6Class( "ArvadosFile", @@ -35,7 +85,6 @@ ArvadosFile <- R6::R6Class( fileSize <- REST$getResourceSize(self$getRelativePath(), private$collection$uuid) - fileSize }, @@ -51,7 +100,7 @@ ArvadosFile <- R6::R6Class( getCollection = function() private$collection, - setCollection = function(collection) + setCollection = function(collection, setRecursively = TRUE) { private$collection <- collection }, @@ -93,14 +142,14 @@ ArvadosFile <- R6::R6Class( connection = function(rw) { - if (rw == "r" || rw == "rb") + if (rw == "r" || rw == "rb") { REST <- private$collection$getRESTService() - return(REST$getConnection(private$collection$uuid, - self$getRelativePath(), + return(REST$getConnection(self$getRelativePath(), + private$collection$uuid, rw)) } - else if (rw == "w") + else if (rw == "w") { private$buffer <- textConnection(NULL, "w") @@ -108,7 +157,7 @@ ArvadosFile <- R6::R6Class( } }, - flush = function() + flush = function() { v <- textConnectionValue(private$buffer) close(private$buffer) @@ -128,20 +177,18 @@ ArvadosFile <- R6::R6Class( writeResult }, - move = function(newLocation) + move = function(destination) { if(is.null(private$collection)) - stop("ArvadosFile doesn't belong to any collection") + stop("ArvadosFile doesn't belong to any collection.") - newLocation <- trimFromEnd(newLocation, "/") - nameAndPath <- splitToPathAndName(newLocation) + destination <- trimFromEnd(destination, "/") + nameAndPath <- splitToPathAndName(destination) newParent <- private$collection$get(nameAndPath$path) if(is.null(newParent)) - { - stop("Unable to get destination subcollection") - } + stop("Unable to get destination subcollection.") childWithSameName <- newParent$get(nameAndPath$name) @@ -154,11 +201,50 @@ ArvadosFile <- R6::R6Class( private$collection$uuid) private$dettachFromCurrentParent() - private$attachToNewParent(newParent) + private$attachToNewParent(self, newParent) + private$parent <- newParent private$name <- nameAndPath$name - "Content moved successfully." + self + }, + + copy = function(destination) + { + if(is.null(private$collection)) + stop("ArvadosFile doesn't belong to any collection.") + + destination <- trimFromEnd(destination, "/") + nameAndPath <- splitToPathAndName(destination) + + newParent <- private$collection$get(nameAndPath$path) + + if(is.null(newParent)) + stop("Unable to get destination subcollection.") + + childWithSameName <- newParent$get(nameAndPath$name) + + if(!is.null(childWithSameName)) + stop("Destination already contains content with same name.") + + REST <- private$collection$getRESTService() + REST$copy(self$getRelativePath(), + paste0(newParent$getRelativePath(), "/", nameAndPath$name), + private$collection$uuid) + + newFile <- self$duplicate(nameAndPath$name) + newFile$setCollection(self$getCollection()) + private$attachToNewParent(newFile, newParent) + newFile$setParent(newParent) + + newFile + }, + + duplicate = function(newName = NULL) + { + name <- if(!is.null(newName)) newName else private$name + newFile <- ArvadosFile$new(name) + newFile } ), @@ -170,30 +256,29 @@ ArvadosFile <- R6::R6Class( collection = NULL, buffer = NULL, - attachToNewParent = function(newParent) + attachToNewParent = function(content, newParent) { - #Note: We temporary set parents collection to NULL. This will ensure that - # add method doesn't post file on REST. + # We temporary set parents collection to NULL. This will ensure that + # add method doesn't post this file on REST. + # We also need to set content's collection to NULL because + # add method throws exception if we try to add content that already + # belongs to a collection. parentsCollection <- newParent$getCollection() + content$setCollection(NULL, setRecursively = FALSE) newParent$setCollection(NULL, setRecursively = FALSE) - - newParent$add(self) - + newParent$add(content) + content$setCollection(parentsCollection, setRecursively = FALSE) newParent$setCollection(parentsCollection, setRecursively = FALSE) - - private$parent <- newParent }, dettachFromCurrentParent = function() { - #Note: We temporary set parents collection to NULL. This will ensure that - # remove method doesn't remove this subcollection from REST. + # We temporary set parents collection to NULL. This will ensure that + # remove method doesn't remove this file from REST. parent <- private$parent parentsCollection <- parent$getCollection() parent$setCollection(NULL, setRecursively = FALSE) - parent$remove(private$name) - parent$setCollection(parentsCollection, setRecursively = FALSE) } ), @@ -201,20 +286,26 @@ ArvadosFile <- R6::R6Class( cloneable = FALSE ) -#' @export print.ArvadosFile -print.ArvadosFile = function(arvadosFile) +#' print.ArvadosFile +#' +#' Custom print function for ArvadosFile class +#' +#' @param x Instance of ArvadosFile class +#' @param ... Optional arguments. +#' @export +print.ArvadosFile = function(x, ...) { collection <- NULL - relativePath <- arvadosFile$getRelativePath() + relativePath <- x$getRelativePath() - if(!is.null(arvadosFile$getCollection())) + if(!is.null(x$getCollection())) { - collection <- arvadosFile$getCollection()$uuid + collection <- x$getCollection()$uuid relativePath <- paste0("/", relativePath) } - cat(paste0("Type: ", "\"", "ArvadosFile", "\""), sep = "\n") - cat(paste0("Name: ", "\"", arvadosFile$getName(), "\""), sep = "\n") - cat(paste0("Relative path: ", "\"", relativePath, "\""), sep = "\n") - cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n") + cat(paste0("Type: ", "\"", "ArvadosFile", "\""), sep = "\n") + cat(paste0("Name: ", "\"", x$getName(), "\""), sep = "\n") + cat(paste0("Relative path: ", "\"", relativePath, "\""), sep = "\n") + cat(paste0("Collection: ", "\"", collection, "\""), sep = "\n") }