~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/hydraulophone

An unofficial YouTube Music app for iPhone, iPad, and Mac.

0
0
main
530 lines · 19.2 KB Raw History
import CryptoKit
import Foundation

/// Identity of immutable encoded bytes. No expiring URL or credential is stored.
public struct TrackAssetIdentity: Codable, Hashable, Sendable {
  public let trackID: String
  public let formatID: Int
  public let version: String
  public let contentLength: Int64

  public init?(trackID: String, formatID: Int?, version: String?, contentLength: Int64?) {
    guard !trackID.isEmpty, trackID.utf8.count <= 256,
      let formatID, formatID > 0, let version, !version.isEmpty, version.utf8.count <= 128,
      let contentLength, contentLength > 0, contentLength <= 2_000_000_000
    else { return nil }
    self.trackID = trackID
    self.formatID = formatID
    self.version = version
    self.contentLength = contentLength
  }

  var key: String {
    let value = "\(trackID.utf8.count):\(trackID):\(formatID):\(version):\(contentLength)"
    return SHA256.hash(data: Data(value.utf8)).map { String(format: "%02x", $0) }.joined()
  }
}

public struct TrackCacheStatistics: Sendable {
  public let bytes: Int64
  public let completeTracks: Int
}

/// Metadata for a fully cached song that can be surfaced while offline.
public struct CachedTrack: Sendable, Identifiable, Equatable {
  public let song: Song
  public let lastAccessed: Date

  public var id: String { song.id }

  public init(song: Song, lastAccessed: Date) {
    self.song = song
    self.lastAccessed = lastAccessed
  }
}

public struct TrackCacheWrite: Sendable {
  let generation: UInt64
  let identity: TrackAssetIdentity
  let bitrate: Int?
  let duration: TimeInterval?
  let loudness: AudioLoudnessMetadata?
  let song: Song?
}

/// An open completed asset survives LRU eviction/clear while it is playing.
/// Closing its last reader releases the unlinked file's storage.
public actor CachedTrackFile {
  public nonisolated let identity: TrackAssetIdentity
  public nonisolated let url: URL
  public nonisolated let bitrate: Int?
  public nonisolated let duration: TimeInterval?
  public nonisolated let loudness: AudioLoudnessMetadata?
  public nonisolated let song: Song?
  private let handle: FileHandle

  fileprivate init(
    url: URL, identity: TrackAssetIdentity, bitrate: Int?, duration: TimeInterval?,
    loudness: AudioLoudnessMetadata?, song: Song?
  )
    throws
  {
    handle = try FileHandle(forReadingFrom: url)
    self.url = url
    self.identity = identity
    self.bitrate = bitrate
    self.duration = duration
    self.loudness = loudness
    self.song = song
  }

  deinit { try? handle.close() }

  public func read(offset: Int64, count: Int) throws -> Data {
    try Task.checkCancellation()
    guard offset >= 0, count >= 0, count <= 4 * 1024 * 1024 else {
      throw CocoaError(.fileReadCorruptFile)
    }
    if offset >= identity.contentLength { return Data() }
    let count = min(count, Int(identity.contentLength - offset))
    try handle.seek(toOffset: UInt64(offset))
    let data = try handle.read(upToCount: count) ?? Data()
    guard data.count == count else { throw CocoaError(.fileReadCorruptFile) }
    return data
  }
}

/// Encoded range storage shared by streaming and explicit downloads.
/// Disk work is serialized off the UI actor. Only fetched ranges are cached;
/// completeness requires every byte. Standard's HLS packages share its budget.
public actor TrackCacheStore {
  public static let chunkSize = 64 * 1024
  private let directory: URL
  var limit: Int64
  var generation: UInt64 = 0
  private var loaded = false
  private var entries: [String: Manifest] = [:]
  var hlsEntries: [String: HLSCacheEntry] = [:]
  var hlsReaders: [String: Int] = [:]
  var hlsPendingRemoval: Set<String> = []

  private struct Manifest: Codable {
    var schema = 1
    let identity: TrackAssetIdentity
    let bitrate: Int?
    let duration: TimeInterval?
    var loudness: AudioLoudnessMetadata? = nil
    var song: Song? = nil
    var chunks: [String: Int] = [:]
    var complete = false
    var accessed = Date()

    var bytes: Int64 {
      complete ? identity.contentLength : chunks.values.reduce(0) { $0 + Int64($1) }
    }
  }

  public init(directory: URL? = nil, limit: Int64 = 1_000_000_000) {
    self.directory =
      directory
      ?? FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
      .appendingPathComponent("HydraulophoneAudio/v1", isDirectory: true)
    self.limit = max(0, limit)
  }

  public func setLimit(_ bytes: Int64) {
    load()
    limit = max(0, bytes)
    if limit == 0 { clear() } else { trim() }
  }

  public func statistics() -> TrackCacheStatistics {
    load()
    return TrackCacheStatistics(
      bytes: entries.values.reduce(0) { $0 + $1.bytes }
        + hlsEntries.values.reduce(0) { $0 + $1.bytes },
      completeTracks: entries.values.filter(\.complete).count
        + hlsEntries.values.filter(\.complete).count)
  }

  public func clear() {
    load()
    generation &+= 1
    for key in Array(entries.keys) { remove(key) }
    for key in Array(hlsEntries.keys) { remove(key) }
  }

  public func invalidate(trackID: String, quality: AudioQualityPreference? = nil) {
    load()
    // Invalidate in-flight writers as well as completed assets.
    generation &+= 1
    if quality != .standard {
      for (key, value) in entries where value.identity.trackID == trackID { remove(key) }
    }
    if quality != .high {
      for (key, value) in hlsEntries where value.trackID == trackID { remove(key) }
    }
  }

  /// Updates metadata on existing cached assets without touching their media
  /// files or invalidating active readers. An empty metadata value is ignored.
  @discardableResult
  public func updateLoudness(_ loudness: AudioLoudnessMetadata, trackID: String) -> Int {
    guard loudness.loudnessDb != nil || loudness.perceptualLoudnessDb != nil else { return 0 }
    load()
    var updatedCount = 0
    for key in Array(entries.keys) {
      guard let current = entries[key], current.identity.trackID == trackID else { continue }
      var entry = current
      entry.loudness = loudness
      do {
        try persist(entry)
        entries[key] = entry
        updatedCount += 1
      } catch {
        // Metadata refresh is best-effort and does not disturb cached bytes.
      }
    }
    for key in Array(hlsEntries.keys) {
      guard let current = hlsEntries[key], current.trackID == trackID else { continue }
      var entry = current
      entry.loudness = loudness
      do {
        try persistHLS(entry)
        hlsEntries[key] = entry
        updatedCount += 1
      } catch {
        // Metadata refresh is best-effort and does not disturb cached packages.
      }
    }
    return updatedCount
  }

  /// Refreshes catalog metadata for existing entries without changing cached audio.
  public func updateMetadata(for song: Song) {
    guard !song.id.isEmpty else { return }
    load()
    for key in Array(entries.keys) {
      guard var entry = entries[key], entry.identity.trackID == song.id else { continue }
      entry.song = metadataSong(song, retainingArtworkFrom: entry.song)
      do {
        try persist(entry)
        entries[key] = entry
      } catch {
        // Metadata refresh is best-effort and does not disturb cached bytes.
      }
    }
    for key in Array(hlsEntries.keys) {
      guard var entry = hlsEntries[key], entry.trackID == song.id else { continue }
      entry.song = metadataSong(song, retainingArtworkFrom: entry.song)
      do {
        try persistHLS(entry)
        hlsEntries[key] = entry
      } catch {
        // Metadata refresh is best-effort and does not disturb cached packages.
      }
    }
  }

  private func metadataSong(_ song: Song, retainingArtworkFrom existing: Song?) -> Song {
    let artworkURL: URL?
    if song.thumbnailURL?.isFileURL == true {
      artworkURL = existing?.thumbnailURL.flatMap { $0.isFileURL ? nil : $0 }
    } else {
      artworkURL = song.thumbnailURL
    }
    return Song(
      id: song.id, title: song.title, artist: song.artist, album: song.album,
      durationText: song.durationText, thumbnailURL: artworkURL,
      artistReferences: song.artistReferences, albumReference: song.albumReference)
  }

  public func writer(
    identity: TrackAssetIdentity, bitrate: Int?, duration: TimeInterval?,
    loudness: AudioLoudnessMetadata? = nil, song: Song? = nil
  )
    -> TrackCacheWrite?
  {
    load()
    guard limit > 0, identity.contentLength <= limit,
      song == nil || song?.id == identity.trackID
    else { return nil }
    return TrackCacheWrite(
      generation: generation, identity: identity, bitrate: bitrate, duration: duration,
      loudness: loudness, song: song)
  }

  /// Returns only complete assets with valid media files and matching track metadata.
  /// Entries from older cache versions remain playable but are omitted until metadata is refreshed.
  public func cachedTracks() -> [CachedTrack] {
    load()
    guard limit > 0 else { return [] }
    var byID: [String: CachedTrack] = [:]
    for (key, entry) in entries where entry.complete {
      guard let song = entry.song, song.id == entry.identity.trackID else { continue }
      let url = folder(key).appendingPathComponent("audio")
      guard
        (try? url.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey, .isSymbolicLinkKey]))
          .map({
            $0.isRegularFile == true && $0.isSymbolicLink != true
              && $0.fileSize == Int(entry.identity.contentLength)
          }) == true
      else {
        remove(key)
        continue
      }
      Self.keepMostRecent(
        CachedTrack(song: song, lastAccessed: entry.accessed), in: &byID)
    }
    for entry in hlsEntries.values where entry.complete {
      guard let song = entry.song, song.id == entry.trackID,
        let url = Self.hlsLocation(bookmark: entry.bookmark),
        FileManager.default.fileExists(atPath: url.path),
        entry.localPackage != true || Self.isValidLocalPackage(url)
      else { continue }
      Self.keepMostRecent(CachedTrack(song: song, lastAccessed: entry.accessed), in: &byID)
    }
    return byID.values.sorted { $0.lastAccessed > $1.lastAccessed }
  }

  private static func keepMostRecent(_ track: CachedTrack, in tracks: inout [String: CachedTrack]) {
    if let existing = tracks[track.id], existing.lastAccessed >= track.lastAccessed { return }
    tracks[track.id] = track
  }

  public func completed(trackID: String) -> CachedTrackFile? {
    load()
    guard limit > 0,
      let entry = entries.values.filter({ $0.complete && $0.identity.trackID == trackID })
        .max(by: { $0.accessed < $1.accessed })
    else { return nil }
    let key = entry.identity.key
    let url = folder(key).appendingPathComponent("audio")
    do {
      guard
        try url.resourceValues(forKeys: [.fileSizeKey]).fileSize
          == Int(entry.identity.contentLength)
      else {
        remove(key)
        return nil
      }
      let reader = try CachedTrackFile(
        url: url, identity: entry.identity, bitrate: entry.bitrate, duration: entry.duration,
        loudness: entry.loudness, song: entry.song)
      var updated = entry
      updated.accessed = Date()
      entries[key] = updated
      try persist(updated)
      return reader
    } catch {
      remove(key)
      return nil
    }
  }

  public func read(_ ticket: TrackCacheWrite, offset: Int64) -> Data? {
    guard ticket.generation == generation, limit > 0,
      var entry = entries[ticket.identity.key], offset >= 0,
      offset < entry.identity.contentLength, offset % Int64(Self.chunkSize) == 0
    else { return nil }
    let expected = min(Self.chunkSize, Int(entry.identity.contentLength - offset))
    do {
      let data: Data
      if entry.complete {
        let handle = try FileHandle(
          forReadingFrom: folder(entry.identity.key).appendingPathComponent("audio"))
        defer { try? handle.close() }
        try handle.seek(toOffset: UInt64(offset))
        data = try handle.read(upToCount: expected) ?? Data()
      } else {
        guard entry.chunks[String(offset)] == expected else { return nil }
        let url = folder(entry.identity.key).appendingPathComponent("\(offset).chunk")
        guard try url.resourceValues(forKeys: [.fileSizeKey]).fileSize == expected else {
          throw CocoaError(.fileReadCorruptFile)
        }
        data = try Data(contentsOf: url)
      }
      guard data.count == expected else { throw CocoaError(.fileReadCorruptFile) }
      entry.accessed = Date()
      entries[entry.identity.key] = entry
      return data
    } catch {
      remove(entry.identity.key)
      return nil
    }
  }

  public func store(_ data: Data, offset: Int64, totalLength: Int64, ticket: TrackCacheWrite) {
    guard ticket.generation == generation, limit > 0,
      totalLength == ticket.identity.contentLength, totalLength <= limit,
      offset >= 0, offset < totalLength, offset % Int64(Self.chunkSize) == 0,
      data.count == min(Self.chunkSize, Int(totalLength - offset))
    else { return }
    let key = ticket.identity.key
    var entry =
      entries[key]
      ?? Manifest(
        identity: ticket.identity, bitrate: ticket.bitrate, duration: ticket.duration,
        loudness: ticket.loudness, song: ticket.song)
    guard !entry.complete else { return }
    entry.loudness = ticket.loudness ?? entry.loudness
    entry.song = ticket.song ?? entry.song
    do {
      try FileManager.default.createDirectory(at: folder(key), withIntermediateDirectories: true)
      try data.write(
        to: folder(key).appendingPathComponent("\(offset).chunk"),
        options: [.atomic, .completeFileProtectionUntilFirstUserAuthentication])
      entry.chunks[String(offset)] = data.count
      entry.accessed = Date()
      // Persist partial progress before finalization, so an interrupted
      // assembly is repaired on next launch without trusting an incomplete file.
      entries[key] = entry
      try persist(entry)
      if entry.bytes == totalLength {
        try assemble(&entry)
        entries[key] = entry
        for (other, value) in entries
        where other != key && value.identity.trackID == entry.identity.trackID { remove(other) }
        for (other, value) in hlsEntries where value.trackID == entry.identity.trackID {
          remove(other)
        }
      }
      trim()
    } catch {
      // Cache/disk failures never turn a successful media read into a failure.
      remove(key)
    }
  }

  private func assemble(_ entry: inout Manifest) throws {
    let directory = folder(entry.identity.key)
    let pending = directory.appendingPathComponent("assembling")
    guard
      FileManager.default.createFile(
        atPath: pending.path, contents: nil,
        attributes: [.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication])
    else {
      throw CocoaError(.fileWriteUnknown)
    }
    let handle = try FileHandle(forWritingTo: pending)
    defer { try? handle.close() }
    for offset in stride(
      from: Int64(0), to: entry.identity.contentLength, by: Self.chunkSize)
    {
      let expected = min(Self.chunkSize, Int(entry.identity.contentLength - offset))
      guard entry.chunks[String(offset)] == expected else { throw CocoaError(.fileReadCorruptFile) }
      let data = try Data(contentsOf: directory.appendingPathComponent("\(offset).chunk"))
      guard data.count == expected else { throw CocoaError(.fileReadCorruptFile) }
      try handle.write(contentsOf: data)
    }
    try handle.synchronize()
    try FileManager.default.moveItem(at: pending, to: directory.appendingPathComponent("audio"))
    let chunks = entry.chunks
    entry.complete = true
    entry.chunks = [:]
    try persist(entry)
    for offset in chunks.keys {
      try? FileManager.default.removeItem(at: directory.appendingPathComponent("\(offset).chunk"))
    }
  }

  func load() {
    guard !loaded else { return }
    loaded = true
    do {
      try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
      var root = directory
      var values = URLResourceValues()
      values.isExcludedFromBackup = true
      try root.setResourceValues(values)
      for url in try FileManager.default.contentsOfDirectory(
        at: directory, includingPropertiesForKeys: [.isSymbolicLinkKey])
      {
        guard (try? url.resourceValues(forKeys: [.isSymbolicLinkKey]).isSymbolicLink) == false
        else { continue }
        if loadHLSEntry(at: url) { continue }
        let manifest = url.appendingPathComponent("manifest.json")
        guard
          (try? manifest.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? Int.max <= 2_000_000,
          let data = try? Data(contentsOf: manifest),
          data.count <= 2_000_000,
          let entry = try? JSONDecoder().decode(Manifest.self, from: data), entry.schema == 1,
          entry.identity.key == url.lastPathComponent,
          TrackAssetIdentity(
            trackID: entry.identity.trackID, formatID: entry.identity.formatID,
            version: entry.identity.version, contentLength: entry.identity.contentLength) != nil,
          entry.chunks.allSatisfy({ key, size in
            guard let offset = Int64(key), offset >= 0, offset < entry.identity.contentLength,
              offset % Int64(Self.chunkSize) == 0
            else { return false }
            return size == min(Self.chunkSize, Int(entry.identity.contentLength - offset))
          })
        else {
          try? FileManager.default.removeItem(at: url)
          continue
        }
        // Clear interrupted assembly/orphan chunks. The manifest is authoritative.
        let keep = Set(
          entry.complete
            ? ["audio", "manifest.json"]
            : ["manifest.json"] + entry.chunks.keys.map { "\($0).chunk" })
        for child
          in (try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil))
          ?? [] where !keep.contains(child.lastPathComponent)
        {
          try? FileManager.default.removeItem(at: child)
        }
        entries[entry.identity.key] = entry
      }
      trim()
    } catch {
      // An unavailable cache is a miss; streaming can still proceed.
    }
  }

  private func persist(_ entry: Manifest) throws {
    try JSONEncoder().encode(entry).write(
      to: folder(entry.identity.key).appendingPathComponent("manifest.json"),
      options: [.atomic, .completeFileProtectionUntilFirstUserAuthentication])
  }

  func trim() {
    var bytes =
      entries.values.reduce(Int64(0)) { $0 + $1.bytes }
      + hlsEntries.values.reduce(0) { $0 + $1.bytes }
    let candidates =
      entries.map { ($0.key, $0.value.accessed) } + hlsEntries.map { ($0.key, $0.value.accessed) }
    for (key, _) in candidates.sorted(by: { $0.1 < $1.1 }) where bytes > limit {
      bytes -= remove(key)
    }
  }

  @discardableResult
  func remove(_ key: String) -> Int64 {
    if let package = hlsEntries[key] {
      if hlsReaders[key, default: 0] > 0 {
        hlsPendingRemoval.insert(key)
        return 0
      }
      if let url = Self.hlsLocation(bookmark: package.bookmark) {
        try? FileManager.default.removeItem(at: url)
      }
      hlsEntries.removeValue(forKey: key)
      hlsPendingRemoval.remove(key)
      try? FileManager.default.removeItem(at: folder(key))
      return package.bytes
    }
    let bytes = entries[key]?.bytes ?? 0
    try? FileManager.default.removeItem(at: folder(key))
    entries.removeValue(forKey: key)
    return bytes
  }

  func hasCompleteHigh(trackID: String) -> Bool {
    entries.values.contains { $0.complete && $0.identity.trackID == trackID }
  }

  func folder(_ key: String) -> URL {
    directory.appendingPathComponent(key, isDirectory: true)
  }
}