|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import FirebaseStorage |
| 16 | + |
| 17 | +/// getResultCallback generates a closure that returns a Result type from a closure that returns an |
| 18 | +/// optional type and Error. |
| 19 | +private func getResultCallback<T>( |
| 20 | + completion: @escaping (Result<T, Error>) -> Void |
| 21 | +) -> (_: T?, _: Error?) -> Void { |
| 22 | + return { (value: T?, error: Error?) -> Void in |
| 23 | + if let value = value { |
| 24 | + completion(.success(value)) |
| 25 | + } else if let error = error { |
| 26 | + completion(.failure(error)) |
| 27 | + } else { |
| 28 | + completion(.failure(NSError(domain: "FirebaseStorageSwift", |
| 29 | + code: -1, |
| 30 | + userInfo: [NSLocalizedDescriptionKey: |
| 31 | + "InternalError - Return type and Error code both nil in " + |
| 32 | + "Storage Result generator"]))) |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +public extension StorageReference { |
| 38 | + func downloadURL(completion: @escaping (Result<URL, Error>) -> Void) { |
| 39 | + downloadURL(completion: getResultCallback(completion: completion)) |
| 40 | + } |
| 41 | + |
| 42 | + func getData(maxSize: Int64, completion: @escaping (Result<Data, Error>) -> Void) |
| 43 | + -> StorageDownloadTask { |
| 44 | + return getData(maxSize: maxSize, completion: getResultCallback(completion: completion)) |
| 45 | + } |
| 46 | + |
| 47 | + func getMetadata(completion: @escaping (Result<StorageMetadata, Error>) -> Void) { |
| 48 | + getMetadata(completion: getResultCallback(completion: completion)) |
| 49 | + } |
| 50 | + |
| 51 | + func list(withMaxResults maxResults: Int64, |
| 52 | + pageToken: String, |
| 53 | + completion: @escaping (Result<StorageListResult, Error>) -> Void) { |
| 54 | + list(withMaxResults: maxResults, |
| 55 | + pageToken: pageToken, |
| 56 | + completion: getResultCallback(completion: completion)) |
| 57 | + } |
| 58 | + |
| 59 | + func list(withMaxResults maxResults: Int64, |
| 60 | + completion: @escaping (Result<StorageListResult, Error>) -> Void) { |
| 61 | + list(withMaxResults: maxResults, |
| 62 | + completion: getResultCallback(completion: completion)) |
| 63 | + } |
| 64 | + |
| 65 | + func listAll(completion: @escaping (Result<StorageListResult, Error>) -> Void) { |
| 66 | + listAll(completion: getResultCallback(completion: completion)) |
| 67 | + } |
| 68 | + |
| 69 | + func putData(_ uploadData: Data, |
| 70 | + metadata: StorageMetadata? = nil, |
| 71 | + completion: @escaping (Result<StorageMetadata, Error>) -> Void) |
| 72 | + -> StorageUploadTask { |
| 73 | + return putData(uploadData, |
| 74 | + metadata: metadata, |
| 75 | + completion: getResultCallback(completion: completion)) |
| 76 | + } |
| 77 | + |
| 78 | + func putFile(from: URL, |
| 79 | + metadata: StorageMetadata? = nil, |
| 80 | + completion: @escaping (Result<StorageMetadata, Error>) -> Void) |
| 81 | + -> StorageUploadTask { |
| 82 | + return putFile(from: from, |
| 83 | + metadata: metadata, |
| 84 | + completion: getResultCallback(completion: completion)) |
| 85 | + } |
| 86 | + |
| 87 | + func updateMetadata(_ metadata: StorageMetadata, |
| 88 | + completion: @escaping (Result<StorageMetadata, Error>) -> Void) { |
| 89 | + return updateMetadata(metadata, completion: getResultCallback(completion: completion)) |
| 90 | + } |
| 91 | + |
| 92 | + func write(toFile: URL, completion: @escaping (Result<URL, Error>) |
| 93 | + -> Void) -> StorageDownloadTask { |
| 94 | + return write(toFile: toFile, completion: getResultCallback(completion: completion)) |
| 95 | + } |
| 96 | +} |
0 commit comments