Skip to content

Multipart Chunked Uploads

FynnCloud streams large uploads through a resilient 3-step multipart protocol (MultipartUploadService) that works identically across Local Disk and S3-Compatible Object Storage while enforcing strict per-user quotas, live revocation checks, and atomic file versioning.

Rule Value
Maximum chunk size STORAGE_MAX_CHUNK_SIZE (default 100mb)
Minimum S3 non-final chunk size 5 MiB (S3 protocol requirement)
Maximum parts per upload maxParts = 10_000 (1-indexed: 1..10000)
Cumulative byte cap slack 5% or 1 MiB above claimed size (enforced atomically via Redis Lua on upload:parts:{sessionID})
Session & chunk TTL 24 hours (abandoned sessions cleaned hourly by UploadCleanupJob)
Local chunk storage path {STORAGE_PATH}/{userID}/_system/chunks/{fileID}/{uploadID}/part_{N}

Step 1: Initialize Upload (POST /api/files/upload/init)

Section titled “Step 1: Initialize Upload (POST /api/files/upload/init)”
{
"name": "archive.tar.gz",
"size": 524288000,
"parentId": "<optional-folder-uuid>",
"mimeType": "application/gzip",
"overwriteFileId": "<optional-existing-file-uuid>"
}
  1. Authorization & Quota Reservation: Verifies write permission to parentId (or overwriteFileId / public file_drop or collaborative share) and calculates reserved bytes:
    • New file or overwrite when storageMaxFileVersions > 0 && previousSize > 0 (versioning will snapshot the previous blob): reserves the full size.
    • Overwrite when versioning is disabled (storageMaxFileVersions == 0): reserves only max(0, size - previousSize).
  2. Storage Preparation:
    • S3: Initiates an S3 Multipart Upload (CreateMultipartUpload) and stores the S3 uploadId.
    • Local Disk: Allocates staging directory {STORAGE_PATH}/{userID}/_system/chunks/{fileID}/{uploadID}/.
  3. Signed Session Token: Returns { uploadId, uploadToken, chunkSize, maxParts } where uploadToken is a signed UploadSessionToken JWT bound to the session, user/share, claimed size, and backing grantID / shareLinkID.

Step 2: Stream Chunks (PUT /api/files/upload/:uploadId/part/:partNumber)

Section titled “Step 2: Stream Chunks (PUT /api/files/upload/:uploadId/part/:partNumber)”

Upload parts concurrently (the web client sends up to 3 parallel chunks per file, 3 concurrent files, with 3 retries per chunk):

PUT /api/files/upload/<uploadId>/part/1
Authorization: Bearer <uploadToken>
Content-Type: application/octet-stream
<raw binary chunk bytes>
  1. Live Revocation Check: Even though uploadToken is a signed JWT, every part checks whether the backing OAuth grant (grant:revoked:{grantID}) or public share link has been revoked—aborting immediately if access was removed mid-upload.
  2. Range & Cap Validation: Validates 1 <= partNumber <= 10_000 and per-part byte limit (STORAGE_MAX_CHUNK_SIZE).
  3. Redis Lua Cumulative Byte Guard (upload:parts:{sessionID}, 24h TTL): Atomically tracks per-part sizes and rejects any chunk that would push the cumulative uploaded bytes beyond claimedSize + max(claimedSize * 5%, 1 MiB) (preventing a client from claiming a 1 MB upload at init to bypass quota and then streaming 10 GB of parts).
  4. Streaming Storage Write:
    • S3: Streams directly into UploadPart (STORAGE_S3_TIMEOUT, default 900s) and returns { partNumber, etag }.
    • Local Disk: Writes {STORAGE_PATH}/{userID}/_system/chunks/{fileID}/{uploadID}/part_{partNumber} and returns { partNumber, etag }.

Step 3: Complete Upload (POST /api/files/upload/:uploadId/complete)

Section titled “Step 3: Complete Upload (POST /api/files/upload/:uploadId/complete)”
{
"parts": [
{ "partNumber": 1, "etag": "\"abc123...\"" },
{ "partNumber": 2, "etag": "\"def456...\"" }
]
}

To upload a 0-byte file (e.g., an empty .txt or .keep file), call init with "size": 0 and call complete immediately with "parts": []—no PUT .../part/N requests are required.

  1. Versioning Snapshot: If overwriting an existing non-empty file and storageMaxFileVersions > 0, snapshots the existing blob into file_versions and prunes versions above the configured cap.
  2. Assembly:
    • S3: Calls CompleteMultipartUpload.
    • Local Disk: Concatenates part_1..part_N sequentially into {STORAGE_PATH}/{userID}/{fileID}, computes the SHA-256 hash, and cleans up _system/chunks/{fileID}/{uploadID}/.
  3. Database, Quota & Events: Commits FileMetadata, releases the Redis quota:pending:{userID} while incrementing users.current_storage_usage, records a FileActivity + SyncLog entry, schedules a debounced File Provider PushKit wakeup (1.5s), emits real-time SSE sync events, and enqueues GenerateThumbnailJob + ProcessFileEmbeddingJob.

Aborting an Upload (DELETE /api/files/upload/:uploadId)

Section titled “Aborting an Upload (DELETE /api/files/upload/:uploadId)”

Clients cancelling a transfer call DELETE /api/files/upload/:uploadId to immediately abort the S3 multipart upload or delete local chunks, release the Redis quota:pending:{userID}, and clear upload:parts:{sessionID}. Uncancelled abandoned sessions are swept automatically by the hourly UploadCleanupJob.