Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/PresetFilename.swift
T
josh dc42db0c59 Add iCloud-synced rig library
Replace the single hand-saved rig file with a managed library of named
rigs persisted in app storage and synced across devices and both builder
apps via a shared iCloud container (iCloud.net.realizedsound.ShoeGazeData),
falling back to local storage when iCloud is unavailable.

Core (LyrebirdShoeGaze/Presets):
- RigEntry: named, timestamped ShoeGazeState (a whole multi-board rig).
- RigArchive: portable multi-rig JSON bundle for backup/transfer; legacy
  single-rig files import as a one-rig archive.
- RigLibraryStore (@MainActor @Observable): one JSON file per rig, with
  NSFileCoordinator writes and NSMetadataQuery live remote-change watching.
- RigLibraryLocation: iCloud-or-local directory resolver (skips the slow
  ubiquity lookup when no iCloud account is signed in).
- ShoeGaze.loadRig(from: ShoeGazeState) + captureEntry; shared filename
  sanitizer factored out of BoardVersionStore.

UI (LyrebirdShoeGazeUI):
- RigLibrarySidebarView: persistent left listing with new/save/save-as/
  duplicate/rename/delete/erase-all and archive import/export.
- RigNamePrompt; RigLibraryBootstrap one-call shell setup.
- ShoeGazeBuilderView: sidebar wired in; selecting a rig replaces the
  session; File menu / sidebar do whole-library archive import/export.

Shells/entitlements: both builder targets own+start the store and carry
the iCloud Documents entitlement; NSUbiquitousContainers exposes the rigs
in the Files app. (macOS pbxproj also carries a pre-existing 1.0->1.1.1
version bump.)

Tests: RigEntry/RigArchive round-trip + RigLibraryStore CRUD/import/export
(15 new; full suite 206 passing).

Provisioning (manual, not done here): enable the iCloud container on both
App IDs and select it in Signing & Capabilities for both targets.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-27 17:56:10 -07:00

66 lines
2.7 KiB
Swift

// PresetFilename.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// Sanitises a user-supplied name so it is safe as a file-system component on HFS+ and APFS.
///
/// Shared by the preset stores (`BoardVersionStore`, `RigLibraryStore`) so every on-disk preset
/// filename follows one scheme: `<sanitized-name>-<uuid>.json`.
///
/// - Replaces `/`, `\`, `:`, NUL (`\0`), and all whitespace with underscores.
/// - Replaces a leading `.` (which would produce a hidden file or `.`/`..`) with an underscore.
/// - Trims leading and trailing underscores that resulted from the substitutions (cosmetic).
/// - Caps the UTF-8 byte length of the result at `maxLength` so the full filename
/// (`<name>-<uuid>.json`, 54 extra bytes) stays within the 255-byte component limit.
/// - Returns `"unnamed"` when the input is empty, all-whitespace, or reduces entirely to
/// separator characters after substitution.
///
/// - Parameters:
/// - name: The raw, user-supplied name.
/// - maxLength: Maximum UTF-8 byte length of the sanitised prefix. Defaults to `200`.
/// - Returns: A file-system-safe name, never empty.
func sanitizePresetFilename(
_ name: String,
maxLength: Int = 200
) -> String {
let replacementCharacter: Character = "_"
let forbiddenCharacters: Set<Character> = ["/", "\\", ":", "\0"]
var sanitized: String = name.map { character -> Character in
if character.isWhitespace || forbiddenCharacters.contains(character) {
return replacementCharacter
}
return character
}.reduce(into: "") { $0.append($1) }
// Replace a leading dot so the filename is never `.`, `..`, or a hidden file.
if sanitized.first == "." {
sanitized.replaceSubrange(sanitized.startIndex...sanitized.startIndex, with: "_")
}
// Trim whitespace-replacement underscores at both ends (cosmetic).
sanitized = sanitized.trimmingCharacters(in: CharacterSet(charactersIn: "_"))
// Treat all-empty or all-separator results as unnamed.
guard !sanitized.isEmpty else { return "unnamed" }
// Cap to maxLength UTF-8 bytes so the full filename stays within 255 bytes.
if sanitized.utf8.count > maxLength {
var truncated: String = ""
var byteCount: Int = 0
for scalar: Unicode.Scalar in sanitized.unicodeScalars {
let encodedLength: Int = UTF8.width(scalar)
guard byteCount + encodedLength <= maxLength else { break }
truncated.unicodeScalars.append(scalar)
byteCount += encodedLength
}
sanitized = truncated
}
return sanitized.isEmpty ? "unnamed" : sanitized
}