StorageReferenceV2.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2022 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. import Foundation
  15. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  16. public extension StorageReference {
  17. /**
  18. * Asynchronously uploads data to the currently specified `StorageReference`.
  19. * This is not recommended for large files, and one should instead upload a file from disk.
  20. * - Parameters:
  21. * - uploadData: The data to upload.
  22. * - metadata: `StorageMetadata` containing additional information (MIME type, etc.)
  23. * about the object being uploaded.
  24. * - Returns: A `StorageMetadata` on success.
  25. * - Throws: A StorageError on failure
  26. */
  27. @discardableResult
  28. func putDataV2(_ uploadData: Data,
  29. metadata: StorageMetadata? = nil) async throws -> StorageMetadata {
  30. let putMetadata = metadata ?? StorageMetadata()
  31. if let path = path.object {
  32. putMetadata.path = path
  33. putMetadata.name = (path as NSString).lastPathComponent as String
  34. }
  35. let fetcherService = storage.fetcherServiceForApp
  36. let task = StorageUploadTaskV2(reference: self,
  37. service: fetcherService,
  38. queue: storage.dispatchQueue,
  39. data: uploadData,
  40. metadata: putMetadata)
  41. return try await task.upload()
  42. }
  43. /**
  44. * Asynchronously uploads a file to the currently specified `StorageReference`.
  45. * - Parameters:
  46. * - fileURL: A URL representing the system file path of the object to be uploaded.
  47. * - metadata: `StorageMetadata` containing additional information (MIME type, etc.)
  48. * about the object being uploaded.
  49. * - progress: Pass a `Progress` handle in for `pause`, `resume`, and `cancel` run control.
  50. * - progressBlock: A block to execute to do progress updates.
  51. * - Returns: A `StorageMetadata` on success.
  52. * - Throws: A StorageError on failure
  53. */
  54. @discardableResult
  55. func putFileV2(from fileURL: URL,
  56. metadata: StorageMetadata? = nil,
  57. progress: Progress? = nil,
  58. progressBlock: ((Progress) -> Void)? = nil) async throws -> StorageMetadata {
  59. var putMetadata: StorageMetadata
  60. if metadata == nil {
  61. putMetadata = StorageMetadata()
  62. if let path = path.object {
  63. putMetadata.path = path
  64. putMetadata.name = (path as NSString).lastPathComponent as String
  65. }
  66. } else {
  67. putMetadata = metadata!
  68. }
  69. let fetcherService = storage.fetcherServiceForApp
  70. let uploadTask = StorageUploadTaskV2(reference: self,
  71. service: fetcherService,
  72. queue: storage.dispatchQueue,
  73. file: fileURL,
  74. metadata: putMetadata,
  75. progress: progress,
  76. progressBlock: progressBlock)
  77. return try await uploadTask.upload()
  78. }
  79. }