Overview
TheFileUploadTask class represents a pending or in-progress upload in the upload queue. It tracks upload progress, status, and provides cancellation support. This is an @Observable class designed for use with SwiftUI’s observation framework.
Properties
Unique identifier for this upload task
Name of the file being uploaded
Size of the file in bytes
Local file system URL of the file to upload
Upload progress from
0.0 (not started) to 1.0 (completed)Current status of the upload task. See Status Enum below
Error message if the upload failed. Set when
status is .failedPublic URL of the uploaded object. Set when
status is .completedOptional custom R2 key for the upload. When set, this is used as the full R2 key instead of generating a random-prefix key. Used for folder-aware uploads from the browser
Security-scoped bookmark for a parent folder. Used when uploading folders to maintain access across app launches
Security-scoped bookmark for the file itself. Used when uploading individual files via file picker to maintain access across app launches
Reference to the running Swift concurrency Task. Held so it can be cancelled via the
cancel() methodStatus Enum
Methods
cancel()
Cancels the upload task if it’s currently running. Behavior:- Cancels the Swift concurrency Task
- Clears the
uploadTaskreference - Sets
statusto.cancelled
Source Code
UploadTask.swift
Protocols
@Observable- SwiftUI observation macro for automatic UI updatesIdentifiable- Has a uniqueidproperty for use in SwiftUI lists
Usage Example
Security-Scoped Bookmarks
TheparentFolderBookmark and fileBookmark properties store security-scoped bookmark data for macOS sandbox compliance:
- parentFolderBookmark: Used when uploading entire folders, maintains access to the parent directory
- fileBookmark: Used when uploading individual files via file picker, maintains access to the specific file
Notes
- This is a reference type (
class) rather than a value type (struct) because it needs to be mutated during upload - The
@Observablemacro enables automatic SwiftUI view updates when properties change - The
uploadTaskproperty stores a reference to the Swift concurrency Task to enable cancellation - Custom upload keys (
uploadKey) are used for maintaining folder structure in browser-based uploads