tv-anarchy/Sources/TVAnarchyCore/MediaPaths.swift
Natalie b44b5a2d1a feat(@applications): add adult content browsing tab
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
2026-06-09 19:51:12 -07:00

61 lines
3.1 KiB
Swift

import Foundation
/// Path policy for the library. The library is identified by **black-side absolute
/// paths** (`/bigdisk/_/media/`) everywhere that's what black's index emits and
/// what remote players (mpv on black) expect. This project does NOT depend on the
/// NFS `~/media` mount: plum's VLC plays a *downloaded* local copy when present,
/// and anything not downloaded is played on black (mpv) instead it never opens a
/// file through a mount.
///
/// `toRemote` stays as a normalizer so any *legacy* plum mount path left in a
/// persisted cache or watch-history key still resolves to its black-side form
/// (it's the identity on paths that are already black-side).
public enum MediaPaths {
/// Black-side media root (env-overridable, matches the TS bridge default).
public static var remoteRoot: String {
ProcessInfo.processInfo.environment["BLACK_MEDIA_ROOT"] ?? "/bigdisk/_/media"
}
/// Legacy plum mount prefix black-side absolute prefix. Longest first so
/// `~/_/bigdisk/_/media` wins over `~/_/bigdisk`. Only relevant for stale paths
/// persisted before the mount was dropped fresh scans are already black-side.
private static var mappings: [(plum: String, remote: String)] {
let home = FileManager.default.homeDirectoryForCurrentUser.path
return [
(home + "/_/bigdisk/_/media", remoteRoot),
(home + "/media", remoteRoot),
(home + "/_/bigdisk", "/bigdisk"),
]
}
/// Normalize any path to its black-side absolute form. Already-black paths and
/// anything we don't manage pass through unchanged (so it's the identity on the
/// canonical paths the scanner now produces).
public static func toRemote(_ path: String) -> String {
let p = path.hasPrefix("file://") ? String(path.dropFirst(7)) : path
for m in mappings where p.hasPrefix(m.plum) {
return m.remote + String(p.dropFirst(m.plum.count))
}
return p
}
/// URL a local player (plum VLC) opens for a library item. VLC can only open a
/// file it has on disk its sftp access is broken on macOS, and there is no
/// NFS mount so this resolves to a local downloaded copy (`file://`) when one
/// exists. For a file with no local copy, the caller routes playback to the TV
/// (`PlayerController` reroutes to black/mpv) rather than handing it here.
/// An already-resolved URL passes through unchanged.
public static func toStreamURL(_ path: String) -> String {
if path.hasPrefix("file://") || path.hasPrefix("http://")
|| path.hasPrefix("https://") || path.hasPrefix("sftp://") { return path }
if let local = localCopy(of: path) { return "file://" + local }
return "file://" + path // best-effort; reroute should have caught this
}
/// Path of a downloaded local copy of `path`, matched by filename through
/// `DownloadsIndex` (media-fetch preserves names but not the tree layout). nil
/// when nothing matching is downloaded. Never touches `~/media`.
public static func localCopy(of path: String) -> String? {
DownloadsIndex.shared.localPath(for: path)
}
}