Skip to content

Commit c1d36bf

Browse files
committed
Added convenience helper to cancel and clear individual upload
1 parent 4fccfe3 commit c1d36bf

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

Sources/TUSKit/TUSClient.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ public final class TUSClient {
230230
scheduler.cancelTask(by: id)
231231
}
232232

233+
/// Cancel an upload and remove its cached data so it will not be retried on the next `start()`.
234+
/// - Parameter id: The id of the upload to cancel and delete.
235+
/// - Returns: `true` if a cached upload was found and deleted, `false` otherwise.
236+
/// - Throws: File-related errors if the cache exists but cannot be removed.
237+
@discardableResult
238+
public func cancelAndDelete(id: UUID) throws -> Bool {
239+
scheduler.cancelTask(by: id)
240+
// Wait for the cancel request to be processed before deleting cached data.
241+
scheduler.queue.sync { }
242+
return try removeCacheFor(id: id)
243+
}
244+
233245
/// This will cancel all running uploads and clear the local cache.
234246
/// Expect errors passed to the delegate for canceled tasks.
235247
/// - Warning: This method is destructive and will remove any stored cache.

Tests/TUSKitTests/TUSClient/TUSClientInternalTests.swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ final class TUSClientInternalTests: XCTestCase {
2222

2323
do {
2424
relativeStoragePath = URL(string: "TUSTEST")!
25-
26-
let docDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
27-
fullStoragePath = docDir.appendingPathComponent(relativeStoragePath.absoluteString)
28-
files = try Files(storageDirectory: fullStoragePath)
29-
clearDirectory(dir: fullStoragePath)
25+
files = try Files(storageDirectory: relativeStoragePath)
26+
fullStoragePath = files.storageDirectory
27+
clearDirectory(dir: files.storageDirectory)
3028

3129
data = Data("abcdef".utf8)
3230

@@ -124,6 +122,21 @@ final class TUSClientInternalTests: XCTestCase {
124122
}
125123
}
126124

125+
func testCancelAndDeleteRemovesCacheAndPreventsResume() throws {
126+
let clientFiles = try Files(storageDirectory: relativeStoragePath)
127+
let id = UUID()
128+
let path = try clientFiles.store(data: data, id: id)
129+
let metaData = UploadMetadata(id: id, filePath: path, uploadURL: URL(string: "io.tus")!, size: data.count, customHeaders: [:], mimeType: nil)
130+
try clientFiles.encodeAndStore(metaData: metaData)
131+
132+
let deleted = try client.cancelAndDelete(id: id)
133+
XCTAssertTrue(deleted)
134+
XCTAssertNil(try clientFiles.findMetadata(id: id))
135+
136+
let resumed = client.start()
137+
XCTAssertTrue(resumed.isEmpty)
138+
}
139+
127140
func testCancellationDoesNotIncrementErrorCountOrRetry() throws {
128141
let metaData = try storeFiles()
129142
let creationTask = try CreationTask(metaData: metaData,

0 commit comments

Comments
 (0)