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))
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))
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()),
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
}
),
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
source("./R/Subcollection.R")
source("./R/ArvadosFile.R")
source("./R/FileTree.R")
+source("./R/HttpRequest.R")
+source("./R/HttpParser.R")
#' Arvados Collection Object
#'
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()
}
},
+ 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.
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
},
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
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)
}
),
{
},
- 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)
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)
}
),
private = list(
- name = NULL,
+ name = NULL,
relativePath = NULL,
- children = NULL,
- parent = NULL
+ children = NULL,
+ parent = NULL
),
cloneable = FALSE
#or
-arvadosSubcollection <- collection.get("location/to/my/directory/")
+arvadosSubcollection <- collection$get("location/to/my/directory/")
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
-#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")
+
+--------------------------------------------------------------------------------------------------------------------------------