Skip to content

Commit b569842

Browse files
committed
Fix for incorrect resume behavior
1 parent d2e5a34 commit b569842

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

Sources/TUSKit/TUSAPI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public enum TUSAPIError: Error {
1919
public var localizedDescription: String {
2020
switch self {
2121
case .underlyingError(let error):
22-
return error.localizedDescription
22+
return "Underlying error: " + error.localizedDescription
2323
case .couldNotFetchStatus:
2424
return "Could not fetch status from server."
2525
case .couldNotFetchServerInfo:

Sources/TUSKit/TUSClient.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,21 @@ public final class TUSClient {
366366
@discardableResult
367367
public func resume(id: UUID) throws -> Bool {
368368
do {
369-
var uploadMetadata: UploadMetadata?
369+
var metaData: UploadMetadata?
370370
queue.sync {
371-
uploadMetadata = uploads[id]
371+
metaData = uploads[id]
372372
}
373-
guard uploadMetadata != nil else { return false }
374-
guard let metaData = try files.findMetadata(id: id) else {
373+
374+
if metaData == nil {
375+
guard let storedMetadata = try files.findMetadata(id: id) else {
376+
return false
377+
}
378+
379+
metaData = storedMetadata
380+
}
381+
382+
guard let metaData else {
383+
// should never happen...
375384
return false
376385
}
377386

TUSKitExample/TUSKitExample/Helpers/TUSWrapper.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,20 @@ class TUSWrapper: ObservableObject {
4040

4141
@MainActor
4242
func resumeUpload(id: UUID) {
43-
_ = try? client.resume(id: id)
44-
45-
if case let .paused(bytesUploaded, totalBytes) = uploads[id] {
46-
withAnimation {
47-
uploads[id] = .uploading(bytesUploaded: bytesUploaded, totalBytes: totalBytes)
43+
do {
44+
guard try client.resume(id: id) == true else {
45+
print("Upload not resumed; metadata not found")
46+
return
4847
}
48+
49+
if case let .paused(bytesUploaded, totalBytes) = uploads[id] {
50+
withAnimation {
51+
uploads[id] = .uploading(bytesUploaded: bytesUploaded, totalBytes: totalBytes)
52+
}
53+
}
54+
} catch {
55+
print("Could not resume upload with id \(id)")
56+
print(error)
4957
}
5058
}
5159

@@ -99,6 +107,10 @@ extension TUSWrapper: TUSClientDelegate {
99107

100108
func uploadFailed(id: UUID, error: Error, context: [String : String]?, client: TUSClient) {
101109
Task { @MainActor in
110+
// Pausing an upload means we cancel it, so we don't want to show it as failed.
111+
if let tusError = error as? TUSClientError, case .taskCancelled = tusError {
112+
return
113+
}
102114

103115
withAnimation {
104116
uploads[id] = .failed(error: error)

TUSKitExample/TUSKitExample/Screens/Upload/UploadsListView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ extension UploadsListView {
133133
UploadedRowView(key: idx.key, url: url)
134134
case .failed(let error):
135135
FailedRowView(key: idx.key, error: error)
136+
.onAppear {
137+
print(error)
138+
print(error.localizedDescription)
139+
}
136140
}
137141
}
138142
Divider()

0 commit comments

Comments
 (0)