@@ -44,14 +44,32 @@ import Foundation
44
44
/// - uploadData: The Data to upload.
45
45
/// - metadata: Optional StorageMetadata containing additional information (MIME type, etc.)
46
46
/// about the object being uploaded.
47
+ /// - onProgress: An optional closure function to return a `Progress` instance while the upload proceeds.
47
48
/// - Throws:
48
49
/// - An error if the operation failed, for example if Storage was unreachable.
49
50
/// - Returns: StorageMetadata with additional information about the object being uploaded.
50
51
func putDataAsync( _ uploadData: Data ,
51
- metadata: StorageMetadata ? = nil ) async throws -> StorageMetadata {
52
+ metadata: StorageMetadata ? = nil ,
53
+ onProgress: ( ( Progress ? ) -> Void ) ? = nil ) async throws -> StorageMetadata {
54
+ guard let onProgress else {
55
+ return try await withCheckedThrowingContinuation { continuation in
56
+ self . putData ( uploadData, metadata: metadata) { result in
57
+ continuation. resume ( with: result)
58
+ }
59
+ }
60
+ }
61
+ let uploadTask = putData ( uploadData, metadata: metadata)
52
62
return try await withCheckedThrowingContinuation { continuation in
53
- _ = self . putData ( uploadData, metadata: metadata) { result in
54
- continuation. resume ( with: result)
63
+ uploadTask. observe ( . progress) {
64
+ onProgress ( $0. progress)
65
+ }
66
+ uploadTask. observe ( . success) { _ in
67
+ continuation. resume ( with: . success( uploadTask. metadata!) )
68
+ }
69
+ uploadTask. observe ( . failure) { snapshot in
70
+ continuation. resume ( with: . failure(
71
+ snapshot. error ?? StorageError . internalError ( " Internal Storage Error in putDataAsync " )
72
+ ) )
55
73
}
56
74
}
57
75
}
@@ -63,14 +81,32 @@ import Foundation
63
81
/// - url: A URL representing the system file path of the object to be uploaded.
64
82
/// - metadata: Optional StorageMetadata containing additional information (MIME type, etc.)
65
83
/// about the object being uploaded.
84
+ /// - onProgress: An optional closure function to return a `Progress` instance while the upload proceeds.
66
85
/// - Throws:
67
86
/// - An error if the operation failed, for example if no file was present at the specified `url`.
68
87
/// - Returns: `StorageMetadata` with additional information about the object being uploaded.
69
88
func putFileAsync( from url: URL ,
70
- metadata: StorageMetadata ? = nil ) async throws -> StorageMetadata {
89
+ metadata: StorageMetadata ? = nil ,
90
+ onProgress: ( ( Progress ? ) -> Void ) ? = nil ) async throws -> StorageMetadata {
91
+ guard let onProgress else {
92
+ return try await withCheckedThrowingContinuation { continuation in
93
+ self . putFile ( from: url, metadata: metadata) { result in
94
+ continuation. resume ( with: result)
95
+ }
96
+ }
97
+ }
98
+ let uploadTask = putFile ( from: url, metadata: metadata)
71
99
return try await withCheckedThrowingContinuation { continuation in
72
- _ = self . putFile ( from: url, metadata: metadata) { result in
73
- continuation. resume ( with: result)
100
+ uploadTask. observe ( . progress) {
101
+ onProgress ( $0. progress)
102
+ }
103
+ uploadTask. observe ( . success) { _ in
104
+ continuation. resume ( with: . success( uploadTask. metadata!) )
105
+ }
106
+ uploadTask. observe ( . failure) { snapshot in
107
+ continuation. resume ( with: . failure(
108
+ snapshot. error ?? StorageError . internalError ( " Internal Storage Error in putFileAsync " )
109
+ ) )
74
110
}
75
111
}
76
112
}
@@ -79,14 +115,32 @@ import Foundation
79
115
///
80
116
/// - Parameters:
81
117
/// - fileUrl: A URL representing the system file path of the object to be uploaded.
118
+ /// - onProgress: An optional closure function to return a `Progress` instance while the download proceeds.
82
119
/// - Throws:
83
120
/// - An error if the operation failed, for example if Storage was unreachable
84
121
/// or `fileURL` did not reference a valid path on disk.
85
122
/// - Returns: A `URL` pointing to the file path of the downloaded file.
86
- func writeAsync( toFile fileURL: URL ) async throws -> URL {
123
+ func writeAsync( toFile fileURL: URL ,
124
+ onProgress: ( ( Progress ? ) -> Void ) ? = nil ) async throws -> URL {
125
+ guard let onProgress else {
126
+ return try await withCheckedThrowingContinuation { continuation in
127
+ _ = self . write ( toFile: fileURL) { result in
128
+ continuation. resume ( with: result)
129
+ }
130
+ }
131
+ }
132
+ let downloadTask = write ( toFile: fileURL)
87
133
return try await withCheckedThrowingContinuation { continuation in
88
- _ = self . write ( toFile: fileURL) { result in
89
- continuation. resume ( with: result)
134
+ downloadTask. observe ( . progress) {
135
+ onProgress ( $0. progress)
136
+ }
137
+ downloadTask. observe ( . success) { _ in
138
+ continuation. resume ( with: . success( fileURL) )
139
+ }
140
+ downloadTask. observe ( . failure) { snapshot in
141
+ continuation. resume ( with: . failure(
142
+ snapshot. error ?? StorageError . internalError ( " Internal Storage Error in writeAsync " )
143
+ ) )
90
144
}
91
145
}
92
146
}
0 commit comments