ArvadosFile's connection method now returns curl connection instead of
[arvados.git] / sdk / R / R / ArvadosFile.R
1 source("./R/util.R")
2
3 #' ArvadosFile Object
4 #'
5 #' Update description
6 #'
7 #' @export ArvadosFile
8 ArvadosFile <- R6::R6Class(
9
10     "ArvadosFile",
11
12     public = list(
13
14         initialize = function(name)
15         {
16             private$name             <- name
17             private$http             <- HttpRequest$new()
18             private$httpParser       <- HttpParser$new()
19         },
20
21         getName = function() private$name,
22
23         getFileListing = function(fullpath = TRUE)
24         {
25             self$getName()
26         },
27
28         getSizeInBytes = function()
29         {
30             if(is.null(private$collection))
31                 return(0)
32
33             REST <- private$collection$getRESTService()
34
35             fileSize <- REST$getResourceSize(self$getRelativePath(),
36                                              private$collection$uuid)
37
38             fileSize
39         },
40
41         get = function(fileLikeObjectName)
42         {
43             return(NULL)
44         },
45
46         getFirst = function()
47         {
48             return(NULL)
49         },
50
51         getCollection = function() private$collection,
52
53         setCollection = function(collection)
54         {
55             private$collection <- collection
56         },
57
58         getRelativePath = function()
59         {
60             relativePath <- c(private$name)
61             parent <- private$parent
62
63             while(!is.null(parent))
64             {
65                 relativePath <- c(parent$getName(), relativePath)
66                 parent <- parent$getParent()
67             }
68
69             relativePath <- relativePath[relativePath != ""]
70             paste0(relativePath, collapse = "/")
71         },
72
73         getParent = function() private$parent,
74
75         setParent = function(newParent) private$parent <- newParent,
76
77         read = function(contentType = "raw", offset = 0, length = 0)
78         {
79             if(is.null(private$collection))
80                 stop("ArvadosFile doesn't belong to any collection.")
81
82             if(offset < 0 || length < 0)
83                 stop("Offset and length must be positive values.")
84
85             REST <- private$collection$getRESTService()
86
87             fileContent <- REST$read(self$getRelativePath(),
88                                      private$collection$uuid,
89                                      contentType, offset, length)
90             fileContent
91         },
92
93         connection = function(rw)
94         {
95             if (rw == "r" || rw == "rb") 
96             {
97                 REST <- private$collection$getRESTService()
98                 return(REST$getConnection(private$collection$uuid,
99                                           self$getRelativePath(),
100                                           rw))
101             }
102             else if (rw == "w") 
103             {
104                 private$buffer <- textConnection(NULL, "w")
105
106                 return(private$buffer)
107             }
108         },
109
110         flush = function() 
111         {
112             v <- textConnectionValue(private$buffer)
113             close(private$buffer)
114             self$write(paste(v, collapse='\n'))
115         },
116
117         write = function(content, contentType = "text/html")
118         {
119             if(is.null(private$collection))
120                 stop("ArvadosFile doesn't belong to any collection.")
121
122             REST <- private$collection$getRESTService()
123
124             writeResult <- REST$write(self$getRelativePath(),
125                                       private$collection$uuid,
126                                       content, contentType)
127             writeResult
128         },
129
130         move = function(newLocation)
131         {
132             if(is.null(private$collection))
133                 stop("ArvadosFile doesn't belong to any collection")
134
135
136             newLocation <- trimFromEnd(newLocation, "/")
137             nameAndPath <- splitToPathAndName(newLocation)
138
139             newParent <- private$collection$get(nameAndPath$path)
140
141             if(is.null(newParent))
142             {
143                 stop("Unable to get destination subcollection")
144             }
145
146             childWithSameName <- newParent$get(nameAndPath$name)
147
148             if(!is.null(childWithSameName))
149                 stop("Destination already contains content with same name.")
150
151             REST <- private$collection$getRESTService()
152             REST$move(self$getRelativePath(),
153                       paste0(newParent$getRelativePath(), "/", nameAndPath$name),
154                       private$collection$uuid)
155
156             private$dettachFromCurrentParent()
157             private$attachToNewParent(newParent)
158
159             private$name <- nameAndPath$name
160
161             "Content moved successfully."
162         }
163     ),
164
165     private = list(
166
167         name       = NULL,
168         size       = NULL,
169         parent     = NULL,
170         collection = NULL,
171         http       = NULL,
172         httpParser = NULL,
173         buffer     = NULL,
174
175         attachToNewParent = function(newParent)
176         {
177             #Note: We temporary set parents collection to NULL. This will ensure that
178             #      add method doesn't post file on REST.
179             parentsCollection <- newParent$getCollection()
180             newParent$setCollection(NULL, setRecursively = FALSE)
181
182             newParent$add(self)
183
184             newParent$setCollection(parentsCollection, setRecursively = FALSE)
185
186             private$parent <- newParent
187         },
188
189         dettachFromCurrentParent = function()
190         {
191             #Note: We temporary set parents collection to NULL. This will ensure that
192             #      remove method doesn't remove this subcollection from REST.
193             parent <- private$parent
194             parentsCollection <- parent$getCollection()
195             parent$setCollection(NULL, setRecursively = FALSE)
196
197             parent$remove(private$name)
198
199             parent$setCollection(parentsCollection, setRecursively = FALSE)
200         }
201     ),
202
203     cloneable = FALSE
204 )