Added feature to create new file in a particular collection.
authorFuad Muhic <fmuhic@capeannenterprises.com>
Thu, 14 Dec 2017 16:22:01 +0000 (17:22 +0100)
committerFuad Muhic <fmuhic@capeannenterprises.com>
Thu, 14 Dec 2017 16:22:01 +0000 (17:22 +0100)
Arvados-DCO-1.1-Signed-off-by: Fuad Muhic <fmuhic@capeannenterprises.com>

sdk/R/.RData
sdk/R/R/Arvados.R
sdk/R/R/ArvadosFile.R
sdk/R/R/Collection.R
sdk/R/R/FileTree.R
sdk/R/R/HttpRequest.R
sdk/R/R/Subcollection.R
sdk/R/README

index 4f8832049e221b7a4fe99ba68c6a6fd84dec5ae3..5745e7da592274eafd78bedff38f6bd491fca3bf 100644 (file)
Binary files a/sdk/R/.RData and b/sdk/R/.RData differ
index c768e49b923ffd15e3bc21d8ad0f2fd3e90ec2db..7d61d773c03b44d3914e57e53ece449d0d5c3c8e 100644 (file)
@@ -34,10 +34,10 @@ Arvados <- R6::R6Class(
             version <- "v1"
             host  <- paste0("https://", host, "/arvados/", version, "/")
 
-            private$http <- HttpRequest$new()
+            private$http       <- HttpRequest$new()
             private$httpParser <- HttpParser$new()
-            private$token <- token
-            private$host <- host
+            private$token      <- token
+            private$host       <- host
             
             headers <- list(Authorization = paste("OAuth2", private$token))
 
@@ -73,8 +73,7 @@ Arvados <- R6::R6Class(
             collectionURL <- paste0(private$host, "collections")
             headers <- list(Authorization = paste("OAuth2", private$token))
 
-            serverResponse <- private$http$GET(collectionURL, headers, NULL, filters, limit, offset)
-
+            serverResponse <- private$http$GET(collectionURL, headers, filters, limit, offset)
             collection <- private$httpParser$parseJSONResponse(serverResponse)
 
             if(!is.null(collection$errors))
index da8692d74a1aa0993e45f2f436f1d3bf07da1d35..563eaa2994835d8758b42783aeee381b65324d90 100644 (file)
@@ -45,11 +45,11 @@ ArvadosFile <- R6::R6Class(
             if(serverResponse$status_code != 206)
                 stop(paste("Server code:", serverResponse$status_code))
 
-            collection
-            parsed_response <- httr::content(serverResponse, "raw")
+            parsedServerResponse <- httr::content(serverResponse, "raw")
+            parsedServerResponse
         },
         
-        write = function(content, contentType)
+        write = function(content, contentType = "text/html")
         {
             fileURL = paste0(private$api$getWebDavHostName(), "c=", private$collection$uuid, "/", private$relativePath);
             headers <- list(Authorization = paste("OAuth2", private$api$getToken()), 
@@ -61,23 +61,10 @@ ArvadosFile <- R6::R6Class(
             if(serverResponse$status_code != 201)
                 stop(paste("Server code:", serverResponse$status_code))
 
-            #Note(Fudo): Everything went well we need to update file size 
-            # in collection tree.
+            private$notifyCollectionThatFileSizeChanges()
 
-            #Todo(Fudo): Move this into HttpRequest
-            uri <- URLencode(paste0(private$api$getWebDavHostName(), "c=", private$collection$uuid))
-            h <- curl::new_handle()
-            curl::handle_setopt(h, customrequest = "PROPFIND")
-
-            curl::handle_setheaders(h, "Authorization" = paste("OAuth2", private$api$getToken()))
-            propfindResponse <- curl::curl_fetch_memory(fileURL, h)
-
-            fileInfo <- private$httpParser$parseWebDAVResponse(propfindResponse, uri)
-
-            private$size <- fileInfo[[1]]$fileSize
-            private$collection$update(self, "File size changed")
-
-            parsed_response <- httr::content(serverResponse, "text")
+            parsedServerResponse <- httr::content(serverResponse, "text")
+            parsedServerResponse
         }
     ),
 
@@ -90,7 +77,21 @@ ArvadosFile <- R6::R6Class(
         api          = NULL,
         collection   = NULL,
         http         = NULL,
-        httpParser   = NULL
+        httpParser   = NULL,
+
+        notifyCollectionThatFileSizeChanges = function()
+        {
+            collectionURL <- URLencode(paste0(private$api$getWebDavHostName(), "c=", private$collection$uuid))
+            fileURL = paste0(collectionURL, "/", private$relativePath);
+            headers = list("Authorization" = paste("OAuth2", private$api$getToken()))
+
+            propfindResponse <- private$http$PROPFIND(fileURL, headers)
+
+            fileInfo <- private$httpParser$parseWebDAVResponse(propfindResponse, collectionURL)
+
+            private$size <- fileInfo[[1]]$fileSize
+            private$collection$update(self, "File size changed")
+        }
     ),
     
     cloneable = FALSE
index c29f8f005535dc0fefaef2009afbf9cbe84bf8f3..0ebaed8af1852de5dd6132f331f5b6edd2d05197 100644 (file)
@@ -1,6 +1,8 @@
 source("./R/Subcollection.R")
 source("./R/ArvadosFile.R")
 source("./R/FileTree.R")
+source("./R/HttpRequest.R")
+source("./R/HttpParser.R")
 
 #' Arvados Collection Object
 #'
@@ -62,9 +64,12 @@ Collection <- R6::R6Class(
             self$trash_at                 <- result$trash_at                           
             self$is_trashed               <- result$is_trashed                         
 
-            private$fileItems <- private$getCollectionContent()
+            private$http <- HttpRequest$new()
+            private$httpParser <- HttpParser$new()
 
+            private$fileItems <- private$getCollectionContent()
             private$fileTree <- FileTree$new(private$fileItems)
+
         },
 
         printFileContent = function()
@@ -100,6 +105,29 @@ Collection <- R6::R6Class(
             }
         },
 
+        createNewFile = function(relativePath, content, contentType)
+        {
+            node <- private$fileTree$getNode(relativePath)
+
+            if(is.null(node))
+                stop("File already exists")
+
+            fileURL <- paste0(private$api$getWebDavHostName(), "c=", self$uuid, "/", relativePath);
+            headers <- list(Authorization = paste("OAuth2", private$api$getToken()), 
+                            "Content-Type" = contentType)
+            body <- content
+
+            serverResponse <- private$http$PUT(fileURL, headers, body)
+
+            if(serverResponse$status_code != 201)
+                stop(paste("Server code:", serverResponse$status_code))
+
+            fileSize = private$getNewFileSize(relativePath)
+            private$fileTree$addNode(relativePath, fileSize)
+
+            paste0("File created (size = ", fileSize , ")")
+        },
+
         update = function(subcollection, event)
         {
             #Todo(Fudo): Add some king of check here later on.
@@ -125,13 +153,19 @@ Collection <- R6::R6Class(
     
     private = list(
 
-        fileItems = NULL,
-        api       = NULL,
-        fileTree  = NULL,
+        fileItems  = NULL,
+        api        = NULL,
+        fileTree   = NULL,
+        http       = NULL,
+        httpParser = NULL,
 
         handleFileSizeChange = function(filePath, newSize)
         {
             node <- private$fileTree$getNode(filePath)
+
+            if(is.null(node))
+                stop("File doesn't exits")
+
             node$size <- newSize
         },
 
@@ -160,19 +194,28 @@ Collection <- R6::R6Class(
 
         getCollectionContent = function()
         {
-            uri <- URLencode(paste0(private$api$getWebDavHostName(), "c=", self$uuid))
+            collectionURL <- URLencode(paste0(private$api$getWebDavHostName(), "c=", self$uuid))
 
-            # fetch directory listing via curl and parse XML response
-            h <- curl::new_handle()
-            curl::handle_setopt(h, customrequest = "PROPFIND")
+            headers = list("Authorization" = paste("OAuth2", private$api$getToken()))
 
-            curl::handle_setheaders(h, "Authorization" = paste("OAuth2", private$api$getToken()))
-            response <- curl::curl_fetch_memory(uri, h)
+            response <- private$http$PROPFIND(collectionURL, headers)
 
-            parsedResponse <- HttpParser$new()$parseWebDAVResponse(response, uri)
+            parsedResponse <- private$httpParser$parseWebDAVResponse(response, collectionURL)
             parsedResponse[-1]
-        }
+        },
 
+        getNewFileSize = function(relativePath)
+        {
+            collectionURL <- URLencode(paste0(private$api$getWebDavHostName(), "c=", self$uuid))
+            fileURL = paste0(collectionURL, "/", relativePath);
+            headers = list("Authorization" = paste("OAuth2", private$api$getToken()))
+
+            propfindResponse <- private$http$PROPFIND(fileURL, headers)
+
+            fileInfo <- private$httpParser$parseWebDAVResponse(propfindResponse, collectionURL)
+
+            fileInfo[[1]]$fileSize
+        }
     ),
 
     cloneable = FALSE
index ee54bd9febafc0f8fb9e127d761e3d44ae5dcdca..4b21fdad07c01a5a0fd9ca6267b2748f8afc8dbe 100644 (file)
@@ -70,13 +70,23 @@ FileTree <- R6::R6Class(
                 for(pathFragment in splitPath)
                 {
                     child = node$getChild(pathFragment)
+
                     if(is.null(child))
-                        stop("Subcollection/ArvadosFile you are looking for doesn't exist.")
+                        return(NULL)
+
                     node = child
                 }
 
                 node
             })
+        },
+
+        addNode = function(relativePathToNode, size)
+        {
+            splitPath <- unlist(strsplit(relativePathToNode, "/", fixed = TRUE))
+
+            branch <- private$createBranch(splitPath, size)
+            private$addBranch(private$tree, branch)
         }
     ),
 
index 6dd8e4c37720ac90fe26921ae15964ccc0346800..f5c11a176fd16fd1c14e9c365d24dd6aafbc369d 100644 (file)
@@ -8,8 +8,7 @@ HttpRequest <- R6::R6Class(
         {
         },
 
-        GET = function(url, headers = NULL, body = NULL,
-                       queryFilters = NULL, limit = NULL, offset = NULL)
+        GET = function(url, headers = NULL, queryFilters = NULL, limit = NULL, offset = NULL)
         {
             headers <- httr::add_headers(unlist(headers))
             query <- private$createQuery(queryFilters, limit, offset)
@@ -46,6 +45,15 @@ HttpRequest <- R6::R6Class(
             url <- paste0(url, query)
 
             serverResponse <- httr::DELETE(url = url, config = headers)
+        },
+
+        PROPFIND = function(url, headers = NULL)
+        {
+            h <- curl::new_handle()
+            curl::handle_setopt(h, customrequest = "PROPFIND")
+            curl::handle_setheaders(h, .list = headers)
+
+            propfindResponse <- curl::curl_fetch_memory(url, h)
         }
     ),
 
index 1d8c8a77d9a473c0a772e42fc05f812d42b84f2f..4fd2d3c9b7b50c4c27997619c2a2b86155a500d7 100644 (file)
@@ -34,10 +34,10 @@ Subcollection <- R6::R6Class(
 
     private = list(
 
-        name = NULL,
+        name         = NULL,
         relativePath = NULL,
-        children = NULL,
-        parent = NULL
+        children     = NULL,
+        parent       = NULL
     ),
     
     cloneable = FALSE
index 344deab78d09d11e18c46a391246e993c6760a8a..c611c04c2380b93060f18e75e8af2def76905edf 100644 (file)
@@ -73,7 +73,7 @@ arvadosFile <- collection$get("location/to/my/file.cpp")
 
 #or
 
-arvadosSubcollection <- collection.get("location/to/my/directory/")
+arvadosSubcollection <- collection$get("location/to/my/directory/")
 
 --------------------------------------------------------------------------------------------------------------------------------
 
@@ -83,9 +83,29 @@ fileContent <- arvadosFile$read(offset = 1024, length = 512)
 
 --------------------------------------------------------------------------------------------------------------------------------
 
-#Get ArvadosFile or Subcollection size
+#Get ArvadosFile or Subcollection size 
 
 size <- arvadosFile$getSizeInBytes()
 size <- arvadosSubcollection$getSizeInBytes()
 
 --------------------------------------------------------------------------------------------------------------------------------
+
+#Create new file in a collection
+
+#Call structure
+
+collection$createNewFile("relativePath", "fileContent", "fileContentType")
+
+#Example
+
+collection$createNewFile("cpp/src/main.cpp", "#include<iostream>", "text/html")
+
+--------------------------------------------------------------------------------------------------------------------------------
+
+#Write to existing file (Override current content of the file)
+
+arvadosFile <- collection$get("location/to/my/file.cpp")
+
+arvadosFile$write("This is new file content")
+
+--------------------------------------------------------------------------------------------------------------------------------