broken changes

This commit is contained in:
Mark Gregory
2026-11-05 12:51:29 +00:00
parent 528e10b43a
commit abee603250
11 changed files with 1076 additions and 55 deletions
+56 -8
View File
@@ -1,6 +1,7 @@
import Foundation
import CryptoKit
import AVFoundation
import Darwin
struct ImportProgress: Sendable {
var completed = 0
@@ -14,6 +15,8 @@ struct ImportProgress: Sendable {
struct Importer {
let media: URL
var audioExtensions: Set<String> = ["mp3","wav","m4a","mp4","m4v","mov"]
var includeGraphics = true
@discardableResult func run(_ urls: [URL],library: Library,copyMedia: Bool = true,cancelled: () -> Bool = { false },progress: (ImportProgress) -> Void = { _ in }) throws -> [Int64] {
try ManagedMediaStorage.prepare(media)
var state = ImportProgress()
@@ -42,7 +45,7 @@ struct Importer {
// Discover before copying: overlapping selections must not import twice,
// and a destination inside the selected tree must not feed itself back in.
let keys: Set<URLResourceKey> = [.isDirectoryKey,.isRegularFileKey,.isSymbolicLinkKey,.isPackageKey]
let supported = Set(["mp3","wav","m4a","mp4","m4v","mov","cdg","zip"])
let supported = audioExtensions.union(includeGraphics ? ["cdg","zip"] : ["zip"])
let managedRoot = media.resolvingSymlinksInPath().standardizedFileURL
var visited = Set<URL>(),discovered = Set<URL>()
func discover(_ input:URL) throws {
@@ -154,19 +157,20 @@ struct Importer {
if values.isRegularFile == true { files.append(url);archiveForFile[url.resolvingSymlinksInPath().path] = folder }
}
}
for cdg in files.filter({ $0.pathExtension.lowercased() == "cdg" }) {
for cdg in files.filter({ includeGraphics && $0.pathExtension.lowercased() == "cdg" }) {
if cancelled() { throw CancellationError() }
let matching = try companions(of:cdg)
if let audio = ["mp3","wav","m4a","mp4","m4v","mov"].compactMap({ matching[$0] }).first { files.append(audio) }
if let audio = (["mp3","wav","m4a","mp4","m4v","mov"]+audioExtensions.subtracting(["mp3","wav","m4a","mp4","m4v","mov"]).sorted()).compactMap({ matching[$0] }).first { files.append(audio) }
}
var imported = 0
let audioFiles = Set(files.map { $0.standardizedFileURL }).filter { ["mp3","wav","m4a","mp4","m4v","mov"].contains($0.pathExtension.lowercased()) }.sorted { $0.path < $1.path }
let audioFiles = Set(files.map { $0.standardizedFileURL }).filter { audioExtensions.contains($0.pathExtension.lowercased()) }.sorted { $0.path < $1.path }
state.total = audioFiles.count+state.completed
report("Importing tracks…",force:true)
for audio in audioFiles {
if cancelled() { throw CancellationError() }
report(audio.lastPathComponent)
let cdg = try companions(of:audio)["cdg"]
let cdg: URL?
if includeGraphics { cdg = try companions(of:audio)["cdg"] } else { cdg = nil }
let archive = archiveForFile[audio.resolvingSymlinksInPath().path]
let copy = copyMedia || archive != nil
let signature = try importSignature(audio,cdg:cdg)
@@ -176,6 +180,7 @@ struct Importer {
state.completed += 1;state.skipped += 1
report("Already imported: "+audio.lastPathComponent,force:state.completed == 1);continue
}
guard MediaByteCopy.nonempty(audio) else { throw LibraryError.message("Audio file is empty or unreadable: "+audio.path) }
let hash = try audioFingerprint(audio,cancelled:cancelled)
let graphicsHash = try cdg.map { try audioFingerprint($0,cancelled:cancelled) }
let duplicate = try library.songWithFingerprint(hash)
@@ -183,18 +188,18 @@ struct Importer {
if let graphicsHash,let stored = duplicate?.cdg,FileManager.default.fileExists(atPath:stored) {
sameGraphics = try audioFingerprint(URL(fileURLWithPath:stored),cancelled:cancelled) == graphicsHash
}
if let existing = duplicate,sameGraphics,FileManager.default.fileExists(atPath:existing.audio),
if let existing = duplicate,sameGraphics,MediaByteCopy.nonempty(URL(fileURLWithPath:existing.audio)),
(!copy || URL(fileURLWithPath:existing.audio).deletingLastPathComponent().standardizedFileURL == media.standardizedFileURL) {
try remember(audio,signature,existing.id);songIDs.insert(existing.id)
state.completed += 1;state.skipped += 1
report("Already in library: "+audio.lastPathComponent,force:state.completed == 1);continue
}
let dest = copy ? media.appendingPathComponent(hash+"."+audio.pathExtension.lowercased()) : audio.standardizedFileURL
if copy && !FileManager.default.fileExists(atPath:dest.path) { try FileManager.default.copyItem(at:audio,to:dest) }
if copy { try MediaByteCopy.copy(audio,to:dest,expectedHash:hash,cancelled:cancelled) }
var graphics: String?
if let cdg {
let target = copy ? media.appendingPathComponent(hash+"-"+(graphicsHash ?? "graphics")+".cdg") : cdg.standardizedFileURL
if copy && !FileManager.default.fileExists(atPath:target.path) { try FileManager.default.copyItem(at:cdg,to:target) }
if copy { try MediaByteCopy.copy(cdg,to:target,expectedHash:graphicsHash!,cancelled:cancelled) }
graphics = target.path
}
imported += 1
@@ -387,3 +392,46 @@ enum ManagedMediaStorage {
progress(state);return state
}
}
/// Copy media content, not source ACLs, flags or private security-scope attributes.
/// A failed/cancelled transfer never publishes a partial destination.
enum MediaByteCopy {
static func nonempty(_ url: URL) -> Bool {
guard let values = try? url.resourceValues(forKeys:[.fileSizeKey,.isRegularFileKey]),values.isRegularFile == true else { return false }
return (values.fileSize ?? 0) > 0
}
static func digest(_ url: URL,cancelled: () -> Bool = { false }) throws -> String {
let input = try FileHandle(forReadingFrom:url);defer { try? input.close() }
var hash = SHA256()
while true {
if cancelled() { throw CancellationError() }
guard let bytes = try input.read(upToCount:1_048_576),!bytes.isEmpty else { break }
hash.update(data:bytes)
}
return hash.finalize().map{String(format:"%02x",$0)}.joined()
}
static func copy(_ source: URL,to destination: URL,expectedHash: String,cancelled: () -> Bool = { false }) throws {
if cancelled() { throw CancellationError() }
if nonempty(destination),try digest(destination,cancelled:cancelled) == expectedHash { return }
let temporary = destination.deletingLastPathComponent().appendingPathComponent(".kjmix-import-"+UUID().uuidString)
defer { try? FileManager.default.removeItem(at:temporary) }
do {
let input = try FileHandle(forReadingFrom:source);defer { try? input.close() }
guard FileManager.default.createFile(atPath:temporary.path,contents:nil,attributes:[.posixPermissions:0o644]) else { throw LibraryError.message("Cannot create a file in "+destination.deletingLastPathComponent().path) }
let output = try FileHandle(forWritingTo:temporary);defer { try? output.close() }
var bytesWritten = 0
while true {
if cancelled() { throw CancellationError() }
guard let data = try input.read(upToCount:1_048_576),!data.isEmpty else { break }
try output.write(contentsOf:data);bytesWritten += data.count
}
try output.synchronize();try output.close()
guard bytesWritten > 0,try digest(temporary,cancelled:cancelled) == expectedHash else { throw LibraryError.message("Audio data was empty or changed during copying. Retry the import.") }
if cancelled() { throw CancellationError() }
// Atomic same-directory replacement also repairs a previous zero-byte copy.
let result = temporary.withUnsafeFileSystemRepresentation { from in destination.withUnsafeFileSystemRepresentation { to in Darwin.rename(from!,to!) } }
guard result == 0 else { throw NSError(domain:NSPOSIXErrorDomain,code:Int(errno)) }
} catch is CancellationError { throw CancellationError() }
catch { throw LibraryError.message("Could not import \(source.lastPathComponent) from \(source.deletingLastPathComponent().path) into \(destination.deletingLastPathComponent().path): \(error.localizedDescription)") }
}
}