Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[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             if(name == "")
17                 stop("Invalid name.")
18
19             private$name <- name
20         },
21
22         getName = function() private$name,
23
24         getFileListing = function(fullpath = TRUE)
25         {
26             self$getName()
27         },
28
29         getSizeInBytes = function()
30         {
31             if(is.null(private$collection))
32                 return(0)
33
34             REST <- private$collection$getRESTService()
35
36             fileSize <- REST$getResourceSize(self$getRelativePath(),
37                                              private$collection$uuid)
38
39             fileSize
40         },
41
42         get = function(fileLikeObjectName)
43         {
44             return(NULL)
45         },
46
47         getFirst = function()
48         {
49             return(NULL)
50         },
51
52         getCollection = function() private$collection,
53
54         setCollection = function(collection)
55         {
56             private$collection <- collection
57         },
58
59         getRelativePath = function()
60         {
61             relativePath <- c(private$name)
62             parent <- private$parent
63
64             while(!is.null(parent))
65             {
66                 relativePath <- c(parent$getName(), relativePath)
67                 parent <- parent$getParent()
68             }
69
70             relativePath <- relativePath[relativePath != ""]
71             paste0(relativePath, collapse = "/")
72         },
73
74         getParent = function() private$parent,
75
76         setParent = function(newParent) private$parent <- newParent,
77
78         read = function(contentType = "raw", offset = 0, length = 0)
79         {
80             if(is.null(private$collection))
81                 stop("ArvadosFile doesn't belong to any collection.")
82
83             if(offset < 0 || length < 0)
84                 stop("Offset and length must be positive values.")
85
86             REST <- private$collection$getRESTService()
87
88             fileContent <- REST$read(self$getRelativePath(),
89                                      private$collection$uuid,
90                                      contentType, offset, length)
91             fileContent
92         },
93
94         connection = function(rw)
95         {
96             if (rw == "r" || rw == "rb") 
97             {
98                 REST <- private$collection$getRESTService()
99                 return(REST$getConnection(private$collection$uuid,
100                                           self$getRelativePath(),
101                                           rw))
102             }
103             else if (rw == "w") 
104             {
105                 private$buffer <- textConnection(NULL, "w")
106
107                 return(private$buffer)
108             }
109         },
110
111         flush = function() 
112         {
113             v <- textConnectionValue(private$buffer)
114             close(private$buffer)
115             self$write(paste(v, collapse='\n'))
116         },
117
118         write = function(content, contentType = "text/html")
119         {
120             if(is.null(private$collection))
121                 stop("ArvadosFile doesn't belong to any collection.")
122
123             REST <- private$collection$getRESTService()
124
125             writeResult <- REST$write(self$getRelativePath(),
126                                       private$collection$uuid,
127                                       content, contentType)
128             writeResult
129         },
130
131         move = function(newLocation)
132         {
133             if(is.null(private$collection))
134                 stop("ArvadosFile doesn't belong to any collection")
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         buffer     = NULL,
172
173         attachToNewParent = function(newParent)
174         {
175             #Note: We temporary set parents collection to NULL. This will ensure that
176             #      add method doesn't post file on REST.
177             parentsCollection <- newParent$getCollection()
178             newParent$setCollection(NULL, setRecursively = FALSE)
179
180             newParent$add(self)
181
182             newParent$setCollection(parentsCollection, setRecursively = FALSE)
183
184             private$parent <- newParent
185         },
186
187         dettachFromCurrentParent = function()
188         {
189             #Note: We temporary set parents collection to NULL. This will ensure that
190             #      remove method doesn't remove this subcollection from REST.
191             parent <- private$parent
192             parentsCollection <- parent$getCollection()
193             parent$setCollection(NULL, setRecursively = FALSE)
194
195             parent$remove(private$name)
196
197             parent$setCollection(parentsCollection, setRecursively = FALSE)
198         }
199     ),
200
201     cloneable = FALSE
202 )
203
204 #' @export print.ArvadosFile
205 print.ArvadosFile = function(arvadosFile)
206 {
207     collection   <- NULL
208     relativePath <- arvadosFile$getRelativePath()
209
210     if(!is.null(arvadosFile$getCollection()))
211     {
212         collection <- arvadosFile$getCollection()$uuid
213         relativePath <- paste0("/", relativePath)
214     }
215
216     cat(paste0("Type:          ", "\"", "ArvadosFile",         "\""), sep = "\n")
217     cat(paste0("Name:          ", "\"", arvadosFile$getName(), "\""), sep = "\n")
218     cat(paste0("Relative path: ", "\"", relativePath,          "\""), sep = "\n")
219     cat(paste0("Collection:    ", "\"", collection,            "\""), sep = "\n")
220 }