removed unwanted bar now with waveforms, major updates
This commit is contained in:
@@ -594,3 +594,78 @@ enum DJStorageMove {
|
||||
state.finished = true;state.current = "DJ media and waveforms moved";progress(state);return state
|
||||
}
|
||||
}
|
||||
|
||||
struct DJCleanupCandidate: Identifiable, Sendable {
|
||||
var id: String { url.path }
|
||||
let url: URL
|
||||
let size: Int64
|
||||
let modified: Date?
|
||||
}
|
||||
struct DJCleanupPlan: Identifiable, Sendable {
|
||||
let id = UUID()
|
||||
var files: [DJCleanupCandidate]
|
||||
var bytes: Int64 { files.reduce(0){$0+$1.size} }
|
||||
}
|
||||
enum DJLibraryMaintenance {
|
||||
static func managedRoots(database: URL,root: URL) throws -> [URL] {
|
||||
let db = try Library(url:database),location = try DJStorageMove.location(in:db)
|
||||
return Array(Set(((location?.roots ?? [])+[location?.path ?? root.path,root.path]).map{URL(fileURLWithPath:$0).resolvingSymlinksInPath()}))
|
||||
}
|
||||
static func cleanupPlan(database: URL,root: URL,cancelled: () -> Bool = {false},progress: (ImportProgress) -> Void = {_ in}) throws -> DJCleanupPlan {
|
||||
let db = try Library(url:database),songs = try db.songs(),roots = try managedRoots(database:database,root:root)
|
||||
let referenced = Set(songs.flatMap{[$0.audio,$0.cdg].compactMap{$0}.map{URL(fileURLWithPath:$0).resolvingSymlinksInPath()}})
|
||||
var canValidateCaches = true
|
||||
var validCaches = Set<String>(),state = ImportProgress(current:"Checking referenced tracks…"),last = Date.distantPast
|
||||
progress(state)
|
||||
for (index,song) in songs.enumerated() {
|
||||
if cancelled() { throw CancellationError() }
|
||||
if let name = try? WaveformCache.location(audio:URL(fileURLWithPath:song.audio),directory:root).lastPathComponent { validCaches.insert(name) }
|
||||
else { canValidateCaches = false }
|
||||
if Date().timeIntervalSince(last) >= 0.12 { state.current = "Checking reference \(index+1) of \(songs.count)";progress(state);last = Date() }
|
||||
}
|
||||
var files = [DJCleanupCandidate]()
|
||||
for folder in roots.flatMap({[$0.appendingPathComponent("Media"),$0.appendingPathComponent("Waveforms")]}) {
|
||||
if cancelled() { throw CancellationError() }
|
||||
guard let items = FileManager.default.enumerator(at:folder,includingPropertiesForKeys:[.isRegularFileKey,.isSymbolicLinkKey,.fileSizeKey,.contentModificationDateKey],options:[.skipsHiddenFiles,.skipsSubdirectoryDescendants]) else { continue }
|
||||
for case let url as URL in items {
|
||||
if cancelled() { throw CancellationError() }
|
||||
let stem = url.deletingPathExtension().lastPathComponent
|
||||
guard [64,129].contains(stem.count),stem.allSatisfy({$0.isHexDigit || $0 == "-"}) else { continue }
|
||||
let isWaveform = folder.lastPathComponent == "Waveforms" && url.pathExtension == "waveform"
|
||||
let isMedia = folder.lastPathComponent == "Media" && ["mp3","wav","m4a","mp4","m4v","mov","aiff","aif","caf","flac","aac","cdg"].contains(url.pathExtension.lowercased())
|
||||
guard isWaveform || isMedia else { continue }
|
||||
if isWaveform,!canValidateCaches { continue }
|
||||
let values = try url.resourceValues(forKeys:[.isRegularFileKey,.isSymbolicLinkKey,.fileSizeKey,.contentModificationDateKey])
|
||||
guard values.isRegularFile == true,values.isSymbolicLink != true,!referenced.contains(url.resolvingSymlinksInPath()),!isWaveform || !validCaches.contains(url.lastPathComponent) else { continue }
|
||||
files.append(.init(url:url,size:Int64(values.fileSize ?? 0),modified:values.contentModificationDate))
|
||||
if Date().timeIntervalSince(last) >= 0.12 { state.current = "Found \(files.count) unused managed files";progress(state);last = Date() }
|
||||
}
|
||||
}
|
||||
return .init(files:files.sorted{$0.url.path < $1.url.path})
|
||||
}
|
||||
static func clean(_ plan: DJCleanupPlan,database: URL,root: URL,cancelled: () -> Bool = {false},progress: (ImportProgress) -> Void = {_ in},trash: (URL) throws -> Void = { try FileManager.default.trashItem(at:$0,resultingItemURL:nil) }) throws -> ImportProgress {
|
||||
let allowed = Set(try cleanupPlan(database:database,root:root,cancelled:cancelled).files.map(\.id))
|
||||
var state = ImportProgress(total:plan.files.count,current:"Moving unused files to Trash…")
|
||||
for file in plan.files {
|
||||
if cancelled() { throw CancellationError() }
|
||||
let values = try? file.url.resourceValues(forKeys:[.fileSizeKey,.contentModificationDateKey,.isSymbolicLinkKey])
|
||||
if allowed.contains(file.id),values?.isSymbolicLink != true,Int64(values?.fileSize ?? -1) == file.size,values?.contentModificationDate == file.modified {
|
||||
try trash(file.url);state.logLines.append("[REMOVED] "+file.url.path)
|
||||
} else { state.skipped += 1;state.logLines.append("[SKIPPED] "+file.url.path+" (changed or now referenced)") }
|
||||
state.completed += 1;state.current = file.url.lastPathComponent;progress(state)
|
||||
}
|
||||
state.finished = true;state.current = "Cleanup complete";return state
|
||||
}
|
||||
static func removeTracks(_ ids: Set<Int64>,database: URL,cancelled: () -> Bool = {false}) throws {
|
||||
let db = try Library(url:database)
|
||||
try db.execute("BEGIN IMMEDIATE")
|
||||
do {
|
||||
for id in ids {
|
||||
if cancelled() { throw CancellationError() }
|
||||
try db.execute("DELETE FROM reinterpret_undo WHERE song=?",[String(id)])
|
||||
try db.execute("DELETE FROM songs WHERE id=?",[String(id)])
|
||||
}
|
||||
try db.execute("COMMIT")
|
||||
} catch { try? db.execute("ROLLBACK");throw error }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user