Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion FirebaseStorage.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Firebase Storage provides robust, secure file uploads and downloads from Firebas
s.prefix_header_file = false

s.source_files = [
'FirebaseStorage/Sources/*.swift',
'FirebaseStorage/Sources/**/*.swift',
'FirebaseStorage/Typedefs/*.h',
]

Expand All @@ -42,6 +42,7 @@ Firebase Storage provides robust, secure file uploads and downloads from Firebas
s.dependency 'FirebaseAuthInterop', '~> 9.0'
s.dependency 'FirebaseCore', '~> 9.0'
s.dependency 'FirebaseCoreExtension', '~> 9.0'
s.dependency 'GTMSessionFetcher/Core', '>= 1.7', '< 3.0'

s.test_spec 'ObjCIntegration' do |objc_tests|
objc_tests.scheme = { :code_coverage => true }
Expand Down
81 changes: 81 additions & 0 deletions FirebaseStorage/Sources/Internal/StorageDeleteTask.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

import FirebaseStorageInternal
#if COCOAPODS
import GTMSessionFetcher
#else
import GTMSessionFetcherCore
#endif

/**
* Task which provides the ability to delete an object in Firebase Storage.
*/
internal class StorageDeleteTask: StorageTask, StorageTaskManagement {
private var fetcher: GTMSessionFetcher?
private var fetcherCompletion: ((Data?, NSError?) -> Void)?
private var taskCompletion: ((_ error: Error?) -> Void)?

internal init(reference: FIRIMPLStorageReference,
fetcherService: GTMSessionFetcherService,
queue: DispatchQueue,
completion: ((_: Error?) -> Void)?) {
super.init(reference: reference, service: fetcherService, queue: queue)
taskCompletion = completion
}

deinit {
self.fetcher?.stopFetching()
}

/**
* Prepares a task and begins execution.
*/
internal func enqueue() {
weak var weakSelf = self
DispatchQueue.global(qos: .background).async {
guard let strongSelf = weakSelf else { return }
strongSelf.state = .queueing
var request = strongSelf.baseRequest
request.httpMethod = "DELETE"
request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime

let callback = strongSelf.taskCompletion
strongSelf.taskCompletion = nil

let fetcher = strongSelf.fetcherService.fetcher(with: request)
fetcher.comment = "DeleteTask"
strongSelf.fetcher = fetcher

strongSelf.fetcherCompletion = { (data: Data?, error: NSError?) in
if let error = error, self.error == nil {
self.error = StorageErrorCode.error(withServerError: error, ref: strongSelf.reference)
}
if let callback = callback {
callback(self.error)
}
self.fetcherCompletion = nil
}

strongSelf.fetcher?.beginFetch { data, error in
let strongSelf = weakSelf
if let fetcherCompletion = strongSelf?.fetcherCompletion {
fetcherCompletion(data, error as? NSError)
}
}
}
}
}
121 changes: 121 additions & 0 deletions FirebaseStorage/Sources/Internal/StorageGetDownloadURLTask.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

import FirebaseStorageInternal
#if COCOAPODS
import GTMSessionFetcher
#else
import GTMSessionFetcherCore
#endif

/**
* Task which provides the ability to get a download URL for an object in Firebase Storage.
*/
internal class StorageGetDownloadURLTask: StorageTask, StorageTaskManagement {
private var fetcher: GTMSessionFetcher?
private var fetcherCompletion: ((Data?, NSError?) -> Void)?
private var taskCompletion: ((_ downloadURL: URL?, _: Error?) -> Void)?

internal init(reference: FIRIMPLStorageReference,
fetcherService: GTMSessionFetcherService,
queue: DispatchQueue,
completion: ((_: URL?, _: Error?) -> Void)?) {
super.init(reference: reference, service: fetcherService, queue: queue)
taskCompletion = completion
}

deinit {
self.fetcher?.stopFetching()
}

/**
* Prepares a task and begins execution.
*/
internal func enqueue() {
weak var weakSelf = self
DispatchQueue.global(qos: .background).async {
guard let strongSelf = weakSelf else { return }
var request = strongSelf.baseRequest
request.httpMethod = "GET"
request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime

let callback = strongSelf.taskCompletion
strongSelf.taskCompletion = nil

let fetcher = strongSelf.fetcherService.fetcher(with: request)
fetcher.comment = "GetDownloadURLTask"
strongSelf.fetcher = fetcher

strongSelf.fetcherCompletion = { (data: Data?, error: NSError?) in
var downloadURL: URL?
if let error = error {
if self.error == nil {
self.error = StorageErrorCode.error(withServerError: error, ref: self.reference)
}
} else {
if let data = data,
let responseDictionary = try? JSONSerialization
.jsonObject(with: data) as? [String: String] {
downloadURL = strongSelf.downloadURLFromMetadataDictionary(responseDictionary)
if downloadURL == nil {
self.error = NSError(domain: StorageErrorDomain,
code: StorageErrorCode.unknown.rawValue,
userInfo: [NSLocalizedDescriptionKey:
"Failed to retrieve a download URL."])
}
} else {
self.error = StorageErrorCode.error(withInvalidRequest: data)
}
}
if let callback = callback {
callback(downloadURL, self.error)
}
self.fetcherCompletion = nil
}

strongSelf.fetcher?.beginFetch { data, error in
let strongSelf = weakSelf
if let fetcherCompletion = strongSelf?.fetcherCompletion {
fetcherCompletion(data, error as? NSError)
}
}
}
}

internal func downloadURLFromMetadataDictionary(_ dictionary: [String: String]) -> URL? {
let downloadTokens = dictionary["downloadTokens"]
guard let downloadTokens = downloadTokens,
downloadTokens.count > 0 else {
return nil
}
let downloadTokenArray = downloadTokens.components(separatedBy: ",")
let bucket = dictionary["bucket"] ?? "<error: missing bucket>"
let path = dictionary["name"] ?? "<error: missing path name>"
let fullPath = "/v0/b/\(bucket)/o/\(StorageUtils.GCSEscapedString(path))"
var components = URLComponents()
components.scheme = reference.storage.scheme
components.host = reference.storage.host
components.port = reference.storage.port
components.percentEncodedPath = fullPath

// The backend can return an arbitrary number of download tokens, but we only expose the first
// token via the download URL.
let altItem = URLQueryItem(name: "alt", value: "media")
let tokenItem = URLQueryItem(name: "token", value: downloadTokenArray[0])
components.queryItems = [altItem, tokenItem]
return components.url
}
}
93 changes: 93 additions & 0 deletions FirebaseStorage/Sources/Internal/StorageGetMetadataTask.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

import FirebaseStorageInternal
#if COCOAPODS
import GTMSessionFetcher
#else
import GTMSessionFetcherCore
#endif

/**
* Task which provides the ability to delete an object in Firebase Storage.
*/
internal class StorageGetMetadataTask: StorageTask, StorageTaskManagement {
private var fetcher: GTMSessionFetcher?
private var fetcherCompletion: ((Data?, NSError?) -> Void)?
private var taskCompletion: ((_ metadata: StorageMetadata?, _: Error?) -> Void)?

internal init(reference: FIRIMPLStorageReference,
fetcherService: GTMSessionFetcherService,
queue: DispatchQueue,
completion: ((_: StorageMetadata?, _: Error?) -> Void)?) {
super.init(reference: reference, service: fetcherService, queue: queue)
taskCompletion = completion
}

deinit {
self.fetcher?.stopFetching()
}

/**
* Prepares a task and begins execution.
*/
internal func enqueue() {
weak var weakSelf = self
DispatchQueue.global(qos: .background).async {
guard let strongSelf = weakSelf else { return }
strongSelf.state = .queueing
var request = strongSelf.baseRequest
request.httpMethod = "GET"
request.timeoutInterval = strongSelf.reference.storage.maxOperationRetryTime

let callback = strongSelf.taskCompletion
strongSelf.taskCompletion = nil

let fetcher = strongSelf.fetcherService.fetcher(with: request)
fetcher.comment = "GetMetadataTask"
strongSelf.fetcher = fetcher

strongSelf.fetcherCompletion = { (data: Data?, error: NSError?) in
var metadata: StorageMetadata?
if let error = error {
if self.error == nil {
self.error = StorageErrorCode.error(withServerError: error, ref: self.reference)
}
} else {
if let data = data,
let responseDictionary = try? JSONSerialization
.jsonObject(with: data) as? [String: Any] {
metadata = StorageMetadata(dictionary: responseDictionary)
metadata?.impl.type = .file
} else {
self.error = StorageErrorCode.error(withInvalidRequest: data)
}
}
if let callback = callback {
callback(metadata, self.error)
}
self.fetcherCompletion = nil
}

strongSelf.fetcher?.beginFetch { data, error in
let strongSelf = weakSelf
if let fetcherCompletion = strongSelf?.fetcherCompletion {
fetcherCompletion(data, error as? NSError)
}
}
}
}
}
Loading