Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Offline/StorageManager.swift3// Description: Where things live on disk, and the only code allowed to delete or move the listener's files.4import Foundation5import CryptoKit67public struct StorageUsage: Sendable, Equatable {8 public var libraryRoot: URL9 public var rootAvailable: Bool10 public var downloadedBytes: Int64 = 011 public var partialBytes: Int64 = 012 public var streamCacheBytes: Int64 = 013 public var artworkBytes: Int64 = 014 public var freeBytes: Int64 = 015 public var volumeName: String = ""16}1718public enum StorageError: Error, Sendable, Equatable {19 case rootUnavailable // the library folder (external disk?) is not there20 case notManaged // refusing to touch a path outside the app's own folders21 case insufficientSpace(needed: Int64, free: Int64)22 case destinationNotEmpty23}2425/// Where things live on disk, and the only code allowed to delete or move the listener's files.26///27/// <library root>/ chosen by the listener (default ~/Music/SP Music/<account>)28/// .spmusic/library.json marker: this folder is managed by the app, for this account29/// .spmusic/partial/ downloads in progress30/// <Artist>/<Album>/01 Title.flac the original files, untouched, readable by any player31/// <data dir>/ ~/Library/Application Support/SP Music/Accounts/<account>32/// library.sqlite, artwork/, stream-cache/33public final class StorageManager: @unchecked Sendable {34 public let dataDirectory: URL35 public let accountKey: String36 private let lock = NSLock()37 private var root: URL38 private let fm = FileManager.default3940 public init(dataDirectory: URL, libraryRoot: URL, accountKey: String) {41 self.dataDirectory = dataDirectory; self.root = libraryRoot; self.accountKey = accountKey42 try? fm.createDirectory(at: dataDirectory, withIntermediateDirectories: true)43 try? fm.createDirectory(at: artworkDirectory, withIntermediateDirectories: true)44 try? fm.createDirectory(at: streamCacheDirectory, withIntermediateDirectories: true)45 }4647 public var libraryRoot: URL { lock.withLock { root } }48 public var artworkDirectory: URL { dataDirectory.appendingPathComponent("artwork", isDirectory: true) }49 public var streamCacheDirectory: URL { dataDirectory.appendingPathComponent("stream-cache", isDirectory: true) }50 public var databaseURL: URL { dataDirectory.appendingPathComponent("library.sqlite") }51 public var partialDirectory: URL { libraryRoot.appendingPathComponent(PathGuard.markerDirectory).appendingPathComponent("partial", isDirectory: true) }52 private func marker(in root: URL) -> URL { root.appendingPathComponent(PathGuard.markerDirectory).appendingPathComponent(PathGuard.markerFile) }5354 /// True when the library folder is present and marked (an unplugged external disk makes this false; nothing is deleted then).55 public var isRootAvailable: Bool { PathGuard.hasMarker(libraryRoot) }5657 /// Creates and marks the library folder. Fails rather than adopt a folder marked for another account.58 public func prepareRoot() throws {59 let r = libraryRoot60 try fm.createDirectory(at: r.appendingPathComponent(PathGuard.markerDirectory).appendingPathComponent("partial"), withIntermediateDirectories: true)61 let m = marker(in: r)62 if let data = try? Data(contentsOf: m), let obj = try? JSONSerialization.jsonObject(with: data) as? [String: Any], let owner = obj["account"] as? String {63 if owner != accountKey { throw StorageError.notManaged }64 return65 }66 let body = try JSONSerialization.data(withJSONObject: ["account": accountKey, "app": "SP Music", "created": APIClient.iso(Date()),67 "note": "Les fichiers audio de ce dossier sont les originaux, non modifiés. Ce fichier indique seulement que SP Music gère ce dossier."], options: [.prettyPrinted])68 try body.write(to: m, options: .atomic)69 }7071 public func fileURL(relativePath: String) -> URL { libraryRoot.appendingPathComponent(relativePath) }7273 /// The downloaded file of a track, when it is really there.74 public func localFile(for track: Track) -> URL? {75 guard track.isDownloaded, let rel = track.localRelativePath else { return nil }76 let url = fileURL(relativePath: rel)77 return fm.fileExists(atPath: url.path) ? url : nil78 }7980 public static func fileExtension(codec: String, container: String, mime: String?) -> String {81 switch codec.lowercased() {82 case "flac": return "flac"83 case "mp3": return "mp3"84 case "alac": return "m4a"85 case "aac": return container.lowercased() == "aac" ? "aac" : "m4a"86 case "vorbis", "opus": return "ogg"87 case let c where c.hasPrefix("dsd"): return container.lowercased() == "dff" ? "dff" : "dsf"88 default: break89 }90 switch container.lowercased() {91 case "wav": return "wav"92 case "aiff": return "aiff"93 case "mov", "mp4", "m4a": return "m4a"94 default: return mime == "audio/flac" ? "flac" : "bin"95 }96 }9798 /// "Artist/Album (2019)/1-03 Title.flac": readable, stable, never proprietary.99 public func proposedRelativePath(for t: Track, albumTrackHasDiscs: Bool) -> String {100 let artist = PathGuard.safeComponent(t.albumArtistName.isEmpty ? t.artistName : t.albumArtistName, fallback: "Artiste inconnu")101 var album = PathGuard.safeComponent(t.albumTitle, fallback: "Album inconnu")102 if let y = t.year, !album.contains("(\(y))") { album += " (\(y))" }103 var prefix = ""104 if let n = t.trackNo { prefix = (albumTrackHasDiscs || t.discNo > 1 ? "\(t.discNo)-" : "") + String(format: "%02d ", n) }105 let name = PathGuard.safeComponent(prefix + t.title, fallback: t.id) + "." + Self.fileExtension(codec: t.codec, container: t.container, mime: t.mime)106 return [artist, album, name].joined(separator: "/")107 }108109 public func partialURL(trackID: String, assetID: String?) -> URL {110 partialDirectory.appendingPathComponent("\(trackID).\(assetID ?? "na").part")111 }112113 /// Moves a verified file into the library. The destination is replaced atomically when it already exists.114 public func install(_ temp: URL, relativePath: String) throws {115 guard isRootAvailable else { throw StorageError.rootUnavailable }116 let dest = fileURL(relativePath: relativePath)117 guard PathGuard.isInside(dest.deletingLastPathComponent().appendingPathComponent("x"), root: libraryRoot) else { throw StorageError.notManaged }118 try fm.createDirectory(at: dest.deletingLastPathComponent(), withIntermediateDirectories: true)119 if fm.fileExists(atPath: dest.path) { _ = try fm.replaceItemAt(dest, withItemAt: temp) } else { try fm.moveItem(at: temp, to: dest) }120 }121122 /// Deletes one downloaded file, then its folders when they are left empty. Refuses anything outside the marked library.123 public func deleteDownloadedFile(relativePath: String) throws {124 let root = libraryRoot125 let url = fileURL(relativePath: relativePath)126 guard PathGuard.mayDelete(url, root: root) else { throw fm.fileExists(atPath: root.path) ? StorageError.notManaged : StorageError.rootUnavailable }127 if fm.fileExists(atPath: url.path) { try fm.removeItem(at: url) }128 var dir = url.deletingLastPathComponent()129 while PathGuard.isInside(dir, root: root) {130 let left = ((try? fm.contentsOfDirectory(atPath: dir.path)) ?? ["?"]).filter { $0 != ".DS_Store" && $0 != "cover.jpg" }131 guard left.isEmpty else { break }132 try? fm.removeItem(at: dir)133 dir = dir.deletingLastPathComponent()134 }135 }136137 public func removePartial(trackID: String) {138 for name in (try? fm.contentsOfDirectory(atPath: partialDirectory.path)) ?? [] where name.hasPrefix(trackID + ".") {139 let u = partialDirectory.appendingPathComponent(name)140 if PathGuard.mayDelete(u, root: libraryRoot) { try? fm.removeItem(at: u) }141 }142 }143144 public static func directorySize(_ url: URL) -> Int64 {145 guard let e = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.fileSizeKey, .isRegularFileKey]) else { return 0 }146 var total: Int64 = 0147 for case let f as URL in e {148 if let v = try? f.resourceValues(forKeys: [.fileSizeKey, .isRegularFileKey]), v.isRegularFile == true { total += Int64(v.fileSize ?? 0) }149 }150 return total151 }152153 public static func freeSpace(at url: URL) -> Int64 {154 var probe = url155 while !FileManager.default.fileExists(atPath: probe.path) && probe.pathComponents.count > 1 { probe = probe.deletingLastPathComponent() }156 let v = try? probe.resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey, .volumeAvailableCapacityKey])157 return v?.volumeAvailableCapacityForImportantUsage ?? Int64(v?.volumeAvailableCapacity ?? 0)158 }159160 public func usage(downloadedBytes: Int64) -> StorageUsage {161 let root = libraryRoot162 var u = StorageUsage(libraryRoot: root, rootAvailable: isRootAvailable)163 u.downloadedBytes = downloadedBytes164 u.partialBytes = Self.directorySize(partialDirectory)165 u.streamCacheBytes = Self.directorySize(streamCacheDirectory)166 u.artworkBytes = Self.directorySize(artworkDirectory)167 u.freeBytes = Self.freeSpace(at: root)168 u.volumeName = (try? root.resourceValues(forKeys: [.volumeNameKey]))?.volumeName ?? ""169 return u170 }171172 public func clearStreamCache() {173 for name in (try? fm.contentsOfDirectory(atPath: streamCacheDirectory.path)) ?? [] { try? fm.removeItem(at: streamCacheDirectory.appendingPathComponent(name)) }174 }175 public func clearArtworkCache() {176 for name in (try? fm.contentsOfDirectory(atPath: artworkDirectory.path)) ?? [] { try? fm.removeItem(at: artworkDirectory.appendingPathComponent(name)) }177 }178179 /// Keeps the streaming cache under `limit` bytes by removing the files used longest ago.180 public func trimStreamCache(limit: Int64, keeping: Set<String> = []) {181 let keys: [URLResourceKey] = [.fileSizeKey, .contentAccessDateKey, .contentModificationDateKey]182 var files = ((try? fm.contentsOfDirectory(at: streamCacheDirectory, includingPropertiesForKeys: keys)) ?? []).compactMap { u -> (URL, Int64, Date)? in183 guard let v = try? u.resourceValues(forKeys: Set(keys)) else { return nil }184 return (u, Int64(v.fileSize ?? 0), v.contentAccessDate ?? v.contentModificationDate ?? .distantPast)185 }186 var total = files.reduce(0) { $0 + $1.1 }187 files.sort { $0.2 < $1.2 }188 for f in files where total > limit {189 if keeping.contains(where: { f.0.lastPathComponent.hasPrefix($0) }) { continue }190 try? fm.removeItem(at: f.0); total -= f.1191 }192 }193194 /// Moves the whole library (files, partial downloads, marker) to another folder, possibly on another disk.195 /// Files are copied and verified by size before anything is removed from the old place; relative paths do not196 /// change, so the database needs no rewrite. `progress` receives (bytes done, bytes total).197 public func moveLibrary(to newRoot: URL, files: [(relativePath: String, size: Int64)], progress: @Sendable (Int64, Int64) -> Void) throws {198 let old = libraryRoot199 guard PathGuard.resolved(old) != PathGuard.resolved(newRoot) else { return }200 guard !PathGuard.isInside(newRoot, root: old), !PathGuard.isInside(old, root: newRoot) else { throw StorageError.notManaged }201 guard PathGuard.hasMarker(old) else { throw StorageError.rootUnavailable }202 try fm.createDirectory(at: newRoot, withIntermediateDirectories: true)203 if PathGuard.hasMarker(newRoot) {204 let data = try Data(contentsOf: marker(in: newRoot))205 let owner = (try? JSONSerialization.jsonObject(with: data) as? [String: Any])?["account"] as? String206 if owner != accountKey { throw StorageError.notManaged }207 } else if !((try? fm.contentsOfDirectory(atPath: newRoot.path)) ?? []).filter({ $0 != ".DS_Store" }).isEmpty {208 throw StorageError.destinationNotEmpty209 }210 let total = files.reduce(0) { $0 + $1.size }211 let free = Self.freeSpace(at: newRoot)212 let sameVolume = (try? old.resourceValues(forKeys: [.volumeIdentifierKey]).volumeIdentifier as? NSObject) == (try? newRoot.resourceValues(forKeys: [.volumeIdentifierKey]).volumeIdentifier as? NSObject)213 if !sameVolume && free < total + 200_000_000 { throw StorageError.insufficientSpace(needed: total, free: free) }214215 try fm.createDirectory(at: newRoot.appendingPathComponent(PathGuard.markerDirectory).appendingPathComponent("partial"), withIntermediateDirectories: true)216 var done: Int64 = 0217 var copied: [String] = []218 do {219 for f in files {220 let src = old.appendingPathComponent(f.relativePath), dst = newRoot.appendingPathComponent(f.relativePath)221 guard fm.fileExists(atPath: src.path) else { continue }222 try fm.createDirectory(at: dst.deletingLastPathComponent(), withIntermediateDirectories: true)223 if fm.fileExists(atPath: dst.path) { try fm.removeItem(at: dst) }224 try fm.copyItem(at: src, to: dst)225 let a = (try? fm.attributesOfItem(atPath: src.path)[.size] as? NSNumber)?.int64Value, b = (try? fm.attributesOfItem(atPath: dst.path)[.size] as? NSNumber)?.int64Value226 guard a != nil, a == b else { throw CocoaError(.fileWriteUnknown) }227 copied.append(f.relativePath)228 // The album's cover travels with its first file.229 let cover = src.deletingLastPathComponent().appendingPathComponent("cover.jpg"), coverDst = dst.deletingLastPathComponent().appendingPathComponent("cover.jpg")230 if fm.fileExists(atPath: cover.path), !fm.fileExists(atPath: coverDst.path) { try? fm.copyItem(at: cover, to: coverDst) }231 done += f.size; progress(done, total)232 }233 try Data(contentsOf: marker(in: old)).write(to: marker(in: newRoot), options: .atomic)234 } catch {235 // Nothing was removed from the old folder: undo the partial copy and report.236 for rel in copied { try? fm.removeItem(at: newRoot.appendingPathComponent(rel)) }237 throw error238 }239 lock.withLock { root = newRoot }240 // Only now, and only what the app put there, leaves the old folder.241 for rel in copied {242 let src = old.appendingPathComponent(rel)243 guard PathGuard.mayDelete(src, root: old) else { continue }244 try? fm.removeItem(at: src)245 let cover = src.deletingLastPathComponent().appendingPathComponent("cover.jpg")246 var dir = src.deletingLastPathComponent()247 if ((try? fm.contentsOfDirectory(atPath: dir.path)) ?? []).filter({ $0 != ".DS_Store" && $0 != "cover.jpg" }).isEmpty { try? fm.removeItem(at: cover) }248 while PathGuard.isInside(dir, root: old), ((try? fm.contentsOfDirectory(atPath: dir.path)) ?? ["?"]).filter({ $0 != ".DS_Store" }).isEmpty {249 try? fm.removeItem(at: dir); dir = dir.deletingLastPathComponent()250 }251 }252 try? fm.removeItem(at: old.appendingPathComponent(PathGuard.markerDirectory))253 if ((try? fm.contentsOfDirectory(atPath: old.path)) ?? ["?"]).filter({ $0 != ".DS_Store" }).isEmpty { try? fm.removeItem(at: old) }254 }255256 public static func sha256(of url: URL) throws -> String {257 let h = try FileHandle(forReadingFrom: url); defer { try? h.close() }258 var hasher = SHA256()259 while let chunk = try h.read(upToCount: 1 << 20), !chunk.isEmpty { hasher.update(data: chunk) }260 return hasher.finalize().map { String(format: "%02x", $0) }.joined()261 }262}263