When env variable ARVADOS_API_HOST_INSECURE is set HTTP client will not try to valida...
[arvados.git] / sdk / R / R / ArvadosFile.R
index cab785009afebabe780543a16d79a6bb2ad7707f..e28ba9606cfebd95a89a436b7ac9953c98d63fb8 100644 (file)
@@ -1,10 +1,54 @@
 source("./R/util.R")
 
-#' ArvadosFile Object
+#' ArvadosFile
+#' 
+#' ArvadosFile class represents a file inside Arvados collection.
+#' 
+#' @section Usage:
+#' \preformatted{file = ArvadosFile$new(name)}
 #'
-#' Update description
+#' @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(newLocation)}{Moves file to a new location inside collection.}
+#' }
 #'
-#' @export ArvadosFile
+#' @name ArvadosFile
+#' @examples
+#' \dontrun{
+#' myFile <- ArvadosFile$new("myFile")
+#'
+#' myFile$write("This is new file content")
+#' fileContent <- myFile$read()
+#' fileContent <- myFile$read("text")
+#' fileContent <- myFile$read("raw", offset = 8, length = 4) 
+#'
+#' #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")
+#' }
+NULL
+
+#' @export
 ArvadosFile <- R6::R6Class(
 
     "ArvadosFile",
@@ -13,9 +57,10 @@ ArvadosFile <- R6::R6Class(
 
         initialize = function(name)
         {
-            private$name             <- name
-            private$http             <- HttpRequest$new()
-            private$httpParser       <- HttpParser$new()
+            if(name == "")
+                stop("Invalid name.")
+
+            private$name <- name
         },
 
         getName = function() private$name,
@@ -92,9 +137,12 @@ ArvadosFile <- R6::R6Class(
 
         connection = function(rw)
         {
-            if (rw == "r") 
+            if (rw == "r" || rw == "rb"
             {
-                return(textConnection(self$read("text")))
+                REST <- private$collection$getRESTService()
+                return(REST$getConnection(private$collection$uuid,
+                                          self$getRelativePath(),
+                                          rw))
             }
             else if (rw == "w") 
             {
@@ -124,34 +172,36 @@ ArvadosFile <- R6::R6Class(
             writeResult
         },
 
-        move = function(newLocationInCollection)
+        move = function(newLocation)
         {
             if(is.null(private$collection))
                 stop("ArvadosFile doesn't belong to any collection")
 
-            newLocationInCollection <- trimFromEnd(newLocationInCollection, "/")
-            newParentLocation <- trimFromEnd(newLocationInCollection, private$name)
+            newLocation <- trimFromEnd(newLocation, "/")
+            nameAndPath <- splitToPathAndName(newLocation)
 
-            newParent <- private$collection$get(newParentLocation)
+            newParent <- private$collection$get(nameAndPath$path)
 
             if(is.null(newParent))
             {
                 stop("Unable to get destination subcollection")
             }
 
-            childWithSameName <- newParent$get(private$name)
+            childWithSameName <- newParent$get(nameAndPath$name)
 
             if(!is.null(childWithSameName))
                 stop("Destination already contains content with same name.")
 
             REST <- private$collection$getRESTService()
             REST$move(self$getRelativePath(),
-                      paste0(newParent$getRelativePath(), "/", self$getName()),
+                      paste0(newParent$getRelativePath(), "/", nameAndPath$name),
                       private$collection$uuid)
 
             private$dettachFromCurrentParent()
             private$attachToNewParent(newParent)
 
+            private$name <- nameAndPath$name
+
             "Content moved successfully."
         }
     ),
@@ -162,8 +212,6 @@ ArvadosFile <- R6::R6Class(
         size       = NULL,
         parent     = NULL,
         collection = NULL,
-        http       = NULL,
-        httpParser = NULL,
         buffer     = NULL,
 
         attachToNewParent = function(newParent)
@@ -196,3 +244,27 @@ ArvadosFile <- R6::R6Class(
 
     cloneable = FALSE
 )
+
+#' 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 <- x$getRelativePath()
+
+    if(!is.null(x$getCollection()))
+    {
+        collection <- x$getCollection()$uuid
+        relativePath <- paste0("/", relativePath)
+    }
+
+    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")
+}