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