14
14
15
15
import FirebaseStorage
16
16
17
- /// getResultCallback generates a closure that returns a Result type from a closure that returns an
18
- /// optional type and Error.
17
+ /// Generates a closure that returns a `Result` type from a closure that returns an optional type
18
+ /// and `Error`.
19
+ ///
20
+ /// - Parameters:
21
+ /// - completion: A completion block returning a `Result` enum with either a generic object or
22
+ /// an `Error`.
23
+ /// - Returns: A closure parameterized with an optional generic and optional `Error` to match
24
+ /// Objective C APIs.
19
25
private func getResultCallback< T> (
20
26
completion: @escaping ( Result < T , Error > ) -> Void
21
27
) -> ( _: T ? , _: Error ? ) -> Void {
@@ -35,19 +41,56 @@ private func getResultCallback<T>(
35
41
}
36
42
37
43
public extension StorageReference {
44
+ /// Asynchronously retrieves a long lived download URL with a revokable token.
45
+ /// This can be used to share the file with others, but can be revoked by a developer
46
+ /// in the Firebase Console if desired.
47
+ ///
48
+ /// - Parameters:
49
+ /// - completion: A completion block returning a `Result` enum with either a URL or an `Error`.
38
50
func downloadURL( completion: @escaping ( Result < URL , Error > ) -> Void ) {
39
51
downloadURL ( completion: getResultCallback ( completion: completion) )
40
52
}
41
53
54
+ /// Asynchronously downloads the object at the `StorageReference` to a `Data` object.
55
+ /// A `Data` of the provided max size will be allocated, so ensure that the device has enough
56
+ /// memory to complete. For downloading large files, writeToFile may be a better option.
57
+
58
+ /// - Parameters:
59
+ /// - maxSize: The maximum size in bytes to download.
60
+ /// - completion: A completion block returning a `Result` enum with either a `Data` object or
61
+ /// an `Error`.
62
+ ///
63
+ /// - Returns: A StorageDownloadTask that can be used to monitor or manage the download.
42
64
func getData( maxSize: Int64 , completion: @escaping ( Result < Data , Error > ) -> Void )
43
65
-> StorageDownloadTask {
44
66
return getData ( maxSize: maxSize, completion: getResultCallback ( completion: completion) )
45
67
}
46
68
69
+ /// Retrieves metadata associated with an object at the current path.
70
+ ///
71
+ /// - Parameters:
72
+ /// - completion: A completion block which returns a `Result` enum with either the
73
+ /// object metadata or an `Error`.
47
74
func getMetadata( completion: @escaping ( Result < StorageMetadata , Error > ) -> Void ) {
48
75
getMetadata ( completion: getResultCallback ( completion: completion) )
49
76
}
50
77
78
+ /// Resumes a previous `list` call, starting after a pagination token.
79
+ /// Returns the next set of items (files) and prefixes (folders) under this StorageReference.
80
+ ///
81
+ /// "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
82
+ /// paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
83
+ /// filtered.
84
+ ///
85
+ /// Only available for projects using Firebase Rules Version 2.
86
+ ///
87
+ /// - Parameters:
88
+ /// - withMaxResults The maximum number of results to return in a single page. Must be
89
+ /// greater than 0 and at most 1000.
90
+ /// - pageToken A page token from a previous call to list.
91
+ /// - completion A completion handler that will be invoked with the next items and
92
+ /// prefixes under the current StorageReference. It returns a `Result` enum
93
+ /// with either the list or an `Error`.
51
94
func list( withMaxResults maxResults: Int64 ,
52
95
pageToken: String ,
53
96
completion: @escaping ( Result < StorageListResult , Error > ) -> Void ) {
@@ -56,16 +99,54 @@ public extension StorageReference {
56
99
completion: getResultCallback ( completion: completion) )
57
100
}
58
101
102
+ /// List up to `maxResults` items (files) and prefixes (folders) under this StorageReference.
103
+ ///
104
+ /// "/" is treated as a path delimiter. Firebase Storage does not support unsupported object
105
+ /// paths that end with "/" or contain two consecutive "/"s. All invalid objects in GCS will be
106
+ /// filtered.
107
+ ///
108
+ /// Only available for projects using Firebase Rules Version 2.
109
+ ///
110
+ /// - Parameters:
111
+ /// - withMaxResults The maximum number of results to return in a single page. Must be
112
+ /// greater than 0 and at most 1000.
113
+ /// - completion A completion handler that will be invoked with the next items and
114
+ /// prefixes under the current `StorageReference`. It returns a `Result` enum
115
+ /// with either the list or an `Error`.
59
116
func list( withMaxResults maxResults: Int64 ,
60
117
completion: @escaping ( Result < StorageListResult , Error > ) -> Void ) {
61
118
list ( withMaxResults: maxResults,
62
119
completion: getResultCallback ( completion: completion) )
63
120
}
64
121
122
+ /// List all items (files) and prefixes (folders) under this StorageReference.
123
+ ///
124
+ /// This is a helper method for calling list() repeatedly until there are no more results.
125
+ /// Consistency of the result is not guaranteed if objects are inserted or removed while this
126
+ /// operation is executing. All results are buffered in memory.
127
+ ///
128
+ /// Only available for projects using Firebase Rules Version 2.
129
+ ///
130
+ /// - Parameters:
131
+ /// - completion A completion handler that will be invoked with all items and prefixes
132
+ /// under the current StorageReference. It returns a `Result` enum with either the
133
+ /// list or an `Error`.
65
134
func listAll( completion: @escaping ( Result < StorageListResult , Error > ) -> Void ) {
66
135
listAll ( completion: getResultCallback ( completion: completion) )
67
136
}
68
137
138
+ /// Asynchronously uploads data to the currently specified `StorageReference`.
139
+ /// This is not recommended for large files, and one should instead upload a file from disk.
140
+ ///
141
+ /// - Parameters:
142
+ /// - uploadData The `Data` to upload.
143
+ /// - metadata `StorageMetadata` containing additional information (MIME type, etc.)
144
+ /// about the object being uploaded.
145
+ /// - completion A completion block that returns a `Result` enum with either the
146
+ /// object metadata or an `Error`.
147
+ ///
148
+ /// - Returns: An instance of `StorageUploadTask`, which can be used to monitor or manage
149
+ /// the upload.
69
150
func putData( _ uploadData: Data ,
70
151
metadata: StorageMetadata ? = nil ,
71
152
completion: @escaping ( Result < StorageMetadata , Error > ) -> Void )
@@ -75,6 +156,17 @@ public extension StorageReference {
75
156
completion: getResultCallback ( completion: completion) )
76
157
}
77
158
159
+ /// Asynchronously uploads a file to the currently specified `StorageReference`.
160
+ ///
161
+ /// - Parameters:
162
+ /// - from A URL representing the system file path of the object to be uploaded.
163
+ /// - metadata `StorageMetadata` containing additional information (MIME type, etc.)
164
+ /// about the object being uploaded.
165
+ /// - completion A completion block that returns a `Result` enum with either the
166
+ /// object metadata or an `Error`.
167
+ ///
168
+ /// - Returns: An instance of `StorageUploadTask`, which can be used to monitor or manage
169
+ /// the upload.
78
170
func putFile( from: URL ,
79
171
metadata: StorageMetadata ? = nil ,
80
172
completion: @escaping ( Result < StorageMetadata , Error > ) -> Void )
@@ -84,11 +176,26 @@ public extension StorageReference {
84
176
completion: getResultCallback ( completion: completion) )
85
177
}
86
178
179
+ /// Updates the metadata associated with an object at the current path.
180
+ ///
181
+ /// - Parameters:
182
+ /// - metadata A `StorageMetadata` object with the metadata to update.
183
+ /// - completion A completion block which returns a `Result` enum with either the
184
+ /// object metadata or an `Error`.
87
185
func updateMetadata( _ metadata: StorageMetadata ,
88
186
completion: @escaping ( Result < StorageMetadata , Error > ) -> Void ) {
89
187
return updateMetadata ( metadata, completion: getResultCallback ( completion: completion) )
90
188
}
91
189
190
+ /// Asynchronously downloads the object at the current path to a specified system filepath.
191
+ ///
192
+ /// - Parameters:
193
+ /// - toFile A file system URL representing the path the object should be downloaded to.
194
+ /// - completion A completion block that fires when the file download completes. The
195
+ /// block returns a `Result` enum with either an NSURL pointing to the file
196
+ /// path of the downloaded file or an `Error`.
197
+ ///
198
+ /// - Returns: A `StorageDownloadTask` that can be used to monitor or manage the download.
92
199
func write( toFile: URL , completion: @escaping ( Result < URL , Error > )
93
200
-> Void ) -> StorageDownloadTask {
94
201
return write ( toFile: toFile, completion: getResultCallback ( completion: completion) )
0 commit comments