Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[arvados.git] / sdk / R / R / ArvadosFile.R
1 #' ArvadosFile Object
2 #'
3 #' Update description
4 #'
5 #' @export ArvadosFile
6 ArvadosFile <- R6::R6Class(
7
8     "ArvadosFile",
9
10     public = list(
11
12         initialize = function(name, relativePath, size, api, collection)
13         {
14             private$name         <- name
15             private$size         <- size
16             private$relativePath <- relativePath
17             private$api          <- api
18             private$collection   <- collection
19             private$http         <- HttpRequest$new()
20             private$httpParser   <- HttpParser$new()
21         },
22
23         getName = function() private$name,
24
25         getRelativePath = function() private$relativePath,
26
27         getSizeInBytes = function() private$size,
28
29         read = function(offset = 0, length = 0)
30         {
31             if(offset < 0 || length < 0)
32             stop("Offset and length must be positive values.")
33
34             range = paste0("bytes=", offset, "-")
35
36             if(length > 0)
37                 range = paste0(range, offset + length - 1)
38             
39             fileURL = paste0(private$api$getWebDavHostName(), "c=", private$collection$uuid, "/", private$relativePath);
40             headers <- list(Authorization = paste("OAuth2", private$api$getToken()), 
41                             Range = range)
42
43             serverResponse <- private$http$GET(fileURL, headers)
44
45             if(serverResponse$status_code != 206)
46                 stop(paste("Server code:", serverResponse$status_code))
47
48             parsedServerResponse <- httr::content(serverResponse, "raw")
49             parsedServerResponse
50         },
51         
52         write = function(content, contentType = "text/html")
53         {
54             fileURL = paste0(private$api$getWebDavHostName(), "c=", private$collection$uuid, "/", private$relativePath);
55             headers <- list(Authorization = paste("OAuth2", private$api$getToken()), 
56                             "Content-Type" = contentType)
57             body <- content
58
59             serverResponse <- private$http$PUT(fileURL, headers, body)
60
61             if(serverResponse$status_code != 201)
62                 stop(paste("Server code:", serverResponse$status_code))
63
64             private$notifyCollectionThatFileSizeChanges()
65
66             parsedServerResponse <- httr::content(serverResponse, "text")
67             parsedServerResponse
68         }
69     ),
70
71     private = list(
72
73         name         = NULL,
74         relativePath = NULL,
75         size         = NULL,
76         parent       = NULL,
77         api          = NULL,
78         collection   = NULL,
79         http         = NULL,
80         httpParser   = NULL,
81
82         notifyCollectionThatFileSizeChanges = function()
83         {
84             collectionURL <- URLencode(paste0(private$api$getWebDavHostName(), "c=", private$collection$uuid))
85             fileURL = paste0(collectionURL, "/", private$relativePath);
86             headers = list("Authorization" = paste("OAuth2", private$api$getToken()))
87
88             propfindResponse <- private$http$PROPFIND(fileURL, headers)
89
90             fileInfo <- private$httpParser$parseWebDAVResponse(propfindResponse, collectionURL)
91
92             private$size <- fileInfo[[1]]$fileSize
93             private$collection$update(self, "File size changed")
94         }
95     ),
96     
97     cloneable = FALSE
98 )