c5fa1832565cb7ef3564a2d4f256df89f0bcffb0
[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)
13         {
14             private$name             <- name
15             private$http             <- HttpRequest$new()
16             private$httpParser       <- HttpParser$new()
17         },
18
19         getName = function() private$name,
20
21         getFileList = function(fullpath = TRUE)
22         {
23             self$getName()
24         },
25
26         getSizeInBytes = function()
27         {
28             collectionURL <- URLencode(paste0(private$collection$api$getWebDavHostName(),
29                                               "c=", private$collection$uuid))
30             fileURL <- paste0(collectionURL, "/", self$getRelativePath());
31
32             headers = list("Authorization" = paste("OAuth2", private$collection$api$getToken()))
33
34             propfindResponse <- private$http$PROPFIND(fileURL, headers)
35
36             sizes <- private$httpParser$extractFileSizeFromWebDAVResponse(propfindResponse, collectionURL)
37             as.numeric(sizes)
38         },
39
40         removeFromCollection = function()
41         {
42             if(is.null(private$collection))
43                 stop("ArvadosFile doesn't belong to any collection.")
44             
45             private$collection$.__enclos_env__$private$deleteFromREST(self$getRelativePath())
46
47             private$addToCollection(NULL)
48             private$detachFromParent()
49
50             "Content removed successfully."
51         },
52
53         getRelativePath = function()
54         {
55             relativePath <- c(private$name)
56             parent <- private$parent
57
58             while(!is.null(parent))
59             {
60                 relativePath <- c(parent$getName(), relativePath)
61                 parent <- parent$getParent()
62             }
63
64             relativePath <- relativePath[relativePath != ""]
65             paste0(relativePath, collapse = "/")
66         },
67
68         getParent = function() private$parent,
69
70         read = function(contentType = "raw", offset = 0, length = 0)
71         {
72             if(is.null(private$collection))
73                 stop("ArvadosFile doesn't belong to any collection.")
74
75             if(offset < 0 || length < 0)
76                 stop("Offset and length must be positive values.")
77
78             if(!(contentType %in% private$http$validContentTypes))
79                 stop("Invalid contentType. Please use text or raw.")
80
81             range = paste0("bytes=", offset, "-")
82
83             if(length > 0)
84                 range = paste0(range, offset + length - 1)
85             
86             fileURL = paste0(private$collection$api$getWebDavHostName(),
87                              "c=", private$collection$uuid, "/", self$getRelativePath());
88
89             if(offset == 0 && length == 0)
90             {
91                 headers <- list(Authorization = paste("OAuth2",
92                                                       private$collection$api$getToken())) 
93             }
94             else
95             {
96                 headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()), 
97                                 Range = range)
98             }
99
100             serverResponse <- private$http$GET(fileURL, headers)
101
102             if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
103                 stop(paste("Server code:", serverResponse$status_code))
104
105             parsedServerResponse <- httr::content(serverResponse, contentType)
106             parsedServerResponse
107         },
108         
109         write = function(content, contentType = "text/html")
110         {
111             if(is.null(private$collection))
112                 stop("ArvadosFile doesn't belong to any collection.")
113
114             fileURL = paste0(private$collection$api$getWebDavHostName(), 
115                              "c=", private$collection$uuid, "/", self$getRelativePath());
116             headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()), 
117                             "Content-Type" = contentType)
118             body <- content
119
120             serverResponse <- private$http$PUT(fileURL, headers, body)
121
122             if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
123                 stop(paste("Server code:", serverResponse$status_code))
124
125             parsedServerResponse <- httr::content(serverResponse, "text")
126             parsedServerResponse
127         },
128
129         move = function(newLocation)
130         {
131             if(is.null(private$collection))
132                 stop("ArvadosFile doesn't belong to any collection.")
133
134             if(endsWith(newLocation, paste0(private$name, "/")))
135             {
136                 newLocation <- substr(newLocation, 0,
137                                       nchar(newLocation) - nchar(paste0(private$name, "/")))
138             }
139             else if(endsWith(newLocation, private$name))
140             {
141                 newLocation <- substr(newLocation, 0, nchar(newLocation) - nchar(private$name))
142             }
143             else
144             {
145                 stop("Destination path is not valid.")
146             }
147
148             newParent <- private$collection$get(newLocation)
149
150             if(is.null(newParent))
151             {
152                 stop("Unable to get destination subcollection.")
153             }
154
155             status <- private$collection$.__enclos_env__$private$moveOnREST(self$getRelativePath(),
156                                                                             paste0(newParent$getRelativePath(), "/", self$getName()))
157
158             private$attachToParent(newParent)
159
160             "Content moved successfully."
161         }
162     ),
163
164     private = list(
165
166         name       = NULL,
167         size       = NULL,
168         parent     = NULL,
169         collection = NULL,
170         http       = NULL,
171         httpParser = NULL,
172
173         getChild = function(name)
174         {
175             return(NULL)
176         },
177
178         getFirstChild = function()
179         {
180             return(NULL)
181         },
182
183         addToCollection = function(collection)
184         {
185             private$collection <- collection
186         },
187
188         detachFromParent = function()
189         {
190             if(!is.null(private$parent))
191             {
192                 private$parent$.__enclos_env__$private$removeChild(private$name)
193                 private$parent <- NULL
194             }
195         },
196
197         attachToParent = function(parent)
198         {
199             parent$.__enclos_env__$private$children <- c(parent$.__enclos_env__$private$children, self)
200             private$parent <- parent
201         }
202     ),
203     
204     cloneable = FALSE
205 )