forked from Read-Write/Submariner
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup operation that runs on startup
Tries to delete debris from Core Data store that could pose problems. More operations can be added. Fixes GH-210
- Loading branch information
1 parent
5cbd481
commit b84a0b9
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// SBLibraryCleanupOrphansOperation.swift | ||
// Submariner | ||
// | ||
// Created by Calvin Buckley on 2024-06-11. | ||
// | ||
// Copyright (c) 2024 Calvin Buckley | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// | ||
|
||
import Cocoa | ||
import os | ||
|
||
fileprivate let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "SBLibraryCleanupOrphansOperation") | ||
|
||
class SBLibraryCleanupOrphansOperation: SBOperation { | ||
init(managedObjectContext: NSManagedObjectContext) { | ||
super.init(managedObjectContext: managedObjectContext, name: "Deleting Orphaned Objects") | ||
} | ||
|
||
override func main() { | ||
defer { | ||
saveThreadedContext() | ||
finish() | ||
} | ||
logger.info("Deleting orphan playlists") | ||
cleanupOrphanPlaylists() | ||
logger.info("Deleting orphan covers") | ||
cleanupOrphanCovers() | ||
// XXX: Do tracks/albums/artists have similar issues? | ||
} | ||
|
||
private func cleanupOrphanPlaylists() { | ||
// look for orphaned playlists as part of a delete | ||
let fetchRequest: NSFetchRequest<SBPlaylist> = SBPlaylist.fetchRequest() | ||
// Server playlists have a server relation, local playlists are in the playlist SBSection | ||
fetchRequest.predicate = NSPredicate(format: "(server == nil) && (section == nil)") | ||
if let playlists = try? threadedContext.fetch(fetchRequest) { | ||
for playlist in playlists { | ||
let name = playlist.resourceName ?? "<nil>" | ||
logger.info("Deleting orphan playlist \"\(name, privacy: .public)\"") | ||
self.threadedContext.delete(playlist) | ||
} | ||
} | ||
} | ||
|
||
private func otherCoversUsingFile(_ cover: SBCover) -> Bool { | ||
// imagePath returns absolute paths, but play it safe. perhaps we can clean up file orphans not in the DB later | ||
guard let imageFile = cover.imagePath?.lastPathComponent else { | ||
return false | ||
} | ||
|
||
let fetchRequest: NSFetchRequest<SBCover> = SBCover.fetchRequest() | ||
fetchRequest.predicate = NSPredicate(format: "(imagePath ENDSWITH %@)", imageFile) | ||
if let covers = try? threadedContext.fetch(fetchRequest) { | ||
logger.debug("\(covers.count) covers with the filename \(imageFile) found") | ||
return covers.count > 1 | ||
} | ||
|
||
return false | ||
} | ||
|
||
private func cleanupOrphanCovers() { | ||
let fetchRequest: NSFetchRequest<SBCover> = SBCover.fetchRequest() | ||
fetchRequest.predicate = NSPredicate(format: "(track == nil) && (album == nil)") | ||
if let covers = try? threadedContext.fetch(fetchRequest) { | ||
for cover in covers { | ||
let name = cover.itemId ?? "<nil>" | ||
logger.info("Deleting orphan cover \"\(name, privacy: .public)\"") | ||
if let path = cover.imagePath as? String, FileManager.default.fileExists(atPath: path), | ||
!otherCoversUsingFile(cover) { | ||
logger.warning("Should delete orphan cover file at \(path)") | ||
// safe to delete - we avoid deleting if any duplicate filename could possibly exist. | ||
// won't get it all, but avoids damage | ||
try? FileManager.default.removeItem(atPath: path) | ||
} | ||
self.threadedContext.delete(cover) | ||
} | ||
} | ||
} | ||
} |