Skip to main content

Overview

The FileUploadTask 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

UUID
required
Unique identifier for this upload task
String
required
Name of the file being uploaded
Int64
required
Size of the file in bytes
URL
required
Local file system URL of the file to upload
Double
default:"0.0"
Upload progress from 0.0 (not started) to 1.0 (completed)
Status
default:".pending"
Current status of the upload task. See Status Enum below
String?
Error message if the upload failed. Set when status is .failed
URL?
Public URL of the uploaded object. Set when status is .completed
String?
Optional 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
Data?
Security-scoped bookmark for a parent folder. Used when uploading folders to maintain access across app launches
Data?
Security-scoped bookmark for the file itself. Used when uploading individual files via file picker to maintain access across app launches
Task<Void, Never>?
Reference to the running Swift concurrency Task. Held so it can be cancelled via the cancel() method

Status Enum

Methods

cancel()

Cancels the upload task if it’s currently running. Behavior:
  • Cancels the Swift concurrency Task
  • Clears the uploadTask reference
  • Sets status to .cancelled

Source Code

UploadTask.swift

Protocols

  • @Observable - SwiftUI observation macro for automatic UI updates
  • Identifiable - Has a unique id property for use in SwiftUI lists

Usage Example

Security-Scoped Bookmarks

The parentFolderBookmark 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
These bookmarks allow the app to access user-selected files across app launches without requiring the user to grant permission again.

Notes

  • This is a reference type (class) rather than a value type (struct) because it needs to be mutated during upload
  • The @Observable macro enables automatic SwiftUI view updates when properties change
  • The uploadTask property 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