-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Convert Storage Task implementation to Swift #9880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
64bcbd3
Checkpoint: integration tests run except error tests
paulb777 7b836be
Delete replaced Objective-C
paulb777 99680ef
delete obj-c snapshot
paulb777 a4e4d88
Fix unit tests with one todo
paulb777 deedc50
mac build fix
paulb777 ed46a0a
Migrate StorageDeleteTask
paulb777 86829d6
Build fixes and unit test todo
paulb777 faabb16
GetMetadataTask
paulb777 fd27940
StorageUpdateMetadataTask
paulb777 a22515e
End to end error implementation
paulb777 714274a
Error implementations, Swift integration tests run
paulb777 5bdf913
Checkpoint after StorageList port - open issue with paged lists
paulb777 35c913f
ListResult fixes - integration tests green
paulb777 c5fa5c5
Port GetDownloadURLTask and delete ObjC Task
paulb777 5ee1d07
Delete more ObjC
paulb777 73abcbd
unit test porting progress
paulb777 0153244
test fixes, unit test porting, style
paulb777 0b2e7c0
fixes
paulb777 0116249
Rest of Task unit tests
paulb777 30a86b6
TODO cleanup
paulb777 5083975
review
paulb777 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)? | ||
paulb777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
121
FirebaseStorage/Sources/Internal/StorageGetDownloadURLTask.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
93
FirebaseStorage/Sources/Internal/StorageGetMetadataTask.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.