Skip to content

Commit ee5b1f6

Browse files
committed
Added convenience helper to cancel and clear individual upload
1 parent 8651258 commit ee5b1f6

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
@@ -239,6 +239,18 @@ public final class TUSClient {
239239
scheduler.cancelTask(by: id)
240240
}
241241

242+
/// Cancel an upload and remove its cached data so it will not be retried on the next `start()`.
243+
/// - Parameter id: The id of the upload to cancel and delete.
244+
/// - Returns: `true` if a cached upload was found and deleted, `false` otherwise.
245+
/// - Throws: File-related errors if the cache exists but cannot be removed.
246+
@discardableResult
247+
public func cancelAndDelete(id: UUID) throws -> Bool {
248+
scheduler.cancelTask(by: id)
249+
// Wait for the cancel request to be processed before deleting cached data.
250+
scheduler.queue.sync { }
251+
return try removeCacheFor(id: id)
252+
}
253+
242254
/// This will cancel all running uploads and clear the local cache.
243255
/// Expect errors passed to the delegate for canceled tasks.
244256
/// - 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)