GigHive bee gighive

Week 1B: TUS Protocol for Cloudflare Compatibility - Implementation Guide

Document Status: Updated October 8, 2025 to reflect current codebase

Overview

This document outlines adding TUS (Tus Resumable Upload) protocol support to the GigHive iOS client for bypassing Cloudflare’s 100MB upload limit.

Important: The original memory issue has been solved with MultipartInputStream (October 2025). This guide now focuses on adding TUS specifically for Cloudflare compatibility.

Current State Analysis (October 2025)

✅ Already Implemented

🎯 New Goal: Cloudflare Compatibility

Cloudflare 100MB Limit: Files >100MB fail with HTTP 413 when uploaded through Cloudflare proxy. TUS protocol will enable chunked uploads (multiple HTTP requests) to bypass this limit.

Current Behavior:

See: CLOUDFLARE_UPLOAD_LIMIT.md for detailed analysis

Week 1 Implementation Tasks

Task 1: Add TUSKit Dependency

File: project.yml Priority: High Changes Required:

packages:
  TUSKit:
    url: https://github.com/tus/TUSKit
    from: 3.0.0

# Update dependencies in targets:
targets:
  GigHive:
    dependencies:
      - package: TUSKit

Task 2: Create TUSUploadClient.swift (NEW FILE)

Location: /Sources/App/TUSUploadClient.swift Size: ~80 lines Priority: High

Purpose: Wrapper around TUSKit to match existing UploadClient interface

Key Features:

Task 3: Enhance UploadClient.swift

File: Sources/App/UploadClient.swift Changes: Add ~30 lines Priority: High

New Method:

func uploadWithTUS(
    _ payload: UploadPayload, 
    progress: ((Int64, Int64) -> Void)? = nil
) async throws -> (status: Int, data: Data, requestURL: URL)

Important Naming:

Logic:

Task 4: Update UploadView.swift

File: Sources/App/UploadView.swift Changes: Minimal - just change line 658 Priority: Medium

// CURRENT:
let (status, data, requestURL) = try await client.uploadWithMultipartInputStream(payload, progress: { ... })

// AFTER TUS IMPLEMENTATION:
let (status, data, requestURL) = try await client.uploadWithTUSIfNeeded(payload, progress: { ... })

Note: The method name was recently changed from uploadWithChunking() to uploadWithMultipartInputStream() to clarify it’s single-request streaming, not TUS protocol.

Implementation Strategy

Phase Approach

  1. Dependency First: Add TUSKit to project.yml and regenerate project
  2. Wrapper Creation: Build TUSUploadClient.swift with basic functionality
  3. Integration: Add chunking logic to existing UploadClient
  4. Testing: Verify both small and large file uploads work

Risk Mitigation

Expected Benefits After TUS Implementation

Immediate Improvements

Already Solved (via MultipartInputStream)

Performance Projections

Integration Points Validation

Existing Flow Compatibility

  1. UploadPayload: ✅ No changes needed - same structure
  2. Progress Callbacks: ✅ Same signature (Int64, Int64) -> Void
  3. Error Handling: ✅ Same async/await pattern
  4. Authentication: ✅ Basic auth preserved
  5. Server Endpoint: ✅ Same /api/uploads.php endpoint

UI Integration

Implementation Checklist

High Priority Tasks

Success Metrics for Week 1

Key Features Implemented

Memory-Efficient Streaming:

Technical Details

File Size Threshold Logic

// Use TUS protocol for files > 100MB (Cloudflare limit)
if fileSize > 100 * 1024 * 1024 {
    // Multiple PATCH requests via TUS protocol
    return try await uploadWithTUS(payload: payload, progress: progress)
} else {
    // Single POST request with MultipartInputStream
    return try await uploadWithMultipartInputStream(payload: payload, progress: progress)
}

Key Differences

Method HTTP Requests Use Case Cloudflare Compatible
uploadWithMultipartInputStream() 1 POST Files <100MB ✅ Yes
uploadWithTUS() Multiple PATCH Files >100MB ✅ Yes
Old upload() method 1 POST (loads to memory) Deprecated ⚠️ <100MB only

TUS Configuration

Ready for Implementation (When Needed)

Current Status: TUS implementation is not urgent because:

When to Implement:

Implementation Readiness:

  1. ✅ Server Ready: Apache configuration already supports 5GB uploads
  2. ✅ Client Architecture: MultipartInputStream provides solid foundation
  3. ✅ Minimal Changes: Only ~110 lines of new code + 1 line change
  4. ⚠️ Server Changes Needed: Must add TUS protocol endpoint (separate from existing /api/uploads.php)
  5. ✅ Backward Compatible: Small files continue using proven MultipartInputStream

Alternative Solution: Instead of TUS, consider creating upload.gighive.app DNS record (DNS-only, bypassing Cloudflare) for simpler implementation. See CLOUDFLARE_UPLOAD_LIMIT.md for details.


Related Documentation: