Skip to content

Commit

Permalink
Fix covers getting deleted in macOS 15
Browse files Browse the repository at this point in the history
UserNotifications now deletes the attachments on macOS 15 (or did
before, and instead of us never triggering, does so automatically).

Regardless, this is not good for us. Instead of using the image on disk
we use for everything else, make a copy instead in the temporary
directory. On >=15, it'll delete that, and before that, it should get
cleaned automatically anyways.
  • Loading branch information
NattyNarwhal committed Jan 21, 2025
1 parent 8a34a30 commit 279b04b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Doing so isn't fatal (it's not a secret), but it is annoying for other contribut
* Top tracks for an artist can now be displayed
* Similar tracks for an artist (sometimes called "radio") can now be displayed
* Directories can be starred.
* Fix covers being deleted by the system on macOS 15.
* Fix searches being ran twice.
* Fix albums with the same ID across multiple servers being mixed.

Expand Down
9 changes: 8 additions & 1 deletion Submariner/SBPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,15 @@ extension NSNotification.Name {
// This means there's also a bunch of empty dupe cover objects in the DB...
if let newCover = currentTrack.album?.cover, let coverPath = newCover.imagePath {
let coverURL = URL(fileURLWithPath: coverPath as String)
if let attachment = try? UNNotificationAttachment(identifier: "", url: coverURL) {
// macOS 15 starts deleting attachment files; make a copy in temp dir to avoid this fate
let tempURL = URL.temporaryFile(fileExtension: coverURL.pathExtension)
do {
try FileManager.default.copyItem(at: coverURL,
to: tempURL)
let attachment = try UNNotificationAttachment(identifier: "", url: tempURL)
content.attachments = [attachment]
} catch {
// if we fail, then just skip making an attachment
}
}

Expand Down
8 changes: 6 additions & 2 deletions Submariner/URL+Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ extension URL {
// #MARK: -
// #MARK: Temporary Files

static func temporaryFile() -> URL {
static func temporaryFile(fileExtension: String? = nil) -> URL {
let tempDir = FileManager.default.temporaryDirectory
let randomName = UUID().uuidString
var randomName = UUID().uuidString
// better to use the UTType appendingPathExtension if possible
if let fileExtension = fileExtension, !fileExtension.isEmpty {
randomName += ".\(fileExtension)"
}
return tempDir.appendingPathComponent(randomName)
}

Expand Down

0 comments on commit 279b04b

Please sign in to comment.