Skip to content

Commit 279b04b

Browse files
committed
Fix covers getting deleted in macOS 15
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.
1 parent 8a34a30 commit 279b04b

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Doing so isn't fatal (it's not a secret), but it is annoying for other contribut
3737
* Top tracks for an artist can now be displayed
3838
* Similar tracks for an artist (sometimes called "radio") can now be displayed
3939
* Directories can be starred.
40+
* Fix covers being deleted by the system on macOS 15.
4041
* Fix searches being ran twice.
4142
* Fix albums with the same ID across multiple servers being mixed.
4243

Submariner/SBPlayer.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,15 @@ extension NSNotification.Name {
393393
// This means there's also a bunch of empty dupe cover objects in the DB...
394394
if let newCover = currentTrack.album?.cover, let coverPath = newCover.imagePath {
395395
let coverURL = URL(fileURLWithPath: coverPath as String)
396-
if let attachment = try? UNNotificationAttachment(identifier: "", url: coverURL) {
396+
// macOS 15 starts deleting attachment files; make a copy in temp dir to avoid this fate
397+
let tempURL = URL.temporaryFile(fileExtension: coverURL.pathExtension)
398+
do {
399+
try FileManager.default.copyItem(at: coverURL,
400+
to: tempURL)
401+
let attachment = try UNNotificationAttachment(identifier: "", url: tempURL)
397402
content.attachments = [attachment]
403+
} catch {
404+
// if we fail, then just skip making an attachment
398405
}
399406
}
400407

Submariner/URL+Parameters.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ extension URL {
1515
// #MARK: -
1616
// #MARK: Temporary Files
1717

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

0 commit comments

Comments
 (0)