removed unwanted bar now with waveforms, major updates

This commit is contained in:
Mark Gregory
2026-11-06 13:09:24 +00:00
parent 7b45184dff
commit f6368e4ae7
7 changed files with 243 additions and 41 deletions
+77 -1
View File
@@ -411,7 +411,7 @@ enum ManagedMediaStorage {
guard try hash(temporary,cancelled:cancelled) == expected else { throw LibraryError.message("Verification failed for \(source.lastPathComponent). The original has been kept.") }
try FileManager.default.moveItem(at:temporary,to:target)
}
verified.append((source,target));state.completed += 1
verified.append((source,target));state.completed += 1;state.logLines.append("[COPIED] "+source.path)
if Date().timeIntervalSince(lastReport) >= 0.1 || state.completed == 1 || state.completed == state.total { progress(state);lastReport = Date() }
}
if cancelled() { throw CancellationError() }
@@ -488,3 +488,79 @@ enum MediaByteCopy {
catch { throw LibraryError.message("Could not import \(source.lastPathComponent) from \(source.deletingLastPathComponent().path) into \(destination.deletingLastPathComponent().path): \(error.localizedDescription)") }
}
}
/// DJ media and waveform cache relocation; the library database stays on this Mac.
enum DJStorageMove {
static let key = "dj.storage.v1"
static func location(in library: Library) throws -> MediaStorageLocation? {
guard let text = try library.setting(key) else { return nil }
return try JSONDecoder().decode(MediaStorageLocation.self,from:Data(text.utf8))
}
static func run(database: URL,defaultRoot: URL,destination: URL,cancelled: () -> Bool = {false},progress: (ImportProgress) -> Void = {_ in}) throws -> ImportProgress {
let db = try Library(url:database),old = try location(in:db)
let oldRoots = Array(Set((old?.roots ?? [])+[defaultRoot.path,old?.path ?? defaultRoot.path])).map{URL(fileURLWithPath:$0)}
let target = destination.resolvingSymlinksInPath(),media = target.appendingPathComponent("Media"),waves = target.appendingPathComponent("Waveforms")
try ManagedMediaStorage.prepare(media);try ManagedMediaStorage.prepare(waves)
let oldSongs = try db.songs()
var cacheSources = [Int64:URL](),allCaches = Set<URL>()
for folder in oldRoots.map({$0.appendingPathComponent("Waveforms")}) {
if let files = try? FileManager.default.contentsOfDirectory(at:folder,includingPropertiesForKeys:[.isRegularFileKey,.isSymbolicLinkKey]) {
for file in files where file.pathExtension == "waveform" {
let attributes = try file.resourceValues(forKeys:[.isRegularFileKey,.isSymbolicLinkKey])
if attributes.isRegularFile == true,attributes.isSymbolicLink != true { allCaches.insert(file.resolvingSymlinksInPath()) }
}
}
for song in oldSongs where cacheSources[song.id] == nil {
if let file = try? WaveformCache.location(audio:URL(fileURLWithPath:song.audio),directory:folder),FileManager.default.fileExists(atPath:file.path) { cacheSources[song.id] = file.resolvingSymlinksInPath() }
}
}
let manifestKey = "dj.storage.waveform.sources.v1"
if let json = try db.setting(manifestKey),let saved = try? JSONDecoder().decode([String:String].self,from:Data(json.utf8)) {
let folders = Set(oldRoots.map{$0.appendingPathComponent("Waveforms").resolvingSymlinksInPath()})
for song in oldSongs where cacheSources[song.id] == nil {
if let path = saved[String(song.id)] {
let file = URL(fileURLWithPath:path).resolvingSymlinksInPath()
if folders.contains(file.deletingLastPathComponent()),file.pathExtension == "waveform",FileManager.default.fileExists(atPath:file.path) { cacheSources[song.id] = file;allCaches.insert(file) }
}
}
}
if cancelled() { throw CancellationError() }
try db.setSetting(manifestKey,String(decoding:try JSONEncoder().encode(Dictionary(uniqueKeysWithValues:cacheSources.map{(String($0.key),$0.value.path)})),as:UTF8.self))
var mediaRoots = oldRoots.map{$0.appendingPathComponent("Media")}
if let json = try db.setting(MediaStorageLocation.key),let location = try? JSONDecoder().decode(MediaStorageLocation.self,from:Data(json.utf8)) { mediaRoots += (location.roots+[location.path]).map{URL(fileURLWithPath:$0)} }
var state = try ManagedMediaStorage.move(database:database,roots:mediaRoots,destination:media,cancelled:cancelled,progress:{ value in var update = value;update.finished = false;progress(update) })
state.finished = false
// Commit the new location before the cache phase so a cancelled cache copy
// still reopens the successfully relocated media on the next launch.
let saved = ManagedMediaStorage.location(target,roots:oldRoots.map(\.path))
try db.setSetting(key,String(decoding:try JSONEncoder().encode(saved),as:UTF8.self))
let songs = try db.songs()
var transfers = [URL:Set<URL>]()
let referencedCaches = Set(cacheSources.values)
for source in allCaches where !referencedCaches.contains(source) { transfers[source,default:[]].insert(waves.appendingPathComponent(source.lastPathComponent)) }
for song in songs {
if let source = cacheSources[song.id] { transfers[source,default:[]].insert(try WaveformCache.location(audio:URL(fileURLWithPath:song.audio),directory:waves)) }
}
state.total += transfers.values.reduce(0){$0+$1.count}
var lastCacheReport = Date.distantPast
progress(state)
for source in transfers.keys.sorted(by:{$0.path < $1.path}) {
if cancelled() { throw CancellationError() }
let hash = try ManagedMediaStorage.hash(source,cancelled:cancelled).map{String(format:"%02x",$0)}.joined()
for target in transfers[source]!.sorted(by:{$0.path < $1.path}) {
if cancelled() { throw CancellationError() }
state.current = "Moving waveform "+source.lastPathComponent
if source.standardizedFileURL != target.standardizedFileURL { try MediaByteCopy.copy(source,to:target,expectedHash:hash,cancelled:cancelled) }
state.completed += 1;state.logLines.append("[COPIED] "+target.path)
if Date().timeIntervalSince(lastCacheReport) >= 0.12 || state.completed == state.total { progress(state);lastCacheReport = Date() }
}
// All copies of this cache are verified before its original is removed.
if source.deletingLastPathComponent().standardizedFileURL != waves.standardizedFileURL {
let unchanged = try ManagedMediaStorage.hash(source,cancelled:cancelled).map{String(format:"%02x",$0)}.joined() == hash
if unchanged { try FileManager.default.removeItem(at:source) }
}
}
try db.setSetting(manifestKey,"")
state.finished = true;state.current = "DJ media and waveforms moved";progress(state);return state
}
}