Added unit tests for RESTService and HttpParser classes
[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") 
96             {
97                 return(textConnection(self$read("text")))
98             }
99             else if (rw == "w") 
100             {
101                 private$buffer <- textConnection(NULL, "w")
102
103                 return(private$buffer)
104             }
105         },
106
107         flush = function() 
108         {
109             v <- textConnectionValue(private$buffer)
110             close(private$buffer)
111             self$write(paste(v, collapse='\n'))
112         },
113
114         write = function(content, contentType = "text/html")
115         {
116             if(is.null(private$collection))
117                 stop("ArvadosFile doesn't belong to any collection.")
118
119             REST <- private$collection$getRESTService()
120
121             writeResult <- REST$write(self$getRelativePath(),
122                                       private$collection$uuid,
123                                       content, contentType)
124             writeResult
125         },
126
127         move = function(newLocationInCollection)
128         {
129             if(is.null(private$collection))
130                 stop("ArvadosFile doesn't belong to any collection")
131
132             newLocationInCollection <- trimFromEnd(newLocationInCollection, "/")
133             newParentLocation <- trimFromEnd(newLocationInCollection, private$name)
134
135             newParent <- private$collection$get(newParentLocation)
136
137             if(is.null(newParent))
138             {
139                 stop("Unable to get destination subcollection")
140             }
141
142             childWithSameName <- newParent$get(private$name)
143
144             if(!is.null(childWithSameName))
145                 stop("Destination already contains content with same name.")
146
147             REST <- private$collection$getRESTService()
148             REST$move(self$getRelativePath(),
149                       paste0(newParent$getRelativePath(), "/", self$getName()),
150                       private$collection$uuid)
151
152             private$dettachFromCurrentParent()
153             private$attachToNewParent(newParent)
154
155             "Content moved successfully."
156         }
157     ),
158
159     private = list(
160
161         name       = NULL,
162         size       = NULL,
163         parent     = NULL,
164         collection = NULL,
165         http       = NULL,
166         httpParser = NULL,
167         buffer     = NULL,
168
169         attachToNewParent = function(newParent)
170         {
171             #Note: We temporary set parents collection to NULL. This will ensure that
172             #      add method doesn't post file on REST.
173             parentsCollection <- newParent$getCollection()
174             newParent$setCollection(NULL, setRecursively = FALSE)
175
176             newParent$add(self)
177
178             newParent$setCollection(parentsCollection, setRecursively = FALSE)
179
180             private$parent <- newParent
181         },
182
183         dettachFromCurrentParent = function()
184         {
185             #Note: We temporary set parents collection to NULL. This will ensure that
186             #      remove method doesn't remove this subcollection from REST.
187             parent <- private$parent
188             parentsCollection <- parent$getCollection()
189             parent$setCollection(NULL, setRecursively = FALSE)
190
191             parent$remove(private$name)
192
193             parent$setCollection(parentsCollection, setRecursively = FALSE)
194         }
195     ),
196
197     cloneable = FALSE
198 )